Skip to content

Conversation

andrewwillowen
Copy link

I stumbled across this issue when trying to write some tests and took a while to figure out the issue.
Based on #13409, it sounds like this is expected behavior.

This PR adds a warning to https://docs.pytest.org/en/stable/how-to/parametrize.html so that hopefully other people don't waste cycles debugging this behavior.

andrewwillowen and others added 2 commits September 21, 2025 13:00
Add warning about using generators as the value argument
@bluetech
Copy link
Member

Hi @andrewwillowen,

Your case seems different from @steadyfirmness's case which was running pytest.main() twice (a rarer case than yours, but still should be perfectly supported). Can you give an example demonstrating your case, just so we're clear about it?

@andrewwillowen
Copy link
Author

Taking the example from the documentation.
Modify the list to use a generator instead.
The second test will be skipped because the generator has been exhausted.

test.py

import pytest

@pytest.mark.parametrize("n,expected", (i for i in [(1, 2), (3, 4)]))
class TestClass:
    def test_simple_case(self, n, expected):
        assert n + 1 == expected

    def test_weird_simple_case(self, n, expected):
        assert (n * 1) + 1 == expected

Then run

pytest -rs ./test.py
======================= test session starts ========================
platform linux -- Python 3.13.1, pytest-8.4.2, pluggy-1.6.0
rootdir: xxxxx
configfile: pyproject.toml
plugins: cov-7.0.0, hypothesis-6.138.15
collected 3 items

test.py ..s                                                  [100%]

===================== short test summary info ======================
SKIPPED [1] test.py: got empty parameter set for (n, expected)
=================== 2 passed, 1 skipped in 0.24s ===================

@bluetech
Copy link
Member

Thanks. In order to keep the discussion in one place, I'll comment further in the issue #13409.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants