views:

439

answers:

11

I'm going to write a quite large application for school project in C++. So far I'm quite used to TDD in Java and Ruby using JUnit and RSpec.

But as far as my experience goes, I've never seen any C/C++ project with any test suite.

What library do you recommend for testing in C++?
Are there any good mocking/stubbing frameworks for C++?

Actually I'm using NetBeans IDE and there seems to be no support for testing whatsoever.
What tool would you recommend for TDD in C++?

Is TDD even possible with C++? The compile time seems to me like a big drawback.

+1  A: 

Have you tried CPPTest?

Asaph
A: 

cxxtest


+4  A: 

There's plenty of unit test frameworks for C++. The 2 I have the most experience with are CppUnit and CxxTest. I prefer CxxTest as I find it easier not to have to register my test cases explicitly as you have to do with CppUnit. there's an eclipse plugin for CxxTest but I don't know if there's one for NetBeans. Compile time shouldn't really be an issue with well written C++ and definately isn't a reason to skip unit tests / tdd.

For acceptance testing I've used exactor. This is a java based tool, but it's not difficult to use in a C++ environment.

Glen
+5  A: 

The Boost Test Library gives you unit tests etc.

Georg Fritzsche
+2  A: 

We use Goggle Test Framework. And we don't run tests on every compile.

Sergius
+1  A: 

For Integrated Testing, might I suggest the C++ implementation of FIT (Framework for Integrated Test) - http://fit.c2.com aka CEEFIT (http://ceefit.woldrich.com/?page=Home) (at the time of writing this article the ceefit site was down. hopefully it will come back up soon). I have personally used CEEFIT to run Integrated Tests on legacy C++ code-bases that interact with Computer-Aided Design platform API (SolidWorks if you are aware of it). I am fortunate that CEEFIT is open source, because I had to extend it to do custom things, like read multiple tables as input (default behavior is to read a single table for a test-class). After having worked with CEEFIT for at least a year now, I am relatively confident I can run Integrated Tests on most systems with this tool.

Shameless plug - some of my blog posts recounting my experience with CEEFIT with downloads http://ossandcad.blogspot.com/2009/07/swx-batch-mode-integrated-tests-with.html http://ossandcad.blogspot.com/2009/02/writing-ceefit-class-like-regular-c.html

