flake8-pytest-style
flake8-pytest-style copied to clipboard
Rule to disallow assertions in fixtures
Rule request
Description
The rule should check that there are no assert statements in fixtures.
Rationale
- assertions in fixtures are a sign of bad test design, as the test logic becomes splattered between setup/teardown code and actual test code
- inspired by eslint-plugin-jasmine rule
no-expect-in-setup-teardown
I don't agree on this one. Many people use fixtures for custom complex asserts. I can even say that this is the best practice.
Examples:
- https://github.com/wemake-services/wemake-python-styleguide/blob/master/tests/test_visitors/conftest.py#L12-L37
- https://github.com/wemake-services/wemake-python-styleguide/blob/master/tests/test_visitors/conftest.py#L40-L66
@sobolevn These cases are not what I meant. If the fixture function returns a function which has some assertions in it, that is fine, since the function is called during test code execution and thus becomes part of the test code.
What I see as a violation is making assertions in the fixture function itself, i.e. during setup/teardown:
@pytest.fixture()
def my_thing():
thing = get_my_thing()
assert thing.some_attribute
return thing
In the above case, if the assertion on thing needs to be made, it should be a separate test case.
Yes, this seems legit! 👍