fix test result reporting

This commit is contained in:
Günter Obiltschnig
2020-01-04 12:34:28 +01:00
parent 50e9d35c3c
commit 09919478ce
3 changed files with 59 additions and 49 deletions

View File

@@ -35,7 +35,7 @@ TestRunner::~TestRunner()
void TestRunner::printBanner()
{
_ostr
_ostr
<< "Usage: driver [-all] [-ignore <file> ] [-long] [-print] [-wait] [name] ..." << std::endl
<< " where name is the name of a test case class" << std::endl;
}
@@ -55,10 +55,10 @@ bool TestRunner::run(const std::vector<std::string>& args)
std::vector<std::string> setup;
std::vector<Test*> tests;
for (int i = 1; i < args.size(); i++)
for (int i = 1; i < args.size(); i++)
{
const std::string& arg = args[i];
if (arg == "-wait")
if (arg == "-wait")
{
wait = true;
continue;
@@ -80,7 +80,7 @@ bool TestRunner::run(const std::vector<std::string>& args)
}
else if (arg == "-print")
{
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
{
print(it->first, it->second, 0);
}
@@ -105,7 +105,7 @@ bool TestRunner::run(const std::vector<std::string>& args)
}
Test* testToRun = 0;
for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it)
for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it)
{
testToRun = find(testCase, it->second, it->first);
}
@@ -114,23 +114,23 @@ bool TestRunner::run(const std::vector<std::string>& args)
collectAllTestCases(testToRun, tests);
}
if (!testToRun)
if (!testToRun)
{
_ostr << "Test " << testCase << " not found." << std::endl;
return false;
}
}
}
std::cout << "ignore file:" << ignore << std::endl;
if (all)
{
tests.clear();
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
{
collectAllTestCases(it->second, tests);
}
}
TextTestResult result(_ostr, ignore);
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
{
Test* testToRun = *it;
@@ -138,17 +138,20 @@ bool TestRunner::run(const std::vector<std::string>& args)
continue;
if (setup.size() > 0)
testToRun->addSetup(setup);
if (!run(testToRun, ignore)) success = false;
testToRun->run(&result);
numberOfTests++;
}
_ostr << result << std::endl;
success = result.wasSuccessful();
if (numberOfTests == 0 && !printed)
if (numberOfTests == 0 && !printed)
{
printBanner();
return false;
}
if (wait)
if (wait)
{
_ostr << "<RETURN> to continue" << std::endl;
std::cin.get();
@@ -158,17 +161,6 @@ bool TestRunner::run(const std::vector<std::string>& args)
}
bool TestRunner::run(Test* test, const std::string& ignore)
{
TextTestResult result(_ostr, ignore);
test->run(&result);
_ostr << result << std::endl;
return result.wasSuccessful();
}
void TestRunner::addTest(const std::string& name, Test* test)
{
_mappings.push_back(Mapping(name, test));
@@ -221,7 +213,7 @@ int TestRunner::collectAllTestCases(Test* pTest, std::vector<Test*>& testcases)
if (pTest->getType() == Test::Suite)
{
TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
if (pSuite)
{
const std::vector<Test*>& tests = pSuite->tests();

View File

@@ -16,21 +16,25 @@
#include <exception>
namespace CppUnit {
TextTestResult::TextTestResult() :
_ostr(std::cout)
{
}
TextTestResult::TextTestResult() :
_ostr(std::cout)
{
}
TextTestResult::TextTestResult(const std::string& ignore):
_ostr(std::cout)
{
if (!ignore.empty())
{
try {
try
{
std::ifstream ifs(ignore);
if (ifs.is_open()) {
if (ifs.is_open())
{
char line[256];
while (ifs.getline(line, sizeof(line)))
{
@@ -44,7 +48,8 @@ TextTestResult::TextTestResult(const std::string& ignore):
ifs.close();
}
}
catch (std::exception e) {
catch (std::exception e)
{
std::cout << e.what() << std::endl;
}
@@ -57,13 +62,16 @@ TextTestResult::TextTestResult(std::ostream& ostr):
_ostr(ostr)
{
}
TextTestResult::TextTestResult(std::ostream& ostr, const std::string& ignore) :
_ostr(ostr)
{
if (!ignore.empty())
{
std::ifstream ifs(ignore);
if (ifs.is_open()) {
if (ifs.is_open())
{
char line[256];
while (ifs.getline(line, sizeof(line)))
{
@@ -80,6 +88,7 @@ TextTestResult::TextTestResult(std::ostream& ostr, const std::string& ignore) :
setup();
}
void TextTestResult::ignoring(const std::string ignore)
{
std::string::const_iterator it = ignore.begin();
@@ -95,6 +104,8 @@ void TextTestResult::ignoring(const std::string ignore)
if (!test.empty()) _ignored.insert(test.erase(test.find_last_not_of(" \n\r\t") + 1));
}
}
void TextTestResult::setup()
{
#if !defined(_WIN32_WCE)
@@ -142,15 +153,15 @@ void TextTestResult::addFailure(Test* test, CppUnitException* e)
void TextTestResult::startTest(Test* test)
{
TestResult::startTest(test);
_ostr << shortName(test->toString()) << ": ";
_ostr << "\n" << shortName(test->toString()) << ": ";
}
void TextTestResult::printErrors(std::ostream& stream)
{
if (testErrors() != 0)
if (testErrors() != 0)
{
stream << std::endl;
stream << "\n";
if (testErrors() == 1)
stream << "There was " << testErrors() << " error: " << std::endl;
@@ -165,9 +176,9 @@ void TextTestResult::printErrors(std::ostream& stream)
stream << std::setw(2) << i
<< ": "
<< failure->failedTest()->toString() << std::endl
<< " \"" << (e ? e->what() : "") << "\"" << std::endl
<< " in \""
<< failure->failedTest()->toString() << "\n"
<< " \"" << (e ? e->what() : "") << "\"\n"
<< " in \""
<< (e ? e->fileName() : std::string())
<< "\", line ";
if (e == 0)
@@ -213,9 +224,9 @@ void TextTestResult::printFailures(std::ostream& stream)
stream << std::setw(2) << i
<< ": "
<< failure->failedTest()->toString() << std::endl
<< " \"" << (e ? e->what() : "") << "\"" << std::endl
<< " in \""
<< failure->failedTest()->toString() << "\n"
<< " \"" << (e ? e->what() : "") << "\"\n"
<< " in \""
<< (e ? e->fileName() : std::string())
<< "\", line ";
if (e == 0)
@@ -227,7 +238,7 @@ void TextTestResult::printFailures(std::ostream& stream)
stream << e->lineNumber();
if (e->data2LineNumber() != CppUnitException::CPPUNIT_UNKNOWNLINENUMBER)
{
stream << " data lines "
stream << " data lines "
<< e->data1LineNumber()
<< ", " << e->data2LineNumber();
}
@@ -253,11 +264,19 @@ void TextTestResult::print(std::ostream& stream)
void TextTestResult::printHeader(std::ostream& stream)
{
if (!wasSuccessful())
stream << "!!!FAILURES!!!" << std::endl
<< "Runs: " << runTests()
<< " Failures: " << testFailures()
<< " Errors: " << testErrors()
stream << "\n\n";
if (wasSuccessful())
stream << "OK ("
<< runTests() << " tests)"
<< std::endl;
else
stream << "!!!FAILURES!!!" << "\n"
<< "Runs: "
<< runTests ()
<< " Failures: "
<< testFailures ()
<< " Errors: "
<< testErrors ()
<< std::endl;
}