# GTest include code wouldĪdd_executable(multiply_test multiply_test.cpp) TEST(MultiplyTests, TestIntegerZero_Zero)įinally, to complete the setup, we define the CMake test targets in the file tests/CMakeLists.txt below. Check the content of the relevant C++ source file below. Luckily for you, I created an executable with a few unit tests. Having created our dummy library with a few public functions, we now need to test it. int multiply(int a, int b) ĭouble multiply(double a, double b) Defining The Google Tests #include Ĭomplementary, you can find the contents of the header file src/multiply/include/multiply/multiply.h below. With the above CMake structure in mind for our dummy target, the source file multiply.cpp has the definitions of the functions for our multiply library. Target_include_directories(multiply PUBLIC include) Specifically, the interesting bits will be under the multiply directory, so the file src/CMakeLists.txt only has a call to add_subdirectory(multiply) in it.įor this reason, the file src/multiply/CMakeLists.txt defines our library multiply, and the include directories for it, as you can see below. Under the directory src, we define the code and CMake targets for our dummy library multiply. Creating A Dummy CMake Library For Our Google Tests Lastly, we add the subdirectories for the library source code and the test code. Remember to add enable_testing() in your root CMakeLists.txt, before any tests are created. Interestingly, we need the call to enable_testing() as it tells CMake to generate the tests that we will later on create. cmake_minimum_required(VERSION 3.16)Ĭlearly, we’re setting the minimum version of CMake, declaring the project name, and immediately settings the C++ standard version boilerplate code. Main CMakeLists.txt – Defining The Project And Adding SubdirectoriesĪs you can see below, the main CMakeLists.txt file declares the project properties and CMake’s boilerplate code. Additionally, each directory has a CMakeLists.txt, either to include the subdirectories or to define targets in the current directory. However, we basically have all the code for the library under src/multiply, and the test executable under tests. Moreover, you can find the contents of important files in the following subsections. In the textbox below, you can find the directory structure for the basic example we’re considering in this post. Particularly, we create a target called multiply, which defined three functions to multiply two numbers. Specifically, we consider a library that defines three different functions. Unsurprisingly, we will look at a very similar setup in this post. For example, say your project defines a library with some common code and an executable that uses the functions inside the library. Believe it or not, I’ve seen many projects (commercial and open-source) that don’t, and they end up wasting a lot of time writing their own flawed testing frameworks! Fitting Google Test Into A CMake Typical Project – The Big Pictureįirstly, let’s look at an example of a typical C++ project. Hopefully, one of the options above will persuade you to use Google Tests in your projects. My thoughts on different methods of integrating GoogleTest.Pulling GTest as a third-party dependency with Vcpkg.Pulling GTest as a third-party dependency with Conan.Using CMake’s fetch_content to pull GTest’s code.In this post, we look at three different ways to achieve this: Getting started with Google Tests in a CMake project is very easy. What’s even more interesting? Google Tests can be integrated quite easily into your CMake projects, using CMake’s test tool: CTest. In addition, it creates a very light-weight executable/program with beautiful pass/failure outputs based on your test outcomes. If you haven’t yet come across Google Test, it simply allows you to define test cases with its API. Figure 1: Building a GTest project and running the tests. Unsurprisingly, Google Test is the most popular testing framework for these purposes, and it can be easily integrated with CMake. For C++ software engineers, this means having many unit and functional tests that can tell you how your code behaves in certain situations. During software development, it’s important to be able to test your code, so you can catch those pesky bugs as early as possible in the development process.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |