From f16770d63dcb69019c0547b26a4a433d2cc62bd7 Mon Sep 17 00:00:00 2001 From: David Matson Date: Thu, 28 Mar 2024 20:09:35 -0700 Subject: Add skipped messages to JSON output (fixes #4507). Fix the gap between JSON and XML output. --- googletest/src/gtest.cc | 57 ++++++++++++++++------ googletest/test/googletest-json-output-unittest.py | 18 +++++++ googletest/test/gtest_json_test_utils.py | 3 ++ 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 2653dbb..2aac959 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -4743,26 +4743,51 @@ void JsonUnitTestResultPrinter::OutputJsonTestResult(::std::ostream* stream, const TestResult& result) { const std::string kIndent = Indent(10); - int failures = 0; - for (int i = 0; i < result.total_part_count(); ++i) { - const TestPartResult& part = result.GetTestPartResult(i); - if (part.failed()) { - *stream << ",\n"; - if (++failures == 1) { - *stream << kIndent << "\"" << "failures" << "\": [\n"; + { + int failures = 0; + for (int i = 0; i < result.total_part_count(); ++i) { + const TestPartResult& part = result.GetTestPartResult(i); + if (part.failed()) { + *stream << ",\n"; + if (++failures == 1) { + *stream << kIndent << "\"" << "failures" << "\": [\n"; + } + const std::string location = + internal::FormatCompilerIndependentFileLocation(part.file_name(), + part.line_number()); + const std::string message = EscapeJson(location + "\n" + part.message()); + *stream << kIndent << " {\n" + << kIndent << " \"failure\": \"" << message << "\",\n" + << kIndent << " \"type\": \"\"\n" + << kIndent << " }"; + } + } + + if (failures > 0) *stream << "\n" << kIndent << "]"; + } + + { + int skipped = 0; + for (int i = 0; i < result.total_part_count(); ++i) { + const TestPartResult& part = result.GetTestPartResult(i); + if (part.skipped()) { + *stream << ",\n"; + if (++skipped == 1) { + *stream << kIndent << "\"" << "skipped" << "\": [\n"; + } + const std::string location = + internal::FormatCompilerIndependentFileLocation(part.file_name(), + part.line_number()); + const std::string message = EscapeJson(location + "\n" + part.message()); + *stream << kIndent << " {\n" + << kIndent << " \"message\": \"" << message << "\"\n" + << kIndent << " }"; } - const std::string location = - internal::FormatCompilerIndependentFileLocation(part.file_name(), - part.line_number()); - const std::string message = EscapeJson(location + "\n" + part.message()); - *stream << kIndent << " {\n" - << kIndent << " \"failure\": \"" << message << "\",\n" - << kIndent << " \"type\": \"\"\n" - << kIndent << " }"; } + + if (skipped > 0) *stream << "\n" << kIndent << "]"; } - if (failures > 0) *stream << "\n" << kIndent << "]"; *stream << "\n" << Indent(8) << "}"; } diff --git a/googletest/test/googletest-json-output-unittest.py b/googletest/test/googletest-json-output-unittest.py index cb97694..323f4b3 100644 --- a/googletest/test/googletest-json-output-unittest.py +++ b/googletest/test/googletest-json-output-unittest.py @@ -150,6 +150,12 @@ EXPECTED_NON_EMPTY = { 'time': '*', 'timestamp': '*', 'classname': 'SkippedTest', + 'skipped': [{ + 'message': ( + 'gtest_xml_output_unittest_.cc:*\n' + '\n' + ) + }], }, { 'name': 'SkippedWithMessage', @@ -160,6 +166,12 @@ EXPECTED_NON_EMPTY = { 'time': '*', 'timestamp': '*', 'classname': 'SkippedTest', + 'skipped': [{ + 'message': ( + 'gtest_xml_output_unittest_.cc:*\n' + 'It is good practice to tell why you skip a test.\n' + ) + }], }, { 'name': 'SkippedAfterFailure', @@ -179,6 +191,12 @@ EXPECTED_NON_EMPTY = { ), 'type': '', }], + 'skipped': [{ + 'message': ( + 'gtest_xml_output_unittest_.cc:*\n' + 'It is good practice to tell why you skip a test.\n' + ) + }], }, ], }, diff --git a/googletest/test/gtest_json_test_utils.py b/googletest/test/gtest_json_test_utils.py index 86a5925..694a7a6 100644 --- a/googletest/test/gtest_json_test_utils.py +++ b/googletest/test/gtest_json_test_utils.py @@ -51,6 +51,9 @@ def normalize(obj): elif key == 'failure': value = re.sub(r'^.*[/\\](.*:)\d+\n', '\\1*\n', value) return re.sub(r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', value) + elif key == 'message': + value = re.sub(r'^.*[/\\](.*:)\d+\n', '\\1*\n', value) + return re.sub(r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', value) elif key == 'file': return re.sub(r'^.*[/\\](.*)', '\\1', value) else: -- cgit v0.12