Skip to content

Commit

Permalink
Merge pull request #154 from okken/fix_153
Browse files Browse the repository at this point in the history
Fix #153, turn off summaries for pytest < 7.3
  • Loading branch information
okken committed Jan 8, 2024
2 parents b41cba7 + 904c84d commit 46ba797
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ All notable changes to this project be documented in this file.
-->

## [2.2.4] - 2024-Jan-*

- fix [153](https://github.com/okken/pytest-check/issues/153)
- Summaries from 2.2.3 are cool, but don't work for pytest < 7.3
- Remove them for earlier pytest
- Add tox test run for pytest 7.0.0
- Change dependencies to require 7.0.0 pytest


## [2.2.3] - 2023-Dec-31

- Check failure info now shows up in summaries.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ authors = [{name = "Brian Okken"}]
readme = "README.md"
license = {file = "LICENSE.txt"}
description="A pytest plugin that allows multiple failures per test."
version = "2.2.3"
version = "2.2.4"
requires-python = ">=3.7"
classifiers = [
"License :: OSI Approved :: MIT License",
"Framework :: Pytest" ,
]
dependencies = ["pytest"]
dependencies = ["pytest>=7.0.0"]

[project.urls]
Home = "https://github.com/okken/pytest-check"
Expand Down
36 changes: 25 additions & 11 deletions src/pytest_check/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,36 @@ 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


def pytest_configure(config):
# Add some red to the failure output, if stdout can accommodate it.
isatty = sys.stdout.isatty()
color = config.option.color
color = getattr(config.option, "color", None)
check_log.should_use_color = (isatty and color == "auto") or (color == "yes")

# If -x or --maxfail=1, then stop on the first failed check
Expand All @@ -69,9 +83,9 @@ def pytest_configure(config):
check_log._stop_on_fail = stop_on_fail

# Allow for --tb=no to turn off check's pseudo tbs
traceback_style = config.getvalue("tbstyle")
traceback_style = config.getoption("tbstyle", default=None)
pseudo_traceback._traceback_style = traceback_style
check_log._showlocals = config.getvalue('showlocals')
check_log._showlocals = config.getoption("showlocals", default=None)

# grab options
check_log._default_max_fail = config.getoption("--check-max-fail")
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 46ba797

Please sign in to comment.