astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.52k stars 1.08k forks source link

Linter request: pytest "usefixture" mark should be flagged #13795

Open lengau opened 2 weeks ago

lengau commented 2 weeks ago

I've seen multiple cases where a test used pytest.mark.usefixture rather than pytest.mark.usefixtures in ways that caused the tests to only sometimes fail (e.g. only on certain platforms). It would be handy to have a linter that changed this.

E.g. the following code:

@pytest.mark.usefixture("side_effect_fixture")
def test_my_function():
    assert my_function() == True

could be (probably with an unsafe marker?) converted to:

@pytest.mark.usefixtures("side_effect_fixture")
def test_my_function():
    assert my_function() == True
MichaReiser commented 2 weeks ago

Do you have a reference to what pytest.mark.usefixture? I was only able to find the documentation for pytest.mark.usefixtures and it's not clear to me what the difference between the two is (I never used pytest)

autinerd commented 2 weeks ago

@pytest.mark.usefixture is almost always a typo and should emit a pytest warning. (but technically, a plugin could register the usefixture mark and do something with it)

Could you run pytest <path_to_file> --markers to check if usefixture is really a registered marker in your context?

sbrugman commented 1 week ago

Real-world examples:

Reference issue at the pytest repository: https://github.com/pytest-dev/pytest/issues/3972