Unit Testing in C++
a) Why is Testing Important?
Unit testing is a crucial aspect of software development. Here's why:
- Quality Assurance: Unit tests ensure that individual units of code (like functions or methods) work as intended.
- Regression Detection: When changes are made to the codebase, unit tests can detect if previously working functionality has been broken.
- Documentation: Tests provide a form of documentation by showcasing how a piece of code is intended to be used.
- Design: Writing tests can help in designing better and more modular code.
- Confidence: With a comprehensive suite of tests, developers can make changes with the confidence that they haven't inadvertently introduced bugs.
Example:
Imagine you have a function add(int a, int b)
that returns the sum of two numbers. A unit test would ensure that add(2, 3)
always returns 5
.
b) Testing in CMake with CTest
CTest is a testing tool distributed as a part of CMake. It allows you to easily create, manage, and run your tests.
Basic Example:
-
In your
CMakeLists.txt
, enable testing:enable_testing()
-
Add a test executable:
add_executable(my_test test.cpp)
-
Add the test to CTest:
add_test(NAME MyTest COMMAND my_test)
-
Build and run tests:
mkdir build && cd build
cmake ..
make
ctest
Documentation: CTest Documentation
Using FetchContent
is a popular way to integrate dependencies directly into a CMake project. Here's how you can integrate Google Test using FetchContent
:
c) Testing in CMake with Google Test
Google Test is a popular C++ testing framework. Here's how you can integrate it with CMake using FetchContent
:
-
Installation using
FetchContent
:In your
CMakeLists.txt
, you can fetch Google Test as follows:include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
)
FetchContent_MakeAvailable(googletest)This will download, build, and make Google Test available in your project.
-
Integration with CMake:
After fetching Google Test, you can link against it:
add_executable(my_gtest test_gtest.cpp)
target_link_libraries(my_gtest gtest_main) -
Writing a Test:
#include <gtest/gtest.h>
TEST(MyTestSuite, MyTestCase) {
EXPECT_EQ(2 + 3, 5);
}With
gtest_main
as a linked library, you don't need amain
function; Google Test provides one for you. -
Build and run tests as you would with CTest.
Documentation: Google Test Github Repository