Unit testing in C++
In C++, unit testing often uses frameworks like Google Test, Catch2, or CTest itself (from the CMake ecosystem).
Here's a simple example using gtest
:
#include "mylibrary.h"
#include "gtest/gtest.h"
TEST(MyLibrary, AddTwoNumbers) {
EXPECT_EQ(add(2, 3), 5);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
In this example, we test the add
function from the mylibrary
module.