Description
- a detailed description of the bug or problem you are having
- output of
pip list
from the virtual environment you are using - pytest and operating system versions
- minimal example if possible
Description
Pytest aborts if a session-scoped fixture errors during teardown when (a) one or more test cases fail and (b) --maxfail=1
is used.
When this happens, test-report.xml
does not contain the teardown fixture error. This is problematic as users may want to also record teardown fixture errors when their test cases fail.
Minimal Example
Code
import pytest
def some_condition() -> bool:
return True
@pytest.fixture(scope="session", autouse=True)
def my_end_of_session_check():
yield
print("Running end of session check!")
if some_condition():
pytest.fail("Oh noes")
def test_case_1():
pytest.fail("This is a failing test")
def test_case_2():
pytest.fail("This is also a failing test")
Args
pytest example.py --maxfail=1 --junitxml=test-report.xml
Output
Console:
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.8.13, pytest-7.4.3, pluggy-1.3.0
rootdir: /Users/bbrown/Downloads
collected 2 items
example.py FRunning end of session check!
Traceback (most recent call last):
File "/Users/bbrown/Downloads/.venv/bin/pytest", line 8, in <module>
sys.exit(console_main())
<clipped>
File "/Users/bbrown/Downloads/.venv/lib/python3.8/site-packages/_pytest/fixtures.py", line 911, in _teardown_yield_fixture
next(it)
File "/Users/bbrown/Downloads/example.py", line 14, in my_end_of_session_check
pytest.fail("Oh noes")
File "/Users/bbrown/Downloads/.venv/lib/python3.8/site-packages/_pytest/outcomes.py", line 198, in fail
raise Failed(msg=reason, pytrace=pytrace)
Failed: Oh noes
test-report.xml
:
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="1" skipped="0" tests="1" time="0.035" timestamp="2023-12-15T08:56:19.980572" hostname="macbookpro"><testcase classname="example" name="test_case_1" time="0.001"><failure message="Failed: This is a failing test">def test_case_1():
> pytest.fail("This is a failing test")
E Failed: This is a failing test
example.py:18: Failed</failure></testcase></testsuite></testsuites>
Expected Output
I expect pytest to not abort, and report "1 failed, 1 error, 1 skipped" in both the console and test-report.xml
when this happens.
Environment
OS:
macOS Sonoma 14.1.2
Virtual environment:
(.venv) % pip list
Package Version
-------------- -------
exceptiongroup 1.2.0
iniconfig 2.0.0
packaging 23.2
pip 23.3.1
pluggy 1.3.0
pytest 7.4.3
setuptools 56.0.0
tomli 2.0.1