Skip to content

Commit 89fee55

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

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

changelog/4901.trivial.rst

+2
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

+5-2
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

+19
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,22 @@ def test():
616616
child = testdir.spawn_pytest(str(p1))
617617
out = child.read()
618618
assert child.wait() == 0, out.decode("utf8")
619+
620+
621+
def test_run_result_repr():
622+
outlines = [""] * 3
623+
errlines = [""] * 4
624+
625+
# known exit code
626+
r = pytester.RunResult(1, outlines, errlines, duration=0.5)
627+
assert (
628+
repr(r) == "<RunResult ret=ExitCode.TESTS_FAILED len(stdout.lines)=3"
629+
" len(stderr.lines)=4 duration=0.50s>"
630+
)
631+
632+
# unknown exit code: just the number
633+
r = pytester.RunResult(99, outlines, errlines, duration=0.5)
634+
assert (
635+
repr(r) == "<RunResult ret=99 len(stdout.lines)=3"
636+
" len(stderr.lines)=4 duration=0.50s>"
637+
)

0 commit comments

Comments
 (0)