Skip to content

Commit 5eb9972

Browse files
dajoseDavid Diaz Barquero
authored andcommitted
Add c++ test output to its own report section
1 parent ced4ecf commit 5eb9972

7 files changed

Lines changed: 64 additions & 21 deletions

File tree

pytest_cpp/boost.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def read_file(name):
5151
]
5252
args.extend(test_args)
5353
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
54-
stdout, _ = p.communicate()
54+
raw_stdout, _ = p.communicate()
55+
stdout = raw_stdout.decode("utf-8") if raw_stdout else ""
5556

5657
log = read_file(log_xml)
5758
report = read_file(report_xml)
@@ -76,20 +77,23 @@ def read_file(name):
7677
returncode=p.returncode,
7778
),
7879
)
79-
return [failure]
80+
return [failure], stdout
8081

8182
if report is not None and (
8283
report.startswith("Boost.Test framework internal error: ")
8384
or report.startswith("Test setup error: ")
8485
):
8586
# boost.test doesn't do XML output on fatal-enough errors.
8687
failure = BoostTestFailure("unknown location", 0, report)
87-
return [failure]
88+
return [failure], stdout
8889

8990
results = self._parse_log(log=log)
9091
shutil.rmtree(temp_dir)
92+
9193
if results:
92-
return results
94+
return results, stdout
95+
96+
return None, stdout
9397

