Wednesday, May 26, 2010

JUnit reporter for Unitest++

The C++ testing framework Unittest++ includes a XML reporter but it is not directly usable with most continuous integration (CI) servers such as Hudson and TeamCity. I wrote a XML reporter for Unittest++ that uses the same format as JUnit, the reporter will split test suites into different files and they can be directly used by for example the junitreport task in ant or in your CI build. Download the header and cpp files below and add them to your Unittest++ build.

This is how you normally use Unittest++'s built-in XML reporter:
std::ofstream f("test-report.xml");
UnitTest::XmlTestReporter reporter(f);
UnitTest::TestRunner runner(reporter);
return runner.RunTestsIf(UnitTest::Test::GetTestList(), NULL, UnitTest::True(), 0);
Simply replace it with the following,
std::string f = "TESTS-";
UnitTest::JUnitXmlTestReporter reporter(f);
UnitTest::TestRunner runner(reporter);
return runner.RunTestsIf(UnitTest::Test::GetTestList(), NULL, UnitTest::True(), 0);
Note that the JUnit reporter does not take a complete filename because the JUnit format requires that different test suites are split into different files. The output files are named TESTS-suitename.xml.

Download
JUnitXMLTestReporter.h
JUnitXMLTestReporter.cpp