I have been using a library called libunittest for unit testing an open source project. A couple months ago I did a feature request for it, namely for an assertion for relative approximation. It’s an incredibly easy library to use and comes with a lot of documentation. Here is one of the examples which shows the “easy” way where you don’t have to register the test class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// easy.cpp #include <libunittest/all.hpp> using namespace unittest::assertions; TEST(test_value_is_true) { ASSERT_TRUE(true); } struct fixture { int value; fixture() : value(42) {} }; TEST_FIXTURE(fixture, test_with_fixture) { ASSERT_EQUAL(1337, value); } |
After installing, link against it and compile, then execute:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
brock:testing$ g++ -std=c++0x -pthread -lunittest easy.cpp -o unittest.exe brock:testing$ ./unittest.exe -v test_value_is_true::test ... [0.000116s] ok test_with_fixture::test ... [0.000186s] FAIL =============================================================================== FAIL: test_with_fixture::test [0.000186s] ------------------------------------------------------------------------------- testfailure: 1337 not equal to 42 assertion: assert_equal in easy.cpp at line 17 calledwith: 1337, value ------------------------------------------------------------------------------- Ran 2 tests in 0.000402s FAILED (failures=1) |
This example and others are covered in the tutorial provided on the website: http://libunittest.sourceforge.net/tutorial.html
I personally like a different style that libunittest supports which is a bit more writing of code but I believe it’s a nice way of organizing a test suite:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// test_template.cpp #include <libunittest/all.hpp> using namespace unittest::assertions; struct test_template : unittest::testcase<> { static void run() { UNITTEST_CLASS(test_template) UNITTEST_RUN(test1) UNITTEST_RUN(test2) } void test1() { ASSERT_EQUAL(true, true); } void test2() { ASSERT_EQUAL(false, false); } }; REGISTER(test_template) |
For learning about the other types of assertions you can do, you may want to peruse the testing code:
http://sourceforge.net/p/libunittest/code/ci/master/tree/test/test_assertions.cpp
Happy testing!
Hi Brock, I also enjoy using libunittest for my C++ projects. Have you considered using the assertion macros that libunittest provides? Aside from less typing, they provide you with the call site signature in case of an assertion failure.
Hi Christian,
I’m assuming you are referring to UNITTEST_ASSERT_EQUAL and their associated shortcuts e.g., ASSERT_EQUAL. Someone at my office recently said the same thing! After considering it, it seems appropriate to make the switch 😉 Googletest also uses assertion macros apparently e.g. http://code.google.com/p/googletest/source/browse/trunk/samples/sample1_unittest.cc But don’t worry, I’m a loyal libunittest supporter 🙂
I’ve updated the example with an assertion failure to demonstrate the call site signature. Thanks for the comment!