Skip to content

Commit

Permalink
Fix #153, turn off summaries for pytest < 7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
okken committed Jan 8, 2024
1 parent 6d1eb97 commit 951385b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
30 changes: 22 additions & 8 deletions src/pytest_check/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions tests/test_summary.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
12 changes: 9 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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

0 comments on commit 951385b

Please sign in to comment.