diff --git a/changelog/4901.trivial.rst b/changelog/4901.trivial.rst new file mode 100644 index 000000000..f6609ddf1 --- /dev/null +++ b/changelog/4901.trivial.rst @@ -0,0 +1,2 @@ +``RunResult`` from ``pytester`` now displays the mnemonic of the ``ret`` attribute when it is a +valid ``pytest.ExitCode`` value. diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index a050dad09..875828fd6 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -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 ( - "" + "" % (self.ret, len(self.stdout.lines), len(self.stderr.lines), self.duration) ) diff --git a/testing/test_pytester.py b/testing/test_pytester.py index f8b0896c5..1a068c1d0 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -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) - ) - == "" - ) - - 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) == "" + ) + + # unknown exit code: just the number + r = pytester.RunResult(99, outlines, errlines, duration=0.5) + assert ( + repr(r) == "" + )