blob: 3a27b7465e91f39adb4dc11777bd4828dd0c568b [file] [log] [blame]
Phil Nash0ab44322010-12-10 08:01:42 +00001/*
2 * catch_reporter_junit.hpp
3 * Catch
4 *
5 * Created by Phil on 26/11/2010.
6 * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
7 *
8 * Distributed under the Boost Software License, Version 1.0. (See accompanying
9 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 *
11 */
12#ifndef TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED
13#define TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED
14
15#include "internal/catch_capture.hpp"
16#include "internal/catch_reporter_registry.hpp"
17#include "internal/catch_xmlwriter.hpp"
18#include <iostream>
19
20namespace Catch
21{
22 class JunitReporter : public Catch::ITestReporter
23 {
24 struct Indenter
25 {
26 Indenter& operator ++()
27 {
28 m_indent += "\t";
29 return *this;
30 }
31 Indenter& operator --()
32 {
33 m_indent = m_indent.substr( 0, m_indent.length()-1 );
34 return *this;
35 }
36
37 friend std::ostream& operator << ( std::ostream& os, const Indenter& indent )
38 {
39 os << indent.m_indent;
40 return os;
41 }
42
43 std::string m_indent;
44 };
45
46 struct TestStats
47 {
48 std::string element;
49 std::string resultType;
50 std::string message;
51 std::string content;
52 };
53
54 struct TestCaseStats
55 {
56 TestCaseStats( const std::string& name = std::string() )
57 : name( name )
58 {
59 }
60
61 double timeInSeconds;
62 std::string status;
63 std::string className;
64 std::string name;
65 std::vector<TestStats> testStats;
66 };
67
68 struct Stats
69 {
70 Stats( const std::string& name = std::string() )
71 : testsCount( 0 ),
72 failuresCount( 0 ),
73 disabledCount( 0 ),
74 errorsCount( 0 ),
75 timeInSeconds( 0 ),
76 name( name )
77 {
78 }
79
80 std::size_t testsCount;
81 std::size_t failuresCount;
82 std::size_t disabledCount;
83 std::size_t errorsCount;
84 double timeInSeconds;
85 std::string name;
86
87 std::vector<TestCaseStats> testCaseStats;
88 };
89
90 public:
91 ///////////////////////////////////////////////////////////////////////////
92 JunitReporter( const ReporterConfig& config = ReporterConfig() )
93 : m_config( config ),
94 m_testSuiteStats( "AllTests" ),
95 m_currentStats( &m_testSuiteStats )
96 {
97 }
98
99 ///////////////////////////////////////////////////////////////////////////
100 static std::string getDescription()
101 {
102 return "Reports test results in an XML format that looks like Ant's junitreport target";
103 }
104
105 private: // ITestReporter
106
107 ///////////////////////////////////////////////////////////////////////////
108 virtual void StartTesting()
109 {
110 }
111
112 ///////////////////////////////////////////////////////////////////////////
113 virtual void StartGroup( const std::string& groupName )
114 {
115
116// m_config.stream() << "\t<testsuite>\n";
117// if( !groupName.empty() )
118 {
119 m_statsForSuites.push_back( Stats( groupName ) );
120 m_currentStats = &m_statsForSuites.back();
121 }
122 }
123
124 ///////////////////////////////////////////////////////////////////////////
125 virtual void EndGroup( const std::string& groupName, std::size_t succeeded, std::size_t failed )
126 {
127// m_config.stream() << "\t</testsuite>\n";
128 (groupName, succeeded, failed);
129 m_currentStats = &m_testSuiteStats;
130 }
131
132 virtual void StartSection( const std::string& sectionName, const std::string description ){(sectionName,description);}
133 virtual void EndSection( const std::string& sectionName, std::size_t succeeded, std::size_t failed ){(sectionName, succeeded, failed);}
134
135 ///////////////////////////////////////////////////////////////////////////
136 virtual void StartTestCase( const Catch::TestCaseInfo& testInfo )
137 {
138// m_config.stream() << "\t\t<testcase name='" << testInfo.getName() << "'>\n";
139 // m_currentTestSuccess = true;
140 m_currentStats->testCaseStats.push_back( TestCaseStats( testInfo.getName() ) );
141
142 }
143
144 ///////////////////////////////////////////////////////////////////////////
145 virtual void Result( const Catch::ResultInfo& resultInfo )
146 {
147 if( !resultInfo.ok() || m_config.includeSuccessfulResults() )
148 {
149 TestCaseStats& testCaseStats = m_currentStats->testCaseStats.back();
150 TestStats stats;
151 std::ostringstream oss;
152 if( !resultInfo.getMessage().empty() )
153 {
154 oss << resultInfo.getMessage() << " at ";
155 }
156 oss << resultInfo.getFilename() << ":" << resultInfo.getLine();
157 stats.content = oss.str();
158 stats.message = resultInfo.getExpandedExpression();
159 stats.resultType = resultInfo.getTestMacroName();
160 switch( resultInfo.getResultType() )
161 {
162 case ResultWas::ThrewException:
163 stats.element = "error";
164 break;
165 case ResultWas::Info:
166 stats.element = "info"; // !TBD ?
167 break;
168 case ResultWas::Warning:
169 stats.element = "warning"; // !TBD ?
170 break;
171 case ResultWas::ExplicitFailure:
172 stats.element = "failure";
173 break;
174 case ResultWas::ExpressionFailed:
175 stats.element = "failure";
176 break;
177 case ResultWas::Ok:
178 stats.element = "success";
179 break;
180 default:
181 stats.element = "unknown";
182 break;
183 }
184 testCaseStats.testStats.push_back( stats );
185
186 }
187 }
188
189 ///////////////////////////////////////////////////////////////////////////
190 virtual void EndTestCase( const Catch::TestCaseInfo&, const std::string& stdOut, const std::string& stdErr )
191 {
192 if( !stdOut.empty() )
193 m_stdOut << stdOut << "\n";
194 if( !stdErr.empty() )
195 m_stdErr << stdErr << "\n";
196 }
197
198 static std::string trim( const std::string& str )
199 {
200 std::string::size_type start = str.find_first_not_of( "\n\r\t " );
201 std::string::size_type end = str.find_last_not_of( "\n\r\t " );
202
203 return start < end ? str.substr( start, 1+end-start ) : "";
204 }
205
206 ///////////////////////////////////////////////////////////////////////////
207 virtual void EndTesting( std::size_t /* succeeded */, std::size_t /* failed */ )
208 {
209 std::ostream& str = m_config.stream();
210 {
211 XmlWriter xml( str );
212
213 if( m_statsForSuites.size() > 0 )
214 xml.startElement( "testsuites" );
215
216 std::vector<Stats>::const_iterator it = m_statsForSuites.begin();
217 std::vector<Stats>::const_iterator itEnd = m_statsForSuites.end();
218
219 for(; it != itEnd; ++it )
220 {
221 XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" );
222 xml.writeAttribute( "name", it->name );
223
224 OutputTestCases( xml, *it );
225 }
226
227 xml.scopedElement( "system-out" ).writeText( trim( m_stdOut.str() ) );
228 xml.scopedElement( "system-err" ).writeText( trim( m_stdOut.str() ) );
229 }
230 }
231
232 ///////////////////////////////////////////////////////////////////////////
233 void OutputTestCases( XmlWriter& xml, const Stats& stats )
234 {
235 std::vector<TestCaseStats>::const_iterator it = stats.testCaseStats.begin();
236 std::vector<TestCaseStats>::const_iterator itEnd = stats.testCaseStats.end();
237 for(; it != itEnd; ++it )
238 {
239 xml.writeBlankLine();
240 xml.writeComment( "Test case" );
241
242 XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
243 xml.writeAttribute( "classname", it->className );
244 xml.writeAttribute( "name", it->name );
245 xml.writeAttribute( "time", "tbd" );
246
247 OutputTestResult( xml, *it );
248 }
249 }
250
251
252 ///////////////////////////////////////////////////////////////////////////
253 void OutputTestResult( XmlWriter& xml, const TestCaseStats& stats )
254 {
255 std::vector<TestStats>::const_iterator it = stats.testStats.begin();
256 std::vector<TestStats>::const_iterator itEnd = stats.testStats.end();
257 for(; it != itEnd; ++it )
258 {
259 if( it->element != "success" )
260 {
261 XmlWriter::ScopedElement e = xml.scopedElement( it->element );
262
263 xml.writeAttribute( "message", it->message );
264 xml.writeAttribute( "type", it->resultType );
265 if( !it->content.empty() )
266 xml.writeText( it->content );
267 }
268 }
269 }
270
271 private:
272 const ReporterConfig& m_config;
273 bool m_currentTestSuccess;
274
275 Stats m_testSuiteStats;
276 Stats* m_currentStats;
277 std::vector<Stats> m_statsForSuites;
278 std::ostringstream m_stdOut;
279 std::ostringstream m_stdErr;
280
281 Indenter m_indent;
282 };
283
284} // end namespace Catch
285
286#endif // TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED