a little madness

A man needs a little madness, or else he never dares cut the rope and be free -Nikos Kazantzakis

Zutubi

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:

jsankey@shiny:~/repo/utpp$ ./utpp
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 <fstream>
#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:

<?xml version="1.0"?>
<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.

Liked this post? Share it!

8 Responses to “UnitTest++: Reports”

  1. September 28th, 2007 at 10:47 pm

    Chengzhi Ma says:

    Great! I have finished the same thing as yours on the base of it’s sample.

  2. May 2nd, 2009 at 9:37 pm

    newcomer says:

    sorry but RunAllTests() do not take 4 arguments at least in the current source version (2009).

    this works:

    // for a specific XML reporter ———————————-begin

    std::ofstream f(“./tests.xml”);
    UnitTest::XmlTestReporter reporter(f);

    UnitTest::TestRunner runner(reporter);

    return runner.RunTestsIf(UnitTest::Test::GetTestList(),
    NULL, UnitTest::True(),
    0);

    // for a specific XML reporter——————————-end

  3. April 2nd, 2011 at 12:27 am

    Generic Restasis says:

    newcomer says “sorry but RunAllTests() do not take 4 arguments at least in the current source version (2009)”

    Just wondering if you have tried to get it to work lately.

  4. April 11th, 2011 at 2:53 am

    Ed Ward says:

    “The default output of UnitTest++ is a simple, human-readable summary of the test results:

    jsankey@shiny:~/repo/utpp$ ./utpp
    utpp.cpp(9): error: Failure in MyTest: false
    FAILURE: 1 out of 1 tests failed (1 failures).
    Test time: 0.00 seconds”

    I agree with you on that. It’s not too complex. I don’t know as to why others don’t agree to that.

  5. July 9th, 2012 at 1:33 pm

    kambleZhang says:

    yes,my own version is 1.4 ,so i configure as the
    std::ofstream f(“./tests.xml”);
    UnitTest::XmlTestReporter reporter(f);

    UnitTest::TestRunner runner(reporter);

    return runner.RunTestsIf(UnitTest::Test::GetTestList(),
    NULL, UnitTest::True(),
    0);
    in my own cpp file .i works.i’m very happy

  6. June 15th, 2013 at 1:04 am

    Hudson, C++ and UnitTest++ – Tech Forum Network says:

    […] looking for something like Hudson’s JUnit support. UnitTest++ can create XML reports (See here). So, perhaps if someone knows how to translate these reports to be JUnit compatible, Hudson will […]

  7. July 16th, 2013 at 12:09 pm

    Geoffrey Hunter says:

    Quick question, do you know if you can easily print the time taken per test? The XML example you’ve shown seems to show it per test suite, so that’s a start.

  8. October 30th, 2013 at 8:32 am

    Hudson, C++ and UnitTest++ | Ask & Answers says:

    […] looking for something like Hudson’s JUnit support. UnitTest++ can create XML reports (See here). So, perhaps if someone knows how to translate these reports to be JUnit compatible, Hudson will […]

Leave a Reply