Skip to content

Commit 277b3d1

Browse files
committed
Output testdir failure stdout and stderr
1 parent ea72f85 commit 277b3d1

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

testing/test_basic.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
#! /usr/bin/env py.test
22

33
import sys
4+
import textwrap
45

56
import pytest
67

78

9+
def assert_outcomes(run_result, outcomes):
10+
formatted_output = format_run_result_output_for_assert(run_result)
11+
12+
try:
13+
outcomes = run_result.parseoutcomes()
14+
except ValueError:
15+
assert False, formatted_output
16+
17+
for name, value in outcomes.items():
18+
assert outcomes.get(name) == value, formatted_output
19+
20+
21+
def format_run_result_output_for_assert(run_result):
22+
return textwrap.dedent('''\
23+
24+
---- stdout
25+
{0}
26+
---- stderr
27+
{1}
28+
----''').format(
29+
run_result.stdout.str(),
30+
run_result.stderr.str(),
31+
)
32+
33+
834
def skip_if_reactor_not(expected_reactor):
935
actual_reactor = pytest.config.getoption('reactor')
1036
return pytest.mark.skipif(
@@ -32,8 +58,7 @@ def doit():
3258
return d
3359
""")
3460
rr = testdir.run(sys.executable, "-m", "pytest")
35-
outcomes = rr.parseoutcomes()
36-
assert outcomes.get("failed") == 1
61+
assert_outcomes(rr, {'failed': 1})
3762

3863

3964
def test_succeed_later(testdir):
@@ -46,8 +71,7 @@ def test_succeed():
4671
return d
4772
""")
4873
rr = testdir.run(sys.executable, "-m", "pytest")
49-
outcomes = rr.parseoutcomes()
50-
assert outcomes.get("passed") == 1
74+
assert_outcomes(rr, {'passed': 1})
5175

5276

5377
def test_non_deferred(testdir):
@@ -58,8 +82,7 @@ def test_succeed():
5882
return 42
5983
""")
6084
rr = testdir.run(sys.executable, "-m", "pytest")
61-
outcomes = rr.parseoutcomes()
62-
assert outcomes.get("passed") == 1
85+
assert_outcomes(rr, {'passed': 1})
6386

6487

6588
def test_exception(testdir):
@@ -68,8 +91,7 @@ def test_more_fail():
6891
raise RuntimeError("foo")
6992
""")
7093
rr = testdir.run(sys.executable, "-m", "pytest")
71-
outcomes = rr.parseoutcomes()
72-
assert outcomes.get("failed") == 1
94+
assert_outcomes(rr, {'failed': 1})
7395

7496

7597
def test_inlineCallbacks(testdir):
@@ -91,9 +113,7 @@ def test_succeed(foo):
91113
raise RuntimeError("baz")
92114
""")
93115
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
94-
outcomes = rr.parseoutcomes()
95-
assert outcomes.get("passed") == 2
96-
assert outcomes.get("failed") == 1
116+
assert_outcomes(rr, {'passed': 2, 'failed': 1})
97117

98118

99119
def test_twisted_greenlet(testdir):
@@ -115,8 +135,7 @@ def test_MAIN():
115135
116136
""")
117137
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
118-
outcomes = rr.parseoutcomes()
119-
assert outcomes.get("passed") == 1
138+
assert_outcomes(rr, {'passed': 1})
120139

121140

122141
@skip_if_reactor_not('default')
@@ -140,8 +159,7 @@ def test_succeed():
140159
return d
141160
""")
142161
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
143-
outcomes = rr.parseoutcomes()
144-
assert outcomes.get("passed") == 1
162+
assert_outcomes(rr, {'passed': 1})
145163

146164

147165
@skip_if_reactor_not('qt5reactor')
@@ -171,8 +189,7 @@ def test_succeed():
171189
return d
172190
""")
173191
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
174-
outcomes = rr.parseoutcomes()
175-
assert outcomes.get("passed") == 1
192+
assert_outcomes(rr, {'passed': 1})
176193

177194

178195
@skip_if_reactor_not('qt5reactor')
@@ -187,9 +204,8 @@ def test_succeed():
187204
rr = testdir.run(
188205
sys.executable, "-m", "pytest", "-v", "--reactor=qt5reactor"
189206
)
190-
outcomes = rr.parseoutcomes()
191207
assert 'WrongReactorAlreadyInstalledError' in rr.stdout.str()
192-
assert outcomes.get("error") == 1
208+
assert_outcomes(rr, {'error': 1})
193209

194210

195211
def test_pytest_from_reactor_thread(testdir):
@@ -238,8 +254,6 @@ def main():
238254
""")
239255
# check test file is ok in standalone mode:
240256
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
241-
outcomes = rr.parseoutcomes()
242-
assert outcomes.get("passed") == 1
243-
assert outcomes.get("failed") == 1
257+
assert_outcomes(rr, {'passed': 1, 'failed': 1})
244258
# test embedded mode:
245259
assert testdir.run(sys.executable, "runner.py").ret == 0

0 commit comments

Comments
 (0)