-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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.
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
Issue a Warning When Test Cases Use @pytest.fixture , Help Beginners Differentiate Between Cases and Fixtures #12989
Comments
There's valid use cases for such fixture names So I'm -1on warning for bad fixture names I'd recommend a linter suggestion |
Agree it's a valid fixture. |
https://github.com/pytest-dev/pytest/blob/main/src/_pytest/python.py#L361 add: if self.funcnamefilter(name) and fixtures.getfixturemarker(obj):
self.warn(PytestCollectionWarning(f" cannot collect test function '{name}' ,because it uses the decorator '@pytest.fixture' and becomes a fixture"))
return Fasle |
There's already a warning on parametrize on fixtures that should trigger |
After discussion here and in the PR I agree it's better suited for a linter, so I opened m-burst/flake8-pytest-style#330. If implemented there it will likely also be picked up by ruff, which should be widely used by beginners. |
When communicating with pytest beginners, it’s common to encounter code like this:
While the code meets functional requirements:
login
first, then executescreate_data
.It also confuses them: Why are there 2 test cases, but only 1 appears in the terminal and reports?
This confusion arises because in most tutorials, blogs, and examples, test cases and fixtures share similarities:
Currently, using
@pytest.mark
in fixtures raises a warning.Should we also issue a warning when
@pytest.fixture
is used for test cases?This could make it clearer that test cases and fixtures are fundamentally different, and their decorators are not interchangeable.
The text was updated successfully, but these errors were encountered: