I've come across cppunit but it didn't look super-easy to use (maybe I didn't look hard, maybe because C++ doesn't work like Java/C#). Are there widely used, simple alternatives?
In fact, is cppunit the standard unit testing framework for C++?
I've come across cppunit but it didn't look super-easy to use (maybe I didn't look hard, maybe because C++ doesn't work like Java/C#). Are there widely used, simple alternatives?
In fact, is cppunit the standard unit testing framework for C++?
There is no standard unit testing library for C++. There are many choices to choose from; cppunit being one of them.
At my company we use Google Test along with its partner Google Mock for unit testing and object mocking. I find them both combined easier to use and much more powerful than cppunit.
Here's a list of unit testing libraries.
http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B
However, as far as I know, cppunit is the most popular.
There is no standard unit testing framework for C++
I recoment you take a look at this blog post comparing various C++ unit testing frameworks.
Boost.Test is my favorite. It's easy to use (or as easy as a C++ unit testing library is ever going to get), is robust and it's part of Boost, which sensible C++ programmers already have installed anyway.
gtest, Google's Test Framework is an alternative.
Here is a simple example from the documentation:
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
It also plays nicely with gmock, Google's mock framework for C++
CppUnit probably is the first unit test framework for C++. It's a direct port of Junit, the famous Java framework. This makes the transition from Junit easier, but at the cost of a somewhat heavy framework, that does not take advantage of C++ capability such as RAII. That's the reason why lightweight versions such as CppUnitLite, NanoCppUnit have been created. CppUnit2 was supposed to improve this, among other improvements.
Tut used to be very light, just one header, but the latest versions introduced a library.
As far as a "standard" framework is concerned, there is none, and C++1X does not define one.
I created a testing suite called saru ( http://github.com/squishyhumans/saru ) for my own code dev. Its a BSD licensed code. I developed it as I didn't like several of the features of other testing suites. Its not widely used, but I've used it on several commercial projects spread across two companies.
So saru addresses most of these features. Its focus is on being able to run a suite of tests written in different languages. With minimal test sizes. Here's the smallest (failing) C++ test
//SARU : dummy dummy
int main() { return (1==2)?0:1; }
All saru really cares about is the return value of the binary that it compiles. It then parses the output to determine what tests failed and so on. It has headers to make working with C++ a little nicer than the above trivial example :
//SARU : dummy dummy
#include "MyStruct.h"
#include "saru_cxx.h"
class Fixture
{
MyStruct s_;
Fixture() : s_() {}
void test_A_is_B()
{
SARU_ASSERT_EQUAL( s_.A(), s_.B() );
}
void test_C_is_7()
{
SARU_ASSERT_EQUAL( 7, s_.C() );
}
};
int main()
{
saru::TestLogger logger;
SARU_TEST( Fixture:: test_A_is_B, logger );
SARU_TEST( Fixture:: test_C_is_7, logger );
logger.printSummary();
return logger.allOK()?0:1;
}
Or if you don't like the way its C++ headers work it should be able to integrate with other unittesting libraries with minimal difficulty.
But it will also run tests that are written in PHP & python. So you can set up full functional tests with saru. Or you can run something like lint over your code as part of the test suite.