[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge pull request #4508 from davidmatson:addJsonSkipped
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 622929007
Change-Id: Ifaf5a701baee74503e6845f32ebc27425882e950
  • Loading branch information
Copybara-Service committed Apr 8, 2024
2 parents f10e11f + f16770d commit 3d73dee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
59 changes: 43 additions & 16 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4743,26 +4743,53 @@ 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) << "}";
}

Expand Down
15 changes: 15 additions & 0 deletions googletest/test/googletest-json-output-unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@
'time': '*',
'timestamp': '*',
'classname': 'SkippedTest',
'skipped': [
{'message': 'gtest_xml_output_unittest_.cc:*\n\n'}
],
},
{
'name': 'SkippedWithMessage',
Expand All @@ -160,6 +163,12 @@
'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',
Expand All @@ -179,6 +188,12 @@
),
'type': '',
}],
'skipped': [{
'message': (
'gtest_xml_output_unittest_.cc:*\n'
'It is good practice to tell why you skip a test.\n'
)
}],
},
],
},
Expand Down
3 changes: 3 additions & 0 deletions googletest/test/gtest_json_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def _normalize(key, value):
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:
Expand Down

1 comment on commit 3d73dee

@LadySkyWatcher
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <gtest/gtest.h>

#include "JsonUnitTestResultPrinter.h" // Include the header file for your class/function

// Test case for a typical scenario
TEST(JsonUnitTestResultPrinterTest, TypicalScenario) {
// Arrange: Set up any necessary objects or data
// Act: Call the function you're testing
// Assert: Check that the function produces the expected output
}

// Test case for an edge case or special scenario
TEST(JsonUnitTestResultPrinterTest, EdgeCaseScenario) {
// Arrange
// Act
// Assert
}

Please sign in to comment.