From f7621f01918415d5d51a5c967cfe0e06a1544bb0 Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Tue, 8 Mar 2022 01:12:35 +0100 Subject: [PATCH 1/5] Output source file path and line number in xml and json files. It outputs the location of testcase only. Standalone EXPECT will have no location. --- googletest/src/gtest.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 396fdfe2..a877bc75 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -4286,10 +4286,11 @@ void XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream, OutputXmlAttribute(stream, kTestsuite, "type_param", test_info.type_param()); } + + OutputXmlAttribute(stream, kTestsuite, "file", test_info.file()); + OutputXmlAttribute(stream, kTestsuite, "line", + StreamableToString(test_info.line())); if (GTEST_FLAG_GET(list_tests)) { - OutputXmlAttribute(stream, kTestsuite, "file", test_info.file()); - OutputXmlAttribute(stream, kTestsuite, "line", - StreamableToString(test_info.line())); *stream << " />\n"; return; } @@ -4744,11 +4745,14 @@ void JsonUnitTestResultPrinter::OutputJsonTestInfo(::std::ostream* stream, OutputJsonKey(stream, kTestsuite, "type_param", test_info.type_param(), kIndent); } + + OutputJsonKey(stream, kTestsuite, "file", test_info.file(), kIndent); + OutputJsonKey(stream, kTestsuite, "line", test_info.line(), kIndent, false); if (GTEST_FLAG_GET(list_tests)) { - OutputJsonKey(stream, kTestsuite, "file", test_info.file(), kIndent); - OutputJsonKey(stream, kTestsuite, "line", test_info.line(), kIndent, false); *stream << "\n" << Indent(8) << "}"; return; + } else { + *stream << ",\n"; } OutputJsonKey(stream, kTestsuite, "status", From ee5e3043ce54939c31e6b7e14ca6cb69cc5bf18f Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Tue, 8 Mar 2022 01:14:21 +0100 Subject: [PATCH 2/5] Add support for testing of xml and json output of source file and line location --- googletest/test/gtest_json_test_utils.py | 2 ++ googletest/test/gtest_xml_test_utils.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/googletest/test/gtest_json_test_utils.py b/googletest/test/gtest_json_test_utils.py index 62bbfc28..f62896c9 100644 --- a/googletest/test/gtest_json_test_utils.py +++ b/googletest/test/gtest_json_test_utils.py @@ -50,6 +50,8 @@ 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 == 'file': + return re.sub(r'^.*[/\\](.*)', '\\1', value) else: return normalize(value) if isinstance(obj, dict): diff --git a/googletest/test/gtest_xml_test_utils.py b/googletest/test/gtest_xml_test_utils.py index 50c6e7dc..c6fb9f44 100755 --- a/googletest/test/gtest_xml_test_utils.py +++ b/googletest/test/gtest_xml_test_utils.py @@ -170,6 +170,10 @@ class GTestXMLTestCase(gtest_test_utils.TestCase): * The stack traces are removed. """ + if element.tagName == 'testcase': + source_file = element.getAttributeNode('file') + if source_file: + source_file.value = re.sub(r'^.*[/\\](.*)', '\\1', source_file.value) if element.tagName in ('testsuites', 'testsuite', 'testcase'): timestamp = element.getAttributeNode('timestamp') timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$', From 5c1b42a58dbc0e045270a96ce4de913ee9d4eeed Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Tue, 8 Mar 2022 01:15:20 +0100 Subject: [PATCH 3/5] Adjust xml and json unit tests to test for source file and line location. --- .../test/googletest-json-outfiles-test.py | 4 ++ .../test/googletest-json-output-unittest.py | 64 +++++++++++++++++++ googletest/test/gtest_xml_outfiles_test.py | 4 +- googletest/test/gtest_xml_output_unittest.py | 60 ++++++++--------- 4 files changed, 100 insertions(+), 32 deletions(-) diff --git a/googletest/test/googletest-json-outfiles-test.py b/googletest/test/googletest-json-outfiles-test.py index db9716c2..179283b8 100644 --- a/googletest/test/googletest-json-outfiles-test.py +++ b/googletest/test/googletest-json-outfiles-test.py @@ -71,6 +71,8 @@ EXPECTED_1 = { u'*', u'testsuite': [{ u'name': u'TestSomeProperties', + u'file': u'gtest_xml_outfile1_test_.cc', + u'line': 41, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -115,6 +117,8 @@ EXPECTED_2 = { u'*', u'testsuite': [{ u'name': u'TestSomeProperties', + u'file': u'gtest_xml_outfile2_test_.cc', + u'line': 41, u'status': u'RUN', u'result': u'COMPLETED', u'timestamp': u'*', diff --git a/googletest/test/googletest-json-output-unittest.py b/googletest/test/googletest-json-output-unittest.py index cb31965e..d65b2a21 100644 --- a/googletest/test/googletest-json-output-unittest.py +++ b/googletest/test/googletest-json-output-unittest.py @@ -90,6 +90,8 @@ EXPECTED_NON_EMPTY = { u'*', u'testsuite': [{ u'name': u'Succeeds', + u'file': u'gtest_xml_output_unittest_.cc', + u"line": 51, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -114,6 +116,10 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'Fails', + u'file': + u'gtest_xml_output_unittest_.cc', + u'line': + 59, u'status': u'RUN', u'result': @@ -148,6 +154,8 @@ EXPECTED_NON_EMPTY = { u'*', u'testsuite': [{ u'name': u'DISABLED_test_not_run', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 66, u'status': u'NOTRUN', u'result': u'SUPPRESSED', u'time': u'*', @@ -171,6 +179,8 @@ EXPECTED_NON_EMPTY = { u'*', u'testsuite': [{ u'name': u'Skipped', + u'file': 'gtest_xml_output_unittest_.cc', + u'line': 73, u'status': u'RUN', u'result': u'SKIPPED', u'time': u'*', @@ -178,6 +188,8 @@ EXPECTED_NON_EMPTY = { u'classname': u'SkippedTest' }, { u'name': u'SkippedWithMessage', + u'file': 'gtest_xml_output_unittest_.cc', + u'line': 77, u'status': u'RUN', u'result': u'SKIPPED', u'time': u'*', @@ -186,6 +198,10 @@ EXPECTED_NON_EMPTY = { }, { u'name': u'SkippedAfterFailure', + u'file': + 'gtest_xml_output_unittest_.cc', + u'line': + 81, u'status': u'RUN', u'result': @@ -220,6 +236,8 @@ EXPECTED_NON_EMPTY = { u'*', u'testsuite': [{ u'name': u'Succeeds', + u'file': 'gtest_xml_output_unittest_.cc', + u'line': 86, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -228,6 +246,10 @@ EXPECTED_NON_EMPTY = { }, { u'name': u'Fails', + u'file': + u'gtest_xml_output_unittest_.cc', + u'line': + 91, u'status': u'RUN', u'result': @@ -251,6 +273,8 @@ EXPECTED_NON_EMPTY = { }] }, { u'name': u'DISABLED_test', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 96, u'status': u'NOTRUN', u'result': u'SUPPRESSED', u'time': u'*', @@ -275,6 +299,10 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'OutputsCData', + u'file': + u'gtest_xml_output_unittest_.cc', + u'line': + 100, u'status': u'RUN', u'result': @@ -311,6 +339,10 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'InvalidCharactersInMessage', + u'file': + u'gtest_xml_output_unittest_.cc', + u'line': + 107, u'status': u'RUN', u'result': @@ -349,6 +381,8 @@ EXPECTED_NON_EMPTY = { u'aye', u'testsuite': [{ u'name': u'OneProperty', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 119, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -357,6 +391,8 @@ EXPECTED_NON_EMPTY = { u'key_1': u'1' }, { u'name': u'IntValuedProperty', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 123, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -365,6 +401,8 @@ EXPECTED_NON_EMPTY = { u'key_int': u'1' }, { u'name': u'ThreeProperties', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 127, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -375,6 +413,8 @@ EXPECTED_NON_EMPTY = { u'key_3': u'3' }, { u'name': u'TwoValuesForOneKeyUsesLastValue', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 133, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -399,6 +439,8 @@ EXPECTED_NON_EMPTY = { u'*', u'testsuite': [{ u'name': u'RecordProperty', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 138, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -407,6 +449,8 @@ EXPECTED_NON_EMPTY = { u'key': u'1' }, { u'name': u'ExternalUtilityThatCallsRecordIntValuedProperty', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 151, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -415,6 +459,8 @@ EXPECTED_NON_EMPTY = { u'key_for_utility_int': u'1' }, { u'name': u'ExternalUtilityThatCallsRecordStringValuedProperty', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 155, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -440,6 +486,8 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'HasTypeParamAttribute', u'type_param': u'int', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 171, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -464,6 +512,8 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'HasTypeParamAttribute', u'type_param': u'long', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 171, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -488,6 +538,8 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'HasTypeParamAttribute', u'type_param': u'int', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 178, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -512,6 +564,8 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'HasTypeParamAttribute', u'type_param': u'long', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 178, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -536,6 +590,8 @@ EXPECTED_NON_EMPTY = { u'testsuite': [{ u'name': u'HasValueParamAttribute/0', u'value_param': u'33', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 162, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -544,6 +600,8 @@ EXPECTED_NON_EMPTY = { }, { u'name': u'HasValueParamAttribute/1', u'value_param': u'42', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 162, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -552,6 +610,8 @@ EXPECTED_NON_EMPTY = { }, { u'name': u'AnotherTestThatHasValueParamAttribute/0', u'value_param': u'33', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 163, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -560,6 +620,8 @@ EXPECTED_NON_EMPTY = { }, { u'name': u'AnotherTestThatHasValueParamAttribute/1', u'value_param': u'42', + u'file': u'gtest_xml_output_unittest_.cc', + u'line': 163, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', @@ -603,6 +665,8 @@ EXPECTED_FILTERED = { u'*', u'testsuite': [{ u'name': u'Succeeds', + u'file': u'gtest_xml_output_unittest_.cc', + u"line": 51, u'status': u'RUN', u'result': u'COMPLETED', u'time': u'*', diff --git a/googletest/test/gtest_xml_outfiles_test.py b/googletest/test/gtest_xml_outfiles_test.py index 916bdf4d..c129e64b 100755 --- a/googletest/test/gtest_xml_outfiles_test.py +++ b/googletest/test/gtest_xml_outfiles_test.py @@ -43,7 +43,7 @@ GTEST_OUTPUT_2_TEST = "gtest_xml_outfile2_test_" EXPECTED_XML_1 = """ - + @@ -57,7 +57,7 @@ EXPECTED_XML_1 = """ EXPECTED_XML_2 = """ - + diff --git a/googletest/test/gtest_xml_output_unittest.py b/googletest/test/gtest_xml_output_unittest.py index f0b0c3b9..e1b7f1fc 100755 --- a/googletest/test/gtest_xml_output_unittest.py +++ b/googletest/test/gtest_xml_output_unittest.py @@ -67,10 +67,10 @@ else: EXPECTED_NON_EMPTY_XML = """ - + - + - - + + - + - + ]]>%(stack)s]]> - + - + - + - + - + - + - + - + - + - + - + - + - - - - + + + + - + - + - + - + """ % { 'stack': STACK_TRACE_TEMPLATE @@ -195,24 +195,24 @@ EXPECTED_FILTERED_TEST_XML = """ timestamp="*" name="AllTests" ad_hoc_property="42"> - + """ EXPECTED_SHARDED_TEST_XML = """ - + - + - + """ From 132059ba0c495406f8e523f01f7ea473ea6c6bd2 Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Tue, 8 Mar 2022 01:16:36 +0100 Subject: [PATCH 4/5] Adjust documentation regarding xml and json source file location otput. --- docs/advanced.md | 20 ++++++++++++++++---- docs/reference/testing.md | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/docs/advanced.md b/docs/advanced.md index f2f8854b..42f2a09b 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -839,7 +839,7 @@ will output XML like this: ```xml ... - + ... ``` @@ -2082,15 +2082,15 @@ could generate this report: - + ... ... - + - + @@ -2108,6 +2108,8 @@ Things to note: * The `timestamp` attribute records the local date and time of the test execution. +* The `file` and `line` attributes record the source file location, where the test was defined. + * Each `` element corresponds to a single failed googletest assertion. @@ -2147,6 +2149,8 @@ The report format conforms to the following JSON Schema: "type": "object", "properties": { "name": { "type": "string" }, + "file": { "type": "string" }, + "line": { "type": "integer" }, "status": { "type": "string", "enum": ["RUN", "NOTRUN"] @@ -2224,6 +2228,8 @@ message TestCase { message TestInfo { string name = 1; + string file = 6; + int32 line = 7; enum Status { RUN = 0; NOTRUN = 1; @@ -2267,6 +2273,8 @@ could generate this report: "testsuite": [ { "name": "Addition", + "file": "test.cpp", + "line": 1, "status": "RUN", "time": "0.007s", "classname": "", @@ -2283,6 +2291,8 @@ could generate this report: }, { "name": "Subtraction", + "file": "test.cpp", + "line": 2, "status": "RUN", "time": "0.005s", "classname": "" @@ -2298,6 +2308,8 @@ could generate this report: "testsuite": [ { "name": "NonContradiction", + "file": "test.cpp", + "line": 3, "status": "RUN", "time": "0.005s", "classname": "" diff --git a/docs/reference/testing.md b/docs/reference/testing.md index 554d6c95..226fe499 100644 --- a/docs/reference/testing.md +++ b/docs/reference/testing.md @@ -518,8 +518,8 @@ Logs a property for the current test, test suite, or entire invocation of the test program. Only the last value for a given key is logged. The key must be a valid XML attribute name, and cannot conflict with the ones -already used by GoogleTest (`name`, `status`, `time`, `classname`, `type_param`, -and `value_param`). +already used by GoogleTest (`name`, `file`, `line`, `status`, `time`, `classname`, +`type_param`, and `value_param`). `RecordProperty` is `public static` so it can be called from utility functions that are not members of the test fixture. From 3c5320bf6f4632756a64dffb83b7837246e7f743 Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Tue, 8 Mar 2022 01:21:16 +0100 Subject: [PATCH 5/5] Add myself to contributors --- CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index d9bc587b..77397a5b 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -56,6 +56,7 @@ Russ Rufer Sean Mcafee Sigurður Ásgeirsson Sverre Sundsdal +Szymon Sobik Takeshi Yoshino Tracy Bialik Vadim Berman