Skip to content

Commit 7beb520

Browse files
committed
Show the mnemonic of pytest.ExitCode in RunResult's repr
Fix #4901
1 parent 978c7ae commit 7beb520

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

changelog/4901.trivial.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``RunResult`` from ``pytester`` now displays the mnemonic of the ``ret`` attribute when it is a
2+
valid ``pytest.ExitCode`` value.

src/_pytest/pytester.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,10 @@ class RunResult:
362362
"""
363363

364364
def __init__(self, ret, outlines, errlines, duration):
365-
self.ret = ret
365+
try:
366+
self.ret = pytest.ExitCode(ret)
367+
except ValueError:
368+
self.ret = ret
366369
self.outlines = outlines
367370
self.errlines = errlines
368371
self.stdout = LineMatcher(outlines)
@@ -371,7 +374,7 @@ def __init__(self, ret, outlines, errlines, duration):
371374

372375
def __repr__(self):
373376
return (
374-
"<RunResult ret=%r len(stdout.lines)=%d len(stderr.lines)=%d duration=%.2fs>"
377+
"<RunResult ret=%s len(stdout.lines)=%d len(stderr.lines)=%d duration=%.2fs>"
375378
% (self.ret, len(self.stdout.lines), len(self.stderr.lines), self.duration)
376379
)
377380

testing/test_pytester.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,6 @@ def test_potato():
121121
assert result.ret == 0
122122

123123

124-
def test_runresult_repr():
125-
from _pytest.pytester import RunResult
126-
127-
assert (
128-
repr(
129-
RunResult(ret="ret", outlines=[""], errlines=["some", "errors"], duration=1)
130-
)
131-
== "<RunResult ret='ret' len(stdout.lines)=1 len(stderr.lines)=2 duration=1.00s>"
132-
)
133-
134-
135124
def test_xpassed_with_strict_is_considered_a_failure(testdir):
136125
testdir.makepyfile(
137126
"""
@@ -616,3 +605,22 @@ def test():
616605
child = testdir.spawn_pytest(str(p1))
617606
out = child.read()
618607
assert child.wait() == 0, out.decode("utf8")
608+
609+
610+
def test_run_result_repr():
611+
outlines = ["some", "normal", "output"]
612+
errlines = ["some", "nasty", "errors", "happened"]
613+
614+
# known exit code
615+
r = pytester.RunResult(1, outlines, errlines, duration=0.5)
616+
assert (
617+
repr(r) == "<RunResult ret=ExitCode.TESTS_FAILED len(stdout.lines)=3"
618+
" len(stderr.lines)=4 duration=0.50s>"
619+
)
620+
621+
# unknown exit code: just the number
622+
r = pytester.RunResult(99, outlines, errlines, duration=0.5)
623+
assert (
624+
repr(r) == "<RunResult ret=99 len(stdout.lines)=3"
625+
" len(stderr.lines)=4 duration=0.50s>"
626+
)

0 commit comments

Comments
 (0)