views:

274

answers:

3

We're developing in C++ under Linux and about to set up automated tests. We intend to use a testing framework like CppUnit oder CxxTest. We're using Ant to build the software and we will also use it to run the tests.

As some tests are going to involve database access, we are looking for a tool or framework which facilitates the tasks of preparing and cleaning up test data in the database - just like DbUnit (a JUnit extension) in the Java world.

Another option may be to employ the actual DbUnit - a Java VM is available. Making use of DbUnit's Ant task seems to be most promising. Any related field reports are welcome!

A: 

I suppose you have your own C++ api to work with DB. If it is true, you'd better to do all your DB preparation by your own. In that case you will test your DB API as well.

Mykola Golubyev
+2  A: 

I would recommend boost unit testing. You would probably have to use the setup and teardown to manually clean up the database. Of course, you could build your own C++ DbUnit in ODBC. IF you do let me know because I could use this as well!

bnantz
Manually accessing the database within the setup/teardown methods of unit tests would also be possible using the CxxTest framework.Is there another reason why you recommend boost?
A great many C++ projects *already* use Boost in one form or another. (program_options, shared_ptr, filesystem, ...) Using Boost for testing, in that case, would mean one less dependence on external tools, which I personally always consider a win.
DevSolar
A: 

As there seems to be no DbUnit-like tool for C++ development, we've built a little framework of our own. Basically it's an adaptor for calling actual DbUnit operations from within C/C++ testrunners. It makes use of the Ant tasks provided by DbUnit.

We defined some macros like TS_DB_INSERT(filename) which call system("ant -Ddb.dataset=filename db.insert") and the like.

In this case, db.insert is an Ant target which executes a DbUnit task performing an INSERT operation on the database. The filename references an XML dataset containing the data to insert. There's also an assertion macro which wraps a DbUnit compare.

The test case might look like this:

void testDatabaseStuff
{
    TS_DB_INSERT("input.xml");

    TestedClass::doSomething();

    TS_DB_ASSERT("expected.xml");
}