Upgrade UnitTest and provide JUnit XML output formatting

This commit is contained in:
Vinnie Falco
2013-07-28 02:38:19 -07:00
parent 9064af3b1c
commit abd3668b65
17 changed files with 603 additions and 320 deletions

View File

@@ -17,6 +17,149 @@
*/
//==============================================================================
namespace UnitTestUtilities
{
JUnitXMLFormatter::JUnitXMLFormatter (UnitTests const& tests)
: m_tests (tests)
, m_currentTime (timeToString (Time::getCurrentTime ()))
, m_hostName (SystemStats::getComputerName ())
{
}
// This is the closest thing to documentation on JUnit XML I could find:
//
// http://junitpdfreport.sourceforge.net/managedcontent/PdfTranslation
//
String JUnitXMLFormatter::createDocumentString ()
{
UnitTests::Results const& results (m_tests.getResults ());
ScopedPointer <XmlElement> testsuites (new XmlElement ("testsuites"));
testsuites->setAttribute ("tests", String (results.tests));
if (results.failures != 0)
testsuites->setAttribute ("failures", String (results.failures));
testsuites->setAttribute ("time", secondsToString (results.secondsElapsed));
for (int i = 0; i < results.suites.size (); ++i)
{
UnitTest::Suite const& suite (*results.suites [i]);
XmlElement* testsuite (new XmlElement ("testsuite"));;
testsuite->setAttribute ("name", suite.className);
testsuite->setAttribute ("tests", String (suite.tests));
if (suite.failures != 0)
testsuite->setAttribute ("failures", String (suite.failures));
testsuite->setAttribute ("time", secondsToString (suite.secondsElapsed));
testsuite->setAttribute ("timestamp", timeToString (suite.whenStarted));
testsuite->setAttribute ("hostname", m_hostName);
testsuite->setAttribute ("package", suite.packageName);
testsuites->addChildElement (testsuite);
for (int i = 0; i < suite.cases.size (); ++i)
{
UnitTest::Case const& testCase (*suite.cases [i]);
XmlElement* testcase (new XmlElement ("testcase"));
testcase->setAttribute ("name", testCase.name);
testcase->setAttribute ("time", secondsToString (testCase.secondsElapsed));
testcase->setAttribute ("classname", suite.className);
testsuite->addChildElement (testcase);
for (int i = 0; i < testCase.items.size (); ++i)
{
UnitTest::Item const& item (testCase.items.getUnchecked (i));
if (!item.passed)
{
XmlElement* failure (new XmlElement ("failure"));
String s;
s << "#" << String (i+1) << " " << item.failureMessage;
failure->setAttribute ("message", s);
testcase->addChildElement (failure);
}
}
}
}
return testsuites->createDocument (
//"https://svn.jenkins-ci.org/trunk/hudson/dtkit/dtkit-format/dtkit-junit-model/src/main/resources/com/thalesgroup/dtkit/junit/model/xsd/junit-4.xsd",
"",
false,
true,
"UTF-8",
999);
};
String JUnitXMLFormatter::timeToString (Time const& time)
{
return time.toString (true, true, false, true);
}
String JUnitXMLFormatter::secondsToString (double seconds)
{
if (seconds < .01)
return String (seconds, 4);
else if (seconds < 1)
return String (seconds, 2);
else if (seconds < 10)
return String (seconds, 1);
else
return String (int (seconds));
}
//------------------------------------------------------------------------------
/** A unit test that always passes.
This can be useful to diagnose continuous integration systems.
*/
class PassUnitTest : public UnitTest
{
public:
PassUnitTest () : UnitTest ("Pass", "beast", runManual)
{
}
void runTest ()
{
beginTestCase ("pass");
pass ();
}
};
static PassUnitTest passUnitTest;
//------------------------------------------------------------------------------
/** A unit test that always fails.
This can be useful to diagnose continuous integration systems.
*/
class FailUnitTest : public UnitTest
{
public:
FailUnitTest () : UnitTest ("Fail", "beast", runManual)
{
}
void runTest ()
{
beginTestCase ("pass");
fail ("Intentional failure");
}
};
static FailUnitTest failUnitTest;
}
//------------------------------------------------------------------------------
class UnitTestUtilitiesTests : public UnitTest
{
public:
@@ -33,7 +176,7 @@ public:
int const numberOfItems = 100;
int64 const seedValue = 50;
beginTest ("Payload");
beginTestCase ("Payload");
Payload p1 (maxBufferSize);
Payload p2 (maxBufferSize);