UnitTest++: Reports
In my previous post, UnitTest++: The New Choice for C++ Unit Testing?, I gave a basic introduction to UnitTest++. Moving beyond the basics, you will often want to integrate UnitTest++ into your development process. A key to this is being able to generate test reports in a format amenable to integration. The default output of UnitTest++ is a simple, human-readable summary of the test results:
utpp.cpp(9): error: Failure in MyTest: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds
This is fine for the developer, but not so easy to process in code for integration purposes. Luckily, the reporting mechanism is not fixed. The test runner accepts an instance of type TestReporter to use for reporting results. The default reporter, TestReporterStdout, produces the developer-friendly output shown above. A second reporter is also included in the UnitTest++ distribution: XmlTestReporter. As its name suggests, this reporter outputs results in XML format, which is much easier to digest in code.
Using the XmlTestReporter is easy. Just construct one with an output stream and pass it to the test runner when executing the tests:
#include "UnitTest++.h"
#include "XmlTestReporter.h"
int main(int, char const *[])
{
std::ofstream f(“tests.xml”);
UnitTest::XmlTestReporter reporter(f);
return UnitTest::RunAllTests(reporter,
UnitTest::Test::GetTestList(),
NULL,
0);
}
The results in this case are saved to a file named “tests.xml”. An example XML report is shown below:
<unittest-results tests=“3″ failedtests=“1″
failures=“1″ time=“0″>
<test suite=“SuiteOne” name=“TestOne” time=“0″/>
<test suite=“SuiteOne” name=“TestTwo” time=“0″>
<failure message=“utpp.cpp(14) : false”/>
</test>
<test suite=“SuiteTwo” name=“TestOne” time=“0″/>
</unittest-results>
The report is easily interpreted, and can be easily parsed for integration in to other systems. A prime use case would be integration with a continuous integration server, such as Pulse, which is why I am looking into the reports myself
.
Finally, if the XML format is not suitable for your purposes, you can also create your own test reporters. The interface is compact and easily implemented.






