pytest-dev / pytest-dev/pytest

Improve (beginner) UX around self vs. fixtures

Open
#12,907 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Something I see sometimes in my pytest company trainings is people accidentally doing:

def test_something(self):
    ...

because they're using to writing things in classes (e.g. from Java?). That results in a

E       fixture 'self' not found
>       available fixtures: [quite a long list here]
>       use 'pytest --fixtures [testpath]' for help on them.

which is not exactly useful if you don't know yet what a fixture is. IMHO, we should detect the self name here, and output an additional hint ("hint: Remove self as this is not a method?" or somesuch).


Similarly, when people start moving tests from functions to classes, they forget to add self, which can be very confusing in combination with fixtures:

class TestSomething:

    def test_x(monkeypatch):
        monkeypatch.setenv("DEBUG", "true")

results in:

    def test_x(monkeypatch):
>       monkeypatch.setenv("DEBUG", "true")
E       AttributeError: 'TestSomething' object has no attribute 'setenv'

which is still somewhat clear, but depending on what you do with a fixture (e.g. passing data to a function under test), it can be quite confusing if you've never seen this kind of message before.

Maybe a bit more tricky here, but it would be nice if pytest could detect that we're in a test method, the first argument is not self, and the first argument is a valid fixture name. If all of those are true, that seems to be worth warning about as well.

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

No files or tests are named in the issue. Start by tracing pytest's fixture lookup and test-method argument handling, then add coverage for both shown examples; done means actionable hints appear for mistaken self usage without changing valid fixture behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
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.