Show the mnemonic of pytest.ExitCode in RunResult's repr

Fix #4901
This commit is contained in:
Bruno Oliveira 2019-10-22 19:33:54 -03:00 committed by Bruno Oliveira
parent 978c7ae1b7
commit 7beb520555
3 changed files with 26 additions and 13 deletions

View File

@ -0,0 +1,2 @@
``RunResult`` from ``pytester`` now displays the mnemonic of the ``ret`` attribute when it is a
valid ``pytest.ExitCode`` value.

View File

@ -362,7 +362,10 @@ class RunResult:
"""
def __init__(self, ret, outlines, errlines, duration):
self.ret = ret
try:
self.ret = pytest.ExitCode(ret)
except ValueError:
self.ret = ret
self.outlines = outlines
self.errlines = errlines
self.stdout = LineMatcher(outlines)
@ -371,7 +374,7 @@ class RunResult:
def __repr__(self):
return (
"<RunResult ret=%r len(stdout.lines)=%d len(stderr.lines)=%d duration=%.2fs>"
"<RunResult ret=%s len(stdout.lines)=%d len(stderr.lines)=%d duration=%.2fs>"
% (self.ret, len(self.stdout.lines), len(self.stderr.lines), self.duration)
)

View File

@ -121,17 +121,6 @@ def test_runresult_assertion_on_xpassed(testdir):
assert result.ret == 0
def test_runresult_repr():
from _pytest.pytester import RunResult
assert (
repr(
RunResult(ret="ret", outlines=[""], errlines=["some", "errors"], duration=1)
)
== "<RunResult ret='ret' len(stdout.lines)=1 len(stderr.lines)=2 duration=1.00s>"
)
def test_xpassed_with_strict_is_considered_a_failure(testdir):
testdir.makepyfile(
"""
@ -616,3 +605,22 @@ def test_spawn_uses_tmphome(testdir):
child = testdir.spawn_pytest(str(p1))
out = child.read()
assert child.wait() == 0, out.decode("utf8")
def test_run_result_repr():
outlines = ["some", "normal", "output"]
errlines = ["some", "nasty", "errors", "happened"]
# known exit code
r = pytester.RunResult(1, outlines, errlines, duration=0.5)
assert (
repr(r) == "<RunResult ret=ExitCode.TESTS_FAILED len(stdout.lines)=3"
" len(stderr.lines)=4 duration=0.50s>"
)
# unknown exit code: just the number
r = pytester.RunResult(99, outlines, errlines, duration=0.5)
assert (
repr(r) == "<RunResult ret=99 len(stdout.lines)=3"
" len(stderr.lines)=4 duration=0.50s>"
)