Unit Test Projects
Making a basic Unit Test project.
Unit Test Project Template
You can use the UnitTestProject template just like the BasicProject template.
The starter code uses a file named tests.cpp instead of main.cpp.
The first lines set up the doctest unit testing framework, and you should leave them alone.
You can #include any headers you need after that, and replace the example functions
and their tests with your own code.
Separating Functions from Tests
To place your functions in a separate file from the unit tests, do the following:
Add new .h and .cpp files to the project folder and name them something like
functions.handfunctions.cpp.To make sure the code gets compiled, add the .cpp file to your
Makefile'sSHARED_FILESlist by making that line look like:SHARED_FILES = functions.cppInclude your .h file from the files with the unit tests after the doctest setup:
... using doctest::Approx; #include "functions.h" //<------ Include your .h file TEST_CASE...To test your setup, add this function declaration to your
.h:int foo();This definition to your
.cpp:#include "functions.h" //<------ Include the .h file int foo() { return 42; }And this test to your file with tests:
TEST_CASE( "test setup" ) { CHECK( foo() == 42 ); }If you get an error about an
undefined reference, double-check your makefile.