9498
def _parse_log(self, log):
9599
"""

pytest_cpp/google.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,21 @@ def strip_comment(x):
5959

6060
def run_test(self, executable, test_id, test_args=(), harness=None):
6161
harness = harness or []
62+
output = ""
6263
xml_filename = self._get_temp_xml_filename()
6364
args = harness + [
6465
executable,
6566
"--gtest_filter=" + test_id,
6667
"--gtest_output=xml:%s" % xml_filename,
6768
]
6869
args.extend(test_args)
70+
6971
try:
70-
subprocess.check_output(
72+
output = subprocess.check_output(
7173
args, stderr=subprocess.STDOUT, universal_newlines=True
7274
)
7375
except subprocess.CalledProcessError as e:
76+
output = e.output
7477
if e.returncode != 1:
7578
msg = (
7679
"Internal Error: calling {executable} "
@@ -85,23 +88,24 @@ def run_test(self, executable, test_id, test_args=(), harness=None):
8588
returncode=e.returncode,
8689
)
8790
)
88-
return [failure]
91+
92+
return [failure], output
8993

9094
results = self._parse_xml(xml_filename)
9195
os.remove(xml_filename)
9296
for (executed_test_id, failures, skipped) in results:
9397
if executed_test_id == test_id:
9498
if failures:
95-
return [GoogleTestFailure(x) for x in failures]
99+
return [GoogleTestFailure(x) for x in failures], output
96100
elif skipped:
97101
pytest.skip()
98102
else:
99-
return None
103+
return None, output
100104

101105
msg = "Internal Error: could not find test " "{test_id} in results:\n{results}"
102106
results_list = "\n".join(x for (x, f) in results)
103107
failure = GoogleTestFailure(msg.format(test_id=test_id, results=results_list))
104-
return [failure]
108+
return [failure], output
105109

106110
def _get_temp_xml_filename(self):
107111
return tempfile.mktemp()

pytest_cpp/plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,15 @@ def from_parent(cls, parent, name, facade, arguments):
126126
)
127127

128128
def runtest(self):
129-
failures = self.facade.run_test(
129+
failures, output = self.facade.run_test(
130130
str(self.fspath),
131131
self.name,
132132
self._arguments,
133133
harness=self.config.getini("cpp_harness"),
134134
)
135+
# Report the c++ output in its own sections
136+
self.add_report_section("call", "c++", output)
137+
135138
if failures:
136139
raise CppFailureError(failures)
137140

tests/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*
2+
3+
!.gitignore
4+
!*.h
5+
!*.cpp
6+
!*.cc
7+
!*.py
8+
!*.xml
9+
!README*
10+
!SCons*

tests/boost_failure.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
BOOST_AUTO_TEST_CASE( test_failure_1 )
77
{
8+
printf("Just saying hi from boost\n");
89
BOOST_CHECK( 2 * 3 == 5 );
910
}
1011

tests/gtest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ TEST_F(FooTest, test_success) {
1414

1515
// Tests that the Foo::Bar() method does Abc.
1616
TEST_F(FooTest, test_failure) {
17+
printf("Just saying hi from gtest\n");
1718
EXPECT_EQ(2 * 3, 5);
1819
EXPECT_EQ(2 * 6, 15);
1920
}

tests/test_pytest_cpp.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,32 @@ def test_is_test_suite(facade, name, other_name, exes, tmpdir):
7878
],
7979
)
8080
def test_success(facade, name, test_id, exes):
81-
assert facade.run_test(exes.get(name), test_id) is None
81+
assert facade.run_test(exes.get(name), test_id)[0] is None
8282

8383

8484
def test_google_failure(exes):
8585
facade = GoogleTestFacade()
86-
failures = facade.run_test(exes.get("gtest"), "FooTest.test_failure")
86+
failures, _ = facade.run_test(exes.get("gtest"), "FooTest.test_failure")
8787
assert len(failures) == 2
8888
colors = ("red", "bold")
8989
assert failures[0].get_lines() == [
9090
(" Expected: 2 * 3", colors),
9191
(" Which is: 6", colors),
9292
("To be equal to: 5", colors),
9393
]
94-
assert failures[0].get_file_reference() == ("gtest.cpp", 17)
94+
assert failures[0].get_file_reference() == ("gtest.cpp", 19)
9595

9696
assert failures[1].get_lines() == [
9797
(" Expected: 2 * 6", colors),
9898
(" Which is: 12", colors),
9999
("To be equal to: 15", colors),
100100
]
101-
assert failures[1].get_file_reference() == ("gtest.cpp", 18)
101+
assert failures[1].get_file_reference() == ("gtest.cpp", 20)
102102

103103

104104
def test_google_error(exes):
105105
facade = GoogleTestFacade()
106-
failures = facade.run_test(exes.get("gtest"), "FooTest.test_error")
106+
failures, _ = facade.run_test(exes.get("gtest"), "FooTest.test_error")
107107
assert len(failures) == 1
108108
colors = ("red", "bold")
109109
assert failures[0].get_lines() == [
@@ -124,21 +124,21 @@ def test_google_disabled(exes):
124124

125125
def test_boost_failure(exes):
126126
facade = BoostTestFacade()
127-
failures = facade.run_test(exes.get("boost_failure"), "<unused>")
127+
failures, _ = facade.run_test(exes.get("boost_failure"), "<unused>")
128128
assert len(failures) == 2
129129

130130
fail1, fail2 = failures
131131
colors = ("red", "bold")
132132
assert fail1.get_lines() == [("check 2 * 3 == 5 failed", colors)]
133-
assert fail1.get_file_reference() == ("boost_failure.cpp", 8)
133+
assert fail1.get_file_reference() == ("boost_failure.cpp", 9)
134134

135135
assert fail2.get_lines() == [("check 2 - 1 == 0 failed", colors)]
136-
assert fail2.get_file_reference() == ("boost_failure.cpp", 14)
136+
assert fail2.get_file_reference() == ("boost_failure.cpp", 15)
137137

138138

139139
def test_boost_fatal_error(exes):
140140
facade = BoostTestFacade()
141-
failures = facade.run_test(exes.get("boost_fatal_error"), "<unused>")
141+
failures, _ = facade.run_test(exes.get("boost_fatal_error"), "<unused>")
142142
assert len(failures) == 1
143143

144144
(fail1,) = failures
@@ -149,7 +149,7 @@ def test_boost_fatal_error(exes):
149149

150150
def test_boost_error(exes):
151151
facade = BoostTestFacade()
152-
failures = facade.run_test(exes.get("boost_error"), "<unused>")
152+
failures, _ = facade.run_test(exes.get("boost_error"), "<unused>")
153153
assert len(failures) == 2
154154

155155
fail1, fail2 = failures
@@ -165,7 +165,7 @@ def test_boost_error(exes):
165165

166166
def test_boost_fixture_setup_error(exes):
167167
facade = BoostTestFacade()
168-
failures = facade.run_test(exes.get("boost_fixture_setup_error"), "<unused>")
168+
failures, _ = facade.run_test(exes.get("boost_fixture_setup_error"), "<unused>")
169169
assert len(failures) == 1
170170

171171
fail1 = failures[0]
@@ -501,6 +501,26 @@ def test_exe_mask_on_windows(tmpdir, monkeypatch):
501501
assert not pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
502502

503503

504+
def test_output_section(testdir, exes):
505+
exes.get("boost_failure")
506+
exes.get("gtest")
507+
508+
testdir.makeini(
509+
"""
510+
[pytest]
511+
cpp_files = gtest* boost*
512+
"""
513+
)
514+
result = testdir.runpytest("-k", "failure")
515+
result.stdout.fnmatch_lines(
516+
[
517+
"*Captured c++ call*",
518+
"Just saying hi from boost",
519+
"Just saying hi from gtest",
520+
]
521+
)
522+
523+
504524
class TestError:
505525
def test_get_whitespace(self):
506526
assert error.get_left_whitespace(" foo") == " "

0 commit comments

Comments
 (0)