views:

463

answers:

7

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++?

+9  A: 

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.

Grant Limberg
+1 Cool, I didn't know these existed.
darren
Just curious, do the google-thangs "play well" with Eclipse and Hudson?
Mawg
It is able to write output to xml files following the same schema as JUnit with the flag --gtest_output="xml:_path_to_output_file_". It works great with hudson for us on Windows, Mac, and Ubuntu. I can't speak for eclipse, though as we don't use that.
Grant Limberg
+1  A: 

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.

Larry Watanabe
+6  A: 

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.

Alexandre Jasmin
That blog post is 5 years old, it's probably a bit out of date.
ergosys
+5  A: 

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.

jalf
+3  A: 

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++

dmeister
A: 

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.

philippe
+1  A: 

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.

  1. I don't like all my tests getting compiled into one binary. My reasons for this are if the compile fails all tests fail, if one test does undefined behaviour the program output is undefined.
  2. I want control over what tests run. I want to be able to group tests and run subsets.
  3. I want compilation failure of a test to be reported as a test failure, and not halt all my tests running.
  4. I want to be able to run tests from multiple different languages
  5. I want a system flexible enough that I can run specific tests under valgrind (not yet in saru :( )

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.

Michael Anderson