pytest-dev / pytest-dev/pytest

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

Open
#12,989 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing pytest’s existing warning for using @pytest.mark on fixtures. Determine how pytest distinguishes test cases from fixtures and define when applying @pytest.fixture to a test-like function should warn. Add focused coverage for the warning and for valid fixtures that must remain silent.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.