Simple header-only test framework in C++11.
- Easy to add and run tests:
- Add a cpp file to your project.
- In it, create a test function:
deftest_basic([object to test], [test name]) {} - In your
mainfunction, callrs::test_harness::run.
- Add a cpp file to your project.
- Filters to limit the total number of tests, or run a specific test.
- Test failure exception provides << insertion operator to append info about the failure.
Note that this framework is missing some significant features you'd expect from a full-fledged testing framework, such as setups and teardowns. However, this is a good starting point if you want to roll your own, and said features wouldn't be difficult to implement.
deftest_basic(fstream, file_io)
{
std::ofstream ofile("test_file.txt");
testassert(ofile.is_open()) << "Output file could not be opened";
ofile << "Avoid using file I/O in tests." << std::endl;
ofile << "The success of your test may be bound to OS filesystem conditions." << std::endl;
ofile.close();
std::ifstream ifile("test_file.txt");
testassert(ifile.is_open()) << "Input file could not be opened";
std::remove("test_file.txt");
}The test is automatically registered with the test harness and executed when you call test_harness::run.
- Download the repo to your machine.
- Copy the
testfolder (from withincode) into your project. - Add the path containing the new
testfolder to the include directories for your build. - In your code
#include "test/test.hpp".
On Windows:
- In the
projectfolder, run eithergmake.batorvs2013.bat. - This will generate a Makefile or Visual Studio solution you can use to build the project.
On other platforms:
- Clone the premake-dev repo and compile.
- Run the resulting executable file in the
codefolder with the command./premake5 gmake - This will generate a Makefile in the
projectfolder.
Given the structure of having an itest separate from its itest_desc description object allows us to implement setup and teardown functions easily. The process would look like this:
- Add getters in
itest_descfor setup/teardown functions. - Add setup/teardown functions to the fields in the
base_test_descclass. - Add code in
test_harnessto callsetupprior torunandteardownafterrun.
Then you can assign setup and teardown functions from within a desctest(test_t) function.