flake8-pytest-style icon indicating copy to clipboard operation
flake8-pytest-style copied to clipboard

Rule to disallow assertions in fixtures

Open m-burst opened this issue 5 years ago • 3 comments

Rule request

Description

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

Rationale

m-burst avatar May 07 '20 19:05 m-burst

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 avatar May 07 '20 20:05 sobolevn

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

m-burst avatar May 07 '20 20:05 m-burst

Yes, this seems legit! 👍

sobolevn avatar May 07 '20 20:05 sobolevn