Creating the most basic unit test for CTest
Writing unit tests is technically possible without any kind of framework. All we have to do is create an instance of the class we want to test, execute one of its methods, and check if the new state or value returned meets our expectations. Then, we report the result and delete the object under test. Let's try it out.
We'll use the following structure:
- CMakeLists.txt - src |- CMakeLists.txt |- calc.cpp |- calc.h |- main.cpp - test |- CMakeLists.txt |- calc_test.cpp
Starting from main.cpp
, we can see it will use a Calc
class, as illustrated in the following code snippet:
chapter08/01-no-framework/src/main.cpp
#include <iostream> #include "calc.h" using namespace std; int main() { Calc c; cout << "2 + 2 = " << c.Sum(2, 2) << endl; cout << "3 * 3 = "...