m-burst / flake8-pytest-style

A flake8 plugin checking common style issues or inconsistencies with pytest-based tests.
MIT License
230 stars 16 forks source link

Rule to disallow assertions in fixtures #31

Open m-burst opened 4 years ago

m-burst commented 4 years ago

Rule request

Description

The rule should check that there are no assert statements in fixtures.

Rationale

sobolevn commented 4 years ago

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:

m-burst commented 4 years ago

@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.

sobolevn commented 4 years ago

Yes, this seems legit! 👍