Newer
Older
Jan Trávníček
committed
#include <version.hpp>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <tclap/CmdLine.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestResult.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/Test.h>
#include <cppunit/TestFailure.h>
#include <cppunit/portability/Stream.h>
#include <cppunit/TestListener.h>
#include <cppunit/SourceLine.h>
#include <cppunit/Exception.h>
CPPUNIT_NS_BEGIN
class CPPUNIT_API TestProgressListener : public TestListener
{
public:
TestProgressListener();
virtual ~TestProgressListener();
void startTest( Test *test );
void addFailure( const TestFailure &failure );
void endTest( Test *test );
int getResult() const;
void printResults() const;
private:
TestProgressListener( const TestProgressListener © );
void operator =( const TestProgressListener © );
private:
int m_Failures;
int m_Tests;
int m_Assertions;
bool m_lastTestFailed;
};
TestProgressListener::TestProgressListener() : m_Failures( 0 ), m_Tests(0), m_Assertions(0), m_lastTestFailed( false )
{
}
TestProgressListener::~TestProgressListener()
{
}
void TestProgressListener::startTest( Test * test )
{
stdCOut() << test->getName() << ":" << "\n";
stdCOut().flush();
m_lastTestFailed = false;
m_Tests++;
}
void TestProgressListener::addFailure( const TestFailure &failure )
{
stdCOut() << (failure.isError() ? "error" : "assertion") << " : " << failure.failedTestName() << " : " << failure.sourceLine().lineNumber() << "\n";
stdCOut() << "Exception " << failure.thrownException()->message().details();
m_lastTestFailed = true;
if(failure.isError()) m_Failures++; else m_Assertions++;
}
void TestProgressListener::endTest( Test * test)
{
stdCOut() << "Result (" << test->getName() << ")";
stdCOut().flush();
if ( !m_lastTestFailed )
stdCOut() << " : OK";
else
stdCOut() << " : Fail";
stdCOut() << "\n\n";
}
int TestProgressListener::getResult() const {
return m_Failures + m_Assertions;
}
void TestProgressListener::printResults() const {
stdCOut() << "Overal result: Tests: " << m_Tests << " Assertions: " << m_Assertions << " Failures: " << m_Failures << "\n";
}
CPPUNIT_NS_END
int main(int argc, char* argv[]) {
try {
Tomáš Pecka
committed
TCLAP::CmdLine cmd("Main test binary.", ' ', ALIB_VERSION_INFO);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
TCLAP::MultiArg<std::string> testPathSegments("p", "path", "test path", false, "string" );
cmd.add( testPathSegments );
cmd.parse(argc, argv);
CppUnit::TestResult controller;
CppUnit::TestResultCollector result;
controller.addListener( &result );
CppUnit::TestProgressListener progressListener;
controller.addListener( &progressListener );
CppUnit::Test *suite = NULL;
std::string testPath = "";
if(testPathSegments.getValue().size() == 0) {
// Get the top level suite from the registry
suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
} else if(testPathSegments.getValue().size() == 1) {
suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest();
} else {
suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest();
bool first = true;
for(const std::string& path : testPathSegments.getValue()) {
if(first) {
first = false;
continue;
}
testPath += path + "/";
}
testPath.pop_back();
}
// Adds the test to the list of test to run
CppUnit::TextUi::TestRunner runner;
runner.addTest( suite );
// Change the default outputter to a compiler error format outputter
runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) );
// Run the tests.
runner.run( controller, testPath );
progressListener.printResults();
std::ofstream xmlFileResults("CppUnitTestResults.xml");
CppUnit::XmlOutputter xmlOut(&result, xmlFileResults);
xmlOut.write();
return progressListener.getResult();
/* } catch(const exception::AlibException& exception) {
std::cerr << exception.getCause() << std::endl;
return 1;*/
} catch(const TCLAP::ArgException& exception) {
std::cerr << exception.error() << std::endl;
return 2;
} catch (const std::exception& exception) {
std::cerr << "Exception caught: " << exception.what() << std::endl;
return 3;
} catch(...) {
std::cerr << "Unknown exception caught." << std::endl;
return 127;
}
}