tut logo

tut_cppunit_reporter.hpp Source File


tut_cppunit_reporter.hpp
1 
2 #ifndef TUT_CPPUNIT_REPORTER
3 #define TUT_CPPUNIT_REPORTER
4 
5 #include <tut/tut.hpp>
6 #include <string>
7 #include <fstream>
8 #include <vector>
9 #include <stdexcept>
10 #include <memory>
11 
12 namespace tut
13 {
14 
19 {
20  std::vector<tut::test_result> failed_tests_;
21  std::vector<tut::test_result> passed_tests_;
22  const std::string filename_;
23  std::auto_ptr<std::ostream> stream_;
24 
25 
27  cppunit_reporter &operator=(const cppunit_reporter &);
28 
29 public:
30  explicit cppunit_reporter(const std::string &filename = "testResult.xml")
31  : failed_tests_(),
32  passed_tests_(),
33  filename_(filename),
34  stream_(new std::ofstream(filename_.c_str()))
35  {
36  if (!stream_->good()) {
37  throw tut_error("Cannot open output file `" + filename_ + "`");
38  }
39  }
40 
41  explicit cppunit_reporter(std::ostream &stream)
42  : failed_tests_(),
43  passed_tests_(),
44  filename_(),
45  stream_(&stream)
46  {
47  }
48 
50  {
51  if(filename_.empty())
52  {
53  stream_.release();
54  }
55  }
56 
57  void run_started()
58  {
59  failed_tests_.clear();
60  passed_tests_.clear();
61  }
62 
64  {
65  assert(tr.result != test_result::dummy );
66  if ( (tr.result == test_result::ok) ||
67  (tr.result == test_result::skipped) )
68  {
69  passed_tests_.push_back(tr);
70  }
71  else
72  {
73  failed_tests_.push_back(tr);
74  }
75  }
76 
78  {
79  int errors = 0;
80  int failures = 0;
81  std::string failure_type;
82  std::string failure_msg;
83 
84  *stream_ << "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" << std::endl
85  << "<TestRun>" << std::endl;
86 
87  if (failed_tests_.size() > 0)
88  {
89  *stream_ << " <FailedTests>" << std::endl;
90 
91  for (unsigned int i=0; i<failed_tests_.size(); i++)
92  {
93  switch (failed_tests_[i].result)
94  {
95  case test_result::fail:
96  failure_type = "Assertion";
97  failure_msg = "";
98  failures++;
99  break;
100  case test_result::ex:
101  failure_type = "Assertion";
102  failure_msg = "Thrown exception: " + failed_tests_[i].exception_typeid + '\n';
103  failures++;
104  break;
105  case test_result::warn:
106  failure_type = "Assertion";
107  failure_msg = "Destructor failed\n";
108  failures++;
109  break;
110  case test_result::term:
111  failure_type = "Error";
112  failure_msg = "Test application terminated abnormally\n";
113  errors++;
114  break;
115  case test_result::ex_ctor:
116  failure_type = "Error";
117  failure_msg = "Constructor has thrown an exception: " + failed_tests_[i].exception_typeid + '\n';
118  errors++;
119  break;
120  case test_result::rethrown:
121  failure_type = "Assertion";
122  failure_msg = "Child failed\n";
123  failures++;
124  break;
125  default: // ok, skipped, dummy
126  failure_type = "Error";
127  failure_msg = "Unknown test status, this should have never happened. "
128  "You may just have found a bug in TUT, please report it immediately.\n";
129  errors++;
130  break;
131  }
132 
133  *stream_ << " <FailedTest id=\"" << failed_tests_[i].test << "\">" << std::endl
134  << " <Name>" << encode(failed_tests_[i].group) + "::" + encode(failed_tests_[i].name) << "</Name>" << std::endl
135  << " <FailureType>" << failure_type << "</FailureType>" << std::endl
136  << " <Location>" << std::endl
137  << " <File>Unknown</File>" << std::endl
138  << " <Line>Unknown</Line>" << std::endl
139  << " </Location>" << std::endl
140  << " <Message>" << encode(failure_msg + failed_tests_[i].message) << "</Message>" << std::endl
141  << " </FailedTest>" << std::endl;
142  }
143 
144  *stream_ << " </FailedTests>" << std::endl;
145  }
146 
147  /* *********************** passed tests ***************************** */
148  if (passed_tests_.size() > 0) {
149  *stream_ << " <SuccessfulTests>" << std::endl;
150 
151  for (unsigned int i=0; i<passed_tests_.size(); i++)
152  {
153  *stream_ << " <Test id=\"" << passed_tests_[i].test << "\">" << std::endl
154  << " <Name>" << encode(passed_tests_[i].group) + "::" + encode(passed_tests_[i].name) << "</Name>" << std::endl
155  << " </Test>" << std::endl;
156  }
157 
158  *stream_ << " </SuccessfulTests>" << std::endl;
159  }
160 
161  /* *********************** statistics ***************************** */
162  *stream_ << " <Statistics>" << std::endl
163  << " <Tests>" << (failed_tests_.size() + passed_tests_.size()) << "</Tests>" << std::endl
164  << " <FailuresTotal>" << failed_tests_.size() << "</FailuresTotal>" << std::endl
165  << " <Errors>" << errors << "</Errors>" << std::endl
166  << " <Failures>" << failures << "</Failures>" << std::endl
167  << " </Statistics>" << std::endl;
168 
169  /* *********************** footer ***************************** */
170  *stream_ << "</TestRun>" << std::endl;
171  }
172 
173  virtual bool all_ok() const
174  {
175  return failed_tests_.empty();
176  }
177 
184  static std::string encode(const std::string & text)
185  {
186  std::string out;
187 
188  for (unsigned int i=0; i<text.length(); ++i) {
189  char c = text[i];
190  switch (c) {
191  case '<':
192  out += "&lt;";
193  break;
194  case '>':
195  out += "&gt;";
196  break;
197  case '&':
198  out += "&amp;";
199  break;
200  case '\'':
201  out += "&apos;";
202  break;
203  case '"':
204  out += "&quot;";
205  break;
206  default:
207  out += c;
208  }
209  }
210 
211  return out;
212  }
213 };
214 
215 }
216 
217 #endif
218 
Definition: tut_exception.hpp:13
void run_completed()
Definition: tut_cppunit_reporter.hpp:77
test finished successfully
Definition: tut_result.hpp:91
Definition: tut_cppunit_reporter.hpp:18
test finished successfully, but test destructor throwed
Definition: tut_result.hpp:94
test failed with ensure() or fail() methods
Definition: tut_result.hpp:92
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
void run_started()
Definition: tut_cppunit_reporter.hpp:57
static std::string encode(const std::string &text)
Encodes text to XML XML-reserved characters (e.g. &quot;&lt;&quot;) are encoded according to specification.
Definition: tut_cppunit_reporter.hpp:184
void test_completed(const tut::test_result &tr)
Definition: tut_cppunit_reporter.hpp:63
Definition: tut_runner.hpp:38

All Rights Reserved. Generated on Wed Dec 18 2013 11:19:52 for TUT by doxygen 1.8.5