CPlusPlusUnit - Tiny single source file C++ Unit testing TDD framework with the output message format like Python unittest.
No installation is required, just copy Cppunit
class definition form cppunit.h into your source file and you are all set.
This is free and unencumbered software released into the public domain. For more information, please refer to http://unlicense.org
For single source file programs:
- Copy
Cppunit
class definition from cppunit.h into your source file - Make sure you have listed all required headers or just
#include <bits/stdc++.h>
is okay - It has to be located before
main()
function and all unit tests
For larger projects:
- Copy cppunit.h file into your project area
- Include it as needed by
#include "cppunit.h"
Next steps:
- Create a new class derived from
Cppunit
- Redefine
test_list()
orsingle_test()
(if only one test is needed) method of the derived class - Write tests by using
CHECK*
macros andtest_cin()
to mock user'sstdin
input stream - Instantiate and Call
run()
method of the derived class to invoke unit tests
See cppunit_test.cc for a simple working example
These macros will provide file, line and test name information in case of checking mismatches:
- Integral type match check
CHECK(2 + 3, 4)
- Boolean type check
CHECKT(2 + 2 == 4)
- String type match check
CHECKS("a" "b", "ac")
Cppunit will override user's stdin
input stream. Use test_cin()
method to provide user's mock input stream.
test_cin("4 1\n-3 -3 -3 -3");
This feature is very useful to test programs written for online judges like: codeforces.com, topcoder.com, spoj etc.
In case of passed tests it prints the number of checks passed and the elapsed time
...
--------------------------------------------------
Ran 3 checks in 0.002s
OK
Prints error message for each failed check
F.F
==================================================
FAIL: single_test
--------------------------------------------------
File "cppunit.cc", line 51 in single_test
Checking 2 + 3 == 4
Error: "5" ! = "4"
==================================================
FAIL: single_test
--------------------------------------------------
File "cppunit.cc", line 57 in single_test
Checking "a" "b" == "ac"
Error: "ab" ! = "ac"
--------------------------------------------------
Ran 3 checks in 0.002s
FAILED (failures=2)