Skip to content

Commit f16770d

Browse files
committed
Add skipped messages to JSON output (fixes #4507).
Fix the gap between JSON and XML output.
1 parent ec7b386 commit f16770d

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

googletest/src/gtest.cc

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4743,26 +4743,51 @@ void JsonUnitTestResultPrinter::OutputJsonTestResult(::std::ostream* stream,
47434743
const TestResult& result) {
47444744
const std::string kIndent = Indent(10);
47454745

4746-
int failures = 0;
4747-
for (int i = 0; i < result.total_part_count(); ++i) {
4748-
const TestPartResult& part = result.GetTestPartResult(i);
4749-
if (part.failed()) {
4750-
*stream << ",\n";
4751-
if (++failures == 1) {
4752-
*stream << kIndent << "\"" << "failures" << "\": [\n";
4746+
{
4747+
int failures = 0;
4748+
for (int i = 0; i < result.total_part_count(); ++i) {
4749+
const TestPartResult& part = result.GetTestPartResult(i);
4750+
if (part.failed()) {
4751+
*stream << ",\n";
4752+
if (++failures == 1) {
4753+
*stream << kIndent << "\"" << "failures" << "\": [\n";
4754+
}
4755+
const std::string location =
4756+
internal::FormatCompilerIndependentFileLocation(part.file_name(),
4757+
part.line_number());
4758+
const std::string message = EscapeJson(location + "\n" + part.message());
4759+
*stream << kIndent << " {\n"
4760+
<< kIndent << " \"failure\": \"" << message << "\",\n"
4761+
<< kIndent << " \"type\": \"\"\n"
4762+
<< kIndent << " }";
4763+
}
4764+
}
4765+
4766+
if (failures > 0) *stream << "\n" << kIndent << "]";
4767+
}
4768+
4769+
{
4770+
int skipped = 0;
4771+
for (int i = 0; i < result.total_part_count(); ++i) {
4772+
const TestPartResult& part = result.GetTestPartResult(i);
4773+
if (part.skipped()) {
4774+
*stream << ",\n";
4775+
if (++skipped == 1) {
4776+
*stream << kIndent << "\"" << "skipped" << "\": [\n";
4777+
}
4778+
const std::string location =
4779+
internal::FormatCompilerIndependentFileLocation(part.file_name(),
4780+
part.line_number());
4781+
const std::string message = EscapeJson(location + "\n" + part.message());
4782+
*stream << kIndent << " {\n"
4783+
<< kIndent << " \"message\": \"" << message << "\"\n"
4784+
<< kIndent << " }";
47534785
}
4754-
const std::string location =
4755-
internal::FormatCompilerIndependentFileLocation(part.file_name(),
4756-
part.line_number());
4757-
const std::string message = EscapeJson(location + "\n" + part.message());
4758-
*stream << kIndent << " {\n"
4759-
<< kIndent << " \"failure\": \"" << message << "\",\n"
4760-
<< kIndent << " \"type\": \"\"\n"
4761-
<< kIndent << " }";
47624786
}
4787+
4788+
if (skipped > 0) *stream << "\n" << kIndent << "]";
47634789
}
47644790

4765-
if (failures > 0) *stream << "\n" << kIndent << "]";
47664791
*stream << "\n" << Indent(8) << "}";
47674792
}
47684793

googletest/test/googletest-json-output-unittest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@
150150
'time': '*',
151151
'timestamp': '*',
152152
'classname': 'SkippedTest',
153+
'skipped': [{
154+
'message': (
155+
'gtest_xml_output_unittest_.cc:*\n'
156+
'\n'
157+
)
158+
}],
153159
},
154160
{
155161
'name': 'SkippedWithMessage',
@@ -160,6 +166,12 @@
160166
'time': '*',
161167
'timestamp': '*',
162168
'classname': 'SkippedTest',
169+
'skipped': [{
170+
'message': (
171+
'gtest_xml_output_unittest_.cc:*\n'
172+
'It is good practice to tell why you skip a test.\n'
173+
)
174+
}],
163175
},
164176
{
165177
'name': 'SkippedAfterFailure',
@@ -179,6 +191,12 @@
179191
),
180192
'type': '',
181193
}],
194+
'skipped': [{
195+
'message': (
196+
'gtest_xml_output_unittest_.cc:*\n'
197+
'It is good practice to tell why you skip a test.\n'
198+
)
199+
}],
182200
},
183201
],
184202
},

googletest/test/gtest_json_test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def _normalize(key, value):
5151
elif key == 'failure':
5252
value = re.sub(r'^.*[/\\](.*:)\d+\n', '\\1*\n', value)
5353
return re.sub(r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', value)
54+
elif key == 'message':
55+
value = re.sub(r'^.*[/\\](.*:)\d+\n', '\\1*\n', value)
56+
return re.sub(r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', value)
5457
elif key == 'file':
5558
return re.sub(r'^.*[/\\](.*)', '\\1', value)
5659
else:

0 commit comments

Comments
 (0)