1 #ifndef TUT_XML_REPORTER
2 #define TUT_XML_REPORTER
3 #include <tut/tut_config.hpp>
5 #include <tut/tut_cppunit_reporter.hpp>
22 typedef std::vector<tut::test_result> TestResults;
23 typedef std::map<std::string, TestResults> TestGroups;
25 TestGroups all_tests_;
26 const std::string filename_;
27 std::auto_ptr<std::ostream> stream_;
37 std::string xml_build_testcase(
const tut::test_result & tr,
const std::string & failure_type,
38 const std::string & failure_msg,
int pid = 0)
43 std::ostringstream out;
46 (tr.result == test_result::skipped) )
56 tr.result ==
test_result::ex || tr.result == test_result::ex_ctor || tr.result == test_result::rethrown )
66 out <<
" <" << tag <<
" message=\"" << err_msg <<
"\"" <<
" type=\"" << failure_type <<
"\"";
67 #if defined(TUT_USE_POSIX)
70 out <<
" child=\"" << pid <<
"\"";
75 out <<
">" << err_msg <<
"</" << tag <<
">" << endl;
76 out <<
" </testcase>";
92 std::string xml_build_testsuite(
int errors,
int failures,
int total,
93 const std::string & name,
const std::string & testcases)
95 std::ostringstream out;
97 out <<
" <testsuite errors=\"" << errors <<
"\" failures=\"" << failures <<
"\" tests=\"" << total <<
"\" name=\"" <<
cppunit_reporter::encode(name) <<
"\">" << std::endl;
99 out <<
" </testsuite>";
118 stream_(new std::ofstream(filename_.c_str())),
125 if (!stream_->good()) {
126 throw tut_error(
"Cannot open output file `" + filename_ +
"`");
144 if(filename_.empty())
173 case test_result::skipped:
177 case test_result::rethrown:
181 case test_result::ex_ctor:
190 case tut::test_result::dummy:
191 assert(!
"Should never be called");
195 all_tests_[tr.
group].push_back(tr);
206 *stream_ <<
"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" << std::endl;
207 *stream_ <<
"<testsuites>" << std::endl;
210 for (TestGroups::const_iterator tgi = all_tests_.begin(); tgi != all_tests_.end(); ++tgi)
216 int terminations = 0;
223 std::ostringstream out;
226 const TestResults &results = tgi->second;
227 for (TestResults::const_iterator tri = results.begin(); tri != results.end(); ++tri)
229 std::string failure_type;
230 std::string failure_msg;
235 case test_result::skipped:
239 failure_type =
"Assertion";
244 failure_type =
"Assertion";
245 failure_msg =
"Thrown exception: " + tri->exception_typeid +
'\n';
249 failure_type =
"Assertion";
250 failure_msg =
"Destructor failed.\n";
254 failure_type =
"Error";
255 failure_msg =
"Test application terminated abnormally.\n";
258 case test_result::ex_ctor:
259 failure_type =
"Assertion";
260 failure_msg =
"Constructor has thrown an exception: " + tri->exception_typeid +
".\n";
263 case test_result::rethrown:
264 failure_type =
"Assertion";
265 failure_msg =
"Child failed.\n";
269 failure_type =
"Error";
270 failure_msg =
"Unknown test status, this should have never happened. "
271 "You may just have found a bug in TUT, please report it immediately.\n";
276 #if defined(TUT_USE_POSIX)
277 out << xml_build_testcase(*tri, failure_type, failure_msg, tri->pid) << std::endl;
279 out << xml_build_testcase(*tri, failure_type, failure_msg) << std::endl;
284 int stat_errors = terminations + errors;
285 int stat_failures = failures + warnings + exceptions;
286 int stat_all = stat_errors + stat_failures + passed;
288 *stream_ << xml_build_testsuite(stat_errors, stat_failures, stat_all, (*tgi).first, out.str()) << std::endl;
291 *stream_ <<
"</testsuites>" << std::endl;
Definition: tut_exception.hpp:13
std::string group
Definition: tut_result.hpp:74
int warnings_count
number of tests that would terminate
Definition: tut_xml_reporter.hpp:109
int terminations_count
number of tests that failed
Definition: tut_xml_reporter.hpp:108
test finished successfully
Definition: tut_result.hpp:91
std::string name
Definition: tut_result.hpp:84
virtual bool all_ok() const
Returns true, if all tests passed.
Definition: tut_xml_reporter.hpp:297
JUnit XML TUT reporter.
Definition: tut_xml_reporter.hpp:20
test finished successfully, but test destructor throwed
Definition: tut_result.hpp:94
virtual void test_completed(const tut::test_result &tr)
Callback function This function is called when test completes. Counters are updated here...
Definition: tut_xml_reporter.hpp:168
virtual void run_completed()
Callback function This function is called when all tests are completed. It generates XML output to fi...
Definition: tut_xml_reporter.hpp:203
int failures_count
number of tests that threw exceptions
Definition: tut_xml_reporter.hpp:107
test failed with ensure() or fail() methods
Definition: tut_result.hpp:92
xml_reporter(const std::string &filename)
number of tests where destructors threw an exception
Definition: tut_xml_reporter.hpp:115
std::string message
Definition: tut_result.hpp:107
Definition: tut_result.hpp:69
test forced test application to terminate abnormally
Definition: tut_result.hpp:95
test throwed an exceptions
Definition: tut_result.hpp:93
static std::string encode(const std::string &text)
Encodes text to XML XML-reserved characters (e.g. "<") are encoded according to specification.
Definition: tut_cppunit_reporter.hpp:184
int exceptions_count
number of passed tests
Definition: tut_xml_reporter.hpp:106
Definition: tut_runner.hpp:38
virtual void run_started()
Callback function This function is called before the first test is executed. It initializes counters...
Definition: tut_xml_reporter.hpp:154