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:
Cppunit
class definition from cppunit.h into your source file#include <bits/stdc++.h>
is okaymain()
function and all unit testsFor larger projects:
#include "cppunit.h"
Next steps:
Cppunit
test_list()
or single_test()
(if only one test is needed) method of the derived classCHECK*
macros and test_cin()
to mock user’s stdin
input streamrun()
method of the derived class to invoke unit testsSee cppunit_test.cc for a simple working example
These macros will provide file, line and test name information in case of checking mismatches:
CHECK(2 + 3, 4)
CHECKT(2 + 2 == 4)
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)