views:

43

answers:

1

I would like to specify the order of testing in CppUnit. According to my research, the testing order depends on either the compiler or linker and how they came across the files.

How does one specify dependencies in CppUnit?

For example, let us consider a rectangle class that has four lines. Each line contains two point classes. Assume that each class is in a separate module or translation unit.

struct Point
{
  int x;
  int y;
};

struct Line
{
  Point a;
  Point b;
};

struct Rectangle
{
  Line top;
  Line left;
  Line right;
  Line bottom;
};

In the above code, the Point class should be tested first, then the Line class and finally the Rectangle class. There is no reason to test the Rectangle class if the Line or Point classes have problems. This is a very simplified example.

For composite classes, the inner classes or member data type classes should be test first.

Let us assume that each class has an associated testing class. Each test class has its own published test methods (which are registered to the CppUnit list), in separate files. The class for testing Lines has no knowledge of the testing class for points; and similar for the rectangle. When these test case classes are compiled, their order is dependent on the compiler and linker.

So, how does one order the test cases?

FYI, I am using CppUnit, wxTestRunner and Visual Studio 2008

+1  A: 

What you're trying to do isn't really unit testing. "Pure" unit testing is intended to test individual units (individual classes), using mocks or fake objects in the place of real dependencies; once you're testing classes' dependencies on each other, that's integration testing, not unit testing.

With that disclaimer out of the way...

It looks like you might be able to use CPPUNIT_TEST_SUITE_NAMED_REGISTRATION to create multiple suites then run each suite in order, only if all previous suites have passed, but you might need to hack up or replace wxTestRunner test runner to do this.

CppUnit's page on Creating TestSuite has other options for registering test suites; CPPUNIT_REGISTRY_ADD, for example, lets you create a hierarchy of suites, which should give you some control over the ordering, but I don't see any way for a failure in one suite to abort subsequent tests.

Finally, just as a suggestion, CppUnit is probably not the best C++ unit testing framework these days. I'm personally a fan of Google Test, but Boost.Test and UnitTest++ are also good. (This answer introduces a personal project called Saru that sounds like it might give you the flexibility you want of ordering tests.)

Josh Kelley
In many cases I don't think the kind of unit testing you're talking about in the first paragraph is practical in C++. In order to do that kind of testing you really have to be able to inject mocks into the system and that becomes quite problematic with value types. In the OP's example case here I'd actually say that these "classes" have no behavior to test.
Noah Roberts
@Noah: Libraries like [Google Mock](http://code.google.com/p/googlemock/) help, but you're right, this is a case where practicality trumps purity of theory. (The OP did note that this was a very simplified example.)
Josh Kelley