Skip to content
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

Open
dongfangtianyu opened this issue Nov 25, 2024 · 3 comments

Comments

@dongfangtianyu
Copy link
Contributor

dongfangtianyu commented Nov 25, 2024

When communicating with pytest beginners, it’s common to encounter code like this:

@pytest.fixture()
def test_login():
    driver = get_driver()
    page = pom.LoginPage(driver)
    page.login()
    return driver

def test_create_data(test_login):
    page = pom.HomePage(test_login)
    msg = page.send_data("./data.csv")
    assert 'ok' in msg

While the code meets functional requirements:

  1. executes login first, then executes create_data.
  2. Uses the same browser instance for all steps.

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:

  • Both are functions.
  • Both can use decorators.
  • Both can return values ( pytest <=8.3.3).
  • Both can use fixtures.

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.

@RonnyPfannschmidt
Copy link
Member

There's valid use cases for such fixture names

So I'm -1on warning for bad fixture names

I'd recommend a linter suggestion

@dongfangtianyu
Copy link
Contributor Author

dongfangtianyu commented Nov 25, 2024

Agree it's a valid fixture.
Propose PytestCollectionWarning, similar to when a class contains an __init__ method.

@dongfangtianyu
Copy link
Contributor Author

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

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

No branches or pull requests

2 participants