There are a few caveats with CEEFIT though - providing input through a table format is not always feasible (not CEEFIT's fault, thats by design of FIT, which CEEFIT simply implements). The source code has not been updated for many years, if memory serves right (since site is down), since 2005 (but since its open source, this has not caused many problems for me in my work).

ossandcad
A: 

Shameless plug: Have a look at cfix and Visual Assert.

Johannes Passing
A: 

If you used to use JUnit and jMock, I would like to recommend you :

Googlemock and seamless coorperate with Googletest.

For TDD, my self use

with some scripts composed by myself. And all of these work well.

yoco
google test works well but it breaks the intellisense in the netbeans IDE
Jay
A: 

unittest++

Patrick
A: 

As you have worked on JUnit , It would be easy for you to work on CPPUnit

sat
A: 

Comparing CppTest and CppUnit I would go with CppTest. CppTest has less hidden framework and IMO easier to understand and implement. I personally like to see the main entry point. I have also included Boost Unit Testing Framework. It is not xUnit based. I am not a fan, but if you are already using the Boost Library it would be nice to incorporate.


CppTest vs CppUnit

Ease of creating a unit test and test suite. Both CppUnit and CppTest create unit tests of class methods, with the class itself derived from some tool-provided Test class. The syntax for CppTest is slightly simpler, though, with the test registration happening inside the class constructor. In the case of CppUnit, the additional macros CPPUNIT_TEST_SUITE and CPPUNIT_TEST_SUITE_ENDS are needed.

Running the tests. CppTest simply calls the run method on the test suite, while CppUnit uses a separate TestRunner class whose run method is invoked for running the tests.

Extending the testing hierarchy. In the case of CppTest, it is always possible to extend the previous test suite by creating a new class that inherits from the old one. The new class will define some additional functions that add to the unit test pool. You simply invoke the run method on the object of the new class type. CppUnit, in contrast, requires that you use the macro CPPUNIT_TEST_SUB_SUITE along with class inheritance to achieve the same effect.

Generating formatted output. Both CppTest and CppUnit have the ability to customize the output. However, although CppTest has a useful, predefined HTML output formatter, CppUnit does not. However, CppUnit exclusively supports XML formatting. Both support text and compiler style formats.

Creating test fixtures. To use test fixtures, CppUnit requires that the test class be derived from CppUnit::TestFixture. You must provide definitions for the setup and tear-down routines. In the case of CppTest, you need to provide definitions only for the setup and tear-down routines. This is definitely a better solution, as it keeps the client code simple. •Predefined utility macro support. Both CppTest and CppUnit have a comparable set of macros for asserts, handling floats, and so on.

Header files. CppTest requires that you include a single header file, while CppUnit client code must include multiple headers, like HelperMacros.h and TextTestRunner.h, depending on the features used.

http://www.ibm.com/developerworks/aix/library/au-ctools3_ccptest/index.html?ca=drs-


CPPTEST

#include “cppTest.h”

class myTestWithFixtures : public Test::Suite { 
  void function1_to_test_some_code( );
  void function2_to_test_some_code( );

  public: 
  myTestWithFixtures ( ) { 
      TEST_ADD (function1_to_test_some_code) {...}; 
      TEST_ADD (function2_to_test_some_code) {...}; 
  } 

  protected: 
    virtual void setup( ) { ... };
    virtual void tear_down( ) { ... };
}; 

int main ( ) 
{ 
  myTestWithFixtures tests; 
  Test::TextOutput output(Test::TextOutput::Verbose);
  return tests.run(output);
} 

http://www.ibm.com/developerworks/aix/library/au-ctools3_ccptest/index.html?ca=drs-


CPPUNIT

#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>

class mystringTest : public CppUnit::TestFixture {
public:
  void setUp() { ... };
  void tearDown() { ... };

  void function1_to_test_some_code() { ... };
  void function2_to_test_some_code() { ... };

  CPPUNIT_TEST_SUITE( mystringTest );
  CPPUNIT_TEST( function1_to_test_some_code );
  CPPUNIT_TEST( function2_to_test_some_code );
  CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION( mystringTest );

with out macros

int main ()
{
  CppUnit::TestSuite* suite = new CppUnit::TestSuite("mystringTest");
  suite->addTest(new CppUnit::TestCaller<mystringTest>("checkLength",
                &mystringTest::checkLength));
  suite->addTest(new CppUnit::TestCaller<mystringTest>("checkValue",
                &mystringTest::checkLength));

  // client code follows next 
  CppUnit::TextTestRunner runner;
  runner.addTest(suite);

  runner.run();
  return 0;
}

http://www.ibm.com/developerworks/aix/library/au-ctools2_cppunit/


Boost Unit Testing Framework

#include <boost/test/unit_test.hpp>

using namespace std;

struct CMyFooTestFixture
{
    CMyFooTestFixture() { ... } //SetUp
    ~CMyFooTestFixture() { ... } //TearDown

    void function1_to_test_some_code(CMyFoo& foo) { ... };
    void function2_to_test_some_code(CMyFoo& foo) { ... };
}

BOOST_FIXTURE_TEST_SUITE(MyFooTest, CMyFooTestFixture);

BOOST_AUTO_TEST_CASE(function1_to_test_some_code)
{
    CMyFoo foo;
    function1_to_test_some_code(foo);
}

BOOST_AUTO_TEST_CASE(function1_to_test_some_code2)
{
    CMyFoo foo;
    function1_to_test_some_code(foo);
}

BOOST_AUTO_TEST_SUITE_END();

http://www.beroux.com/english/articles/boost_unit_testing/

Shiftbit