Emit warning for shadowed tests #12794
-
Functions that are redefined are not detected by pytest. It would be nice if pytest would emit a warning (or an error!) in these cases, or simply allow multiple tests with the same name.
def test_bool():
assert False
def test_bool():
assert False
def test_bool():
assert True Output of
Posting here rather than feature suggestions because I'm not sure if there is some flag or plugin that enables this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The problem is that pytest cannot trivially do this: Outside of inspecting the ast, there is no way for pytest to find out about this - at runtime, pytest only sees the last function, because a function definition is basically just an assignment. However, there are already plenty of linters that do detect this via the AST (ruff, mypy, flake8, pylint, your IDE probably, ...). I don't think there is much value in pytest adding all this complexity (even though I agree this is a common pitfall), since I'd expect a typical Python development environment to already take care of this. |
Beta Was this translation helpful? Give feedback.
The problem is that pytest cannot trivially do this: Outside of inspecting the ast, there is no way for pytest to find out about this - at runtime, pytest only sees the last function, because a function definition is basically just an assignment.
However, there are already plenty of linters that do detect this via the AST (ruff, mypy, flake8, pylint, your IDE probably, ...). I don't think there is much value in pytest adding all this complexity (even though I agree this is a common pitfall), since I'd expect a typical Python development environment to already take care of this.