GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — use Contextmanagers to handle StopIteration in generators by RonnyPfannschmidt · Pull Request #12934 · pytest-dev/pytest

use Contextmanagers to handle StopIteration in generators #12934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
graingert merged 13 commits into pytest-dev:main from RonnyPfannschmidt:bug-stopiteration-hook
May 31, 2025
+54 −3
Merged

use Contextmanagers to handle StopIteration in generators #12934

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
040b787
prepare example test for stopiteration passover issue
RonnyPfannschmidt Nov 3, 2024
24b1cd8
WIP: use contextmanagers instead of yield from
RonnyPfannschmidt Nov 3, 2024
4f9cf8d
fixup rebase
RonnyPfannschmidt Mar 27, 2025
c69bb2a
rebase fixup more
RonnyPfannschmidt Mar 27, 2025
ccaf348
Merge branch 'main' into bug-stopiteration-hook
graingert May 29, 2025
1e67ca5
add tests
graingert May 31, 2025
1701ce1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 31, 2025
d6d20a5
remove hook_exceptions examples - these are tests now
graingert May 31, 2025
e6211c2
add changelog
graingert May 31, 2025
08c56c4
Merge branch 'main' into bug-stopiteration-hook
graingert May 31, 2025
a617f94
handle different numbers of ===
graingert May 31, 2025
7e3f5e1
handle different numbers of !!!
graingert May 31, 2025
54b2f67
Merge branch 'main' into bug-stopiteration-hook
graingert May 31, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 changelog/12929.bugfix.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle StopIteration from test cases, setup and teardown correctly.
10 changes: 7 additions & 3 deletions 10 src/_pytest/logging.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ def pytest_runtest_logstart(self) -> None:
def pytest_runtest_logreport(self) -> None:
self.log_cli_handler.set_when("logreport")

@contextmanager
def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None]:
"""Implement the internals of the pytest_runtest_xxx() hooks."""
with (
Expand Down Expand Up @@ -838,20 +839,23 @@ def pytest_runtest_setup(self, item: nodes.Item) -> Generator[None]:

empty: dict[str, list[logging.LogRecord]] = {}
item.stash[caplog_records_key] = empty
yield from self._runtest_for(item, "setup")
with self._runtest_for(item, "setup"):
yield

@hookimpl(wrapper=True)
def pytest_runtest_call(self, item: nodes.Item) -> Generator[None]:
self.log_cli_handler.set_when("call")

yield from self._runtest_for(item, "call")
with self._runtest_for(item, "call"):
yield

@hookimpl(wrapper=True)
def pytest_runtest_teardown(self, item: nodes.Item) -> Generator[None]:
self.log_cli_handler.set_when("teardown")

try:
yield from self._runtest_for(item, "teardown")
with self._runtest_for(item, "teardown"):
yield
finally:
del item.stash[caplog_records_key]
del item.stash[caplog_handler_key]
Expand Down
46 changes: 46 additions & 0 deletions 46 testing/acceptance_test.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1606,3 +1606,49 @@ def test_no_terminal_plugin(pytester: Pytester) -> None:
pytester.makepyfile("def test(): assert 1 == 2")
result = pytester.runpytest("-pno:terminal", "-s")
assert result.ret == ExitCode.TESTS_FAILED


def test_stop_iteration_from_collect(pytester: Pytester) -> None:
pytester.makepyfile(test_it="raise StopIteration('hello')")
result = pytester.runpytest()
assert result.ret == ExitCode.INTERRUPTED
result.assert_outcomes(failed=0, passed=0, errors=1)
result.stdout.fnmatch_lines(
[
"=* short test summary info =*",
"ERROR test_it.py - StopIteration: hello",
"!* Interrupted: 1 error during collection !*",
"=* 1 error in * =*",
]
)


def test_stop_iteration_runtest_protocol(pytester: Pytester) -> None:
pytester.makepyfile(
test_it="""
import pytest
@pytest.fixture
def fail_setup():
raise StopIteration(1)
def test_fail_setup(fail_setup):
pass
def test_fail_teardown(request):
def stop_iteration():
raise StopIteration(2)
request.addfinalizer(stop_iteration)
Comment thread
graingert marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

def test_fail_call():
raise StopIteration(3)
"""
)
result = pytester.runpytest()
assert result.ret == ExitCode.TESTS_FAILED
result.assert_outcomes(failed=1, passed=1, errors=2)
result.stdout.fnmatch_lines(
[
"=* short test summary info =*",
"FAILED test_it.py::test_fail_call - StopIteration: 3",
"ERROR test_it.py::test_fail_setup - StopIteration: 1",
"ERROR test_it.py::test_fail_teardown - StopIteration: 2",
"=* 1 failed, 1 passed, 2 errors in * =*",
]
)
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.