From 951385b2901c7997723ccd5d34d7190815e6d795 Mon Sep 17 00:00:00 2001 From: Brian Okken <1568356+okken@users.noreply.github.com> Date: Mon, 8 Jan 2024 05:37:55 -0800 Subject: [PATCH] Fix #153, turn off summaries for pytest < 7.3 --- src/pytest_check/plugin.py | 30 ++++++++++++++++++++++-------- tests/test_summary.py | 10 ++++++++++ tox.ini | 12 +++++++++--- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/pytest_check/plugin.py b/src/pytest_check/plugin.py index 9ff1419..67b36f4 100644 --- a/src/pytest_check/plugin.py +++ b/src/pytest_check/plugin.py @@ -40,14 +40,28 @@ def pytest_runtest_makereport(item, call): raise AssertionError(report.longrepr) except AssertionError as e: excinfo = ExceptionInfo.from_current() - e_str = str(e) - # will be 5 with color, 0 without - if e_str.find('FAILURE: ') in (0, 5): - e_str = e_str.split('FAILURE: ')[1] - reprcrash = ReprFileLocation(item.nodeid, 0, e_str) - reprtraceback = ExceptionRepr(reprcrash, excinfo) - chain_repr = ExceptionChainRepr([(reprtraceback, reprcrash, str(e))]) - report.longrepr = chain_repr + if pytest.version_tuple >= (7,3,0): + # Build a summary report with failure reason + # Depends on internals of pytest, which changed in 7.3 + # + # Example: Before 7.3: + # =========== short test summary info =========== + # FAILED test_example_simple.py::test_fail + # Example after 7.3: + # =========== short test summary info =========== + # FAILED test_example_simple.py::test_fail - assert 1 == 2 + # + e_str = str(e) + e_str = e_str.split('FAILURE: ')[1] # Remove redundant "Failure: " + reprcrash = ReprFileLocation(item.nodeid, 0, e_str) + reprtraceback = ExceptionRepr(reprcrash, excinfo) + chain_repr = ExceptionChainRepr([(reprtraceback, reprcrash, str(e))]) + report.longrepr = chain_repr + else: # pragma: no cover + # coverage is run on latest pytest + # we'll have one test run on an older pytest just to make sure + # it works. + ... call.excinfo = excinfo diff --git a/tests/test_summary.py b/tests/test_summary.py index 871d08a..d1724bb 100644 --- a/tests/test_summary.py +++ b/tests/test_summary.py @@ -1,9 +1,19 @@ +import pytest + + +require_pytest_7_3 = pytest.mark.skipif( + pytest.version_tuple < (7, 3, 0), + reason="summary message only supported on pytest7.3+") + + +@require_pytest_7_3 def test_baseline(pytester): pytester.copy_example("examples/test_example_summary.py") result = pytester.runpytest("-k check_no_msg") result.stdout.fnmatch_lines(["*FAILED*-*check 1 == 2*"]) +@require_pytest_7_3 def test_message(pytester): pytester.copy_example("examples/test_example_summary.py") result = pytester.runpytest("-k check_msg") diff --git a/tox.ini b/tox.ini index 8018dae..65fa20e 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py37, py38, py39, py310, py311, py312, coverage, lint +envlist = py37, py38, py39, py310, py311, py312, pytest_earliest, coverage, lint skip_missing_interpreters = true @@ -17,6 +17,12 @@ commands = coverage report --fail-under=100 --show-missing description = Run pytest, with coverage +[testenv:pytest_earliest] +deps = pytest==7.0.0 +basepython = python3.11 +commands = pytest {posargs} +description = Run earliest supported pytest + [testenv:lint] skip_install = true deps = ruff @@ -25,10 +31,10 @@ commands = ruff src tests examples description = Run ruff over src, test, exampless [pytest] -addopts = +addopts = --color=yes --strict-markers - --strict-config + --strict-config -ra testpaths = tests