Skip to content

Issue a Warning When Test Cases Use @pytest.fixture , Help Beginners Differentiate Between Cases and Fixtures #12989

Open
@dongfangtianyu

Description

@dongfangtianyu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions