2 #ifndef TUT_CPPUNIT_REPORTER
\r
3 #define TUT_CPPUNIT_REPORTER
\r
5 #include <tut/tut.hpp>
\r
15 * CppUnit TUT reporter
\r
17 class cppunit_reporter : public tut::callback
\r
20 std::vector<tut::test_result> failed_tests;
\r
21 std::vector<tut::test_result> passed_tests;
\r
22 std::string filename;
\r
24 std::string encode(const std::string & text)
\r
28 for (unsigned int i=0; i<text.length(); ++i) {
\r
56 cppunit_reporter(const std::string & _filename = "")
\r
58 setFilename(_filename);
\r
61 void setFilename(const std::string & _filename)
\r
63 if (_filename == "")
\r
65 filename = "testResult.xml";
\r
69 filename = _filename;
\r
75 failed_tests.clear();
\r
76 passed_tests.clear();
\r
79 void test_completed(const tut::test_result& tr)
\r
81 if (tr.result == test_result::ok) {
\r
82 passed_tests.push_back(tr);
\r
84 failed_tests.push_back(tr);
\r
88 void run_completed()
\r
92 std::string failure_type;
\r
93 std::string failure_msg;
\r
94 std::ofstream xmlfile;
\r
96 xmlfile.open(filename.c_str(), std::ios::in | std::ios::trunc);
\r
97 if (!xmlfile.is_open()) {
\r
98 throw (std::runtime_error("Cannot open file for output"));
\r
101 /* *********************** header ***************************** */
\r
102 xmlfile << "<?xml version=\"1.0\" encoding='ISO-8859-1' standalone='yes' ?>" << std::endl
\r
103 << "<TestRun>" << std::endl;
\r
105 /* *********************** failed tests ***************************** */
\r
106 if (failed_tests.size() > 0) {
\r
107 xmlfile << " <FailedTests>" << std::endl;
\r
109 for (unsigned int i=0; i<failed_tests.size(); i++) {
\r
110 switch (failed_tests[i].result) {
\r
111 case test_result::fail:
\r
112 failure_type = "Assertion";
\r
116 case test_result::ex:
\r
117 failure_type = "Assertion";
\r
118 failure_msg = "Thrown exception: " + failed_tests[i].exception_typeid + '\n';
\r
121 case test_result::warn:
\r
122 failure_type = "Assertion";
\r
123 failure_msg = "Destructor failed.\n";
\r
126 case test_result::term:
\r
127 failure_type = "Error";
\r
128 failure_msg = "Test application terminated abnormally.\n";
\r
131 case test_result::ex_ctor:
\r
132 failure_type = "Error";
\r
133 failure_msg = "Constructor has thrown an exception: " + failed_tests[i].exception_typeid + '\n';
\r
136 case test_result::rethrown:
\r
137 failure_type = "Assertion";
\r
138 failure_msg = "Child failed";
\r
142 failure_type = "Error";
\r
143 failure_msg = "Unknown test status, this should have never happened. "
\r
144 "You may just have found a BUG in TUT CppUnit reporter, please report it immediately.\n";
\r
149 xmlfile << " <FailedTest id=\"" << failed_tests[i].test << "\">" << std::endl
\r
150 << " <Name>" << encode(failed_tests[i].group) + "::" + encode(failed_tests[i].name) << "</Name>" << std::endl
\r
151 << " <FailureType>" << failure_type << "</FailureType>" << std::endl
\r
152 << " <Location>" << std::endl
\r
153 << " <File>Unknown</File>" << std::endl
\r
154 << " <Line>Unknown</Line>" << std::endl
\r
155 << " </Location>" << std::endl
\r
156 << " <Message>" << encode(failure_msg + failed_tests[i].message) << "</Message>" << std::endl
\r
157 << " </FailedTest>" << std::endl;
\r
160 xmlfile << " </FailedTests>" << std::endl;
\r
163 /* *********************** passed tests ***************************** */
\r
164 if (passed_tests.size() > 0) {
\r
165 xmlfile << " <SuccessfulTests>" << std::endl;
\r
167 for (unsigned int i=0; i<passed_tests.size(); i++) {
\r
168 xmlfile << " <Test id=\"" << passed_tests[i].test << "\">" << std::endl
\r
169 << " <Name>" << encode(passed_tests[i].group) + "::" + encode(passed_tests[i].name) << "</Name>" << std::endl
\r
170 << " </Test>" << std::endl;
\r
173 xmlfile << " </SuccessfulTests>" << std::endl;
\r
176 /* *********************** statistics ***************************** */
\r
177 xmlfile << " <Statistics>" << std::endl
\r
178 << " <Tests>" << (failed_tests.size() + passed_tests.size()) << "</Tests>" << std::endl
\r
179 << " <FailuresTotal>" << failed_tests.size() << "</FailuresTotal>" << std::endl
\r
180 << " <Errors>" << errors << "</Errors>" << std::endl
\r
181 << " <Failures>" << failures << "</Failures>" << std::endl
\r
182 << " </Statistics>" << std::endl;
\r
184 /* *********************** footer ***************************** */
\r
185 xmlfile << "</TestRun>" << std::endl;
\r
190 bool all_ok() const
\r
192 return failed_tests.empty();
\r