pytest-fixture-param-without-value (PT019)
源自 flake8-pytest-style linter。
作用
检查应使用 @pytest.mark.usefixtures
装饰器修饰的 pytest
测试函数。
为什么这不好?
在 pytest
中,fixture 注入用于在测试函数中激活 fixture。
fixture 可以通过将其作为参数传递给测试函数,或使用 @pytest.mark.usefixtures
装饰器来注入。
如果测试函数依赖于 fixture 被激活,但不在测试主体中使用它或以其他方式依赖于其返回值,请首选 @pytest.mark.usefixtures
装饰器,以使依赖关系明确并避免未使用参数造成的混淆。
示例
建议改为
import pytest
@pytest.fixture
def _patch_something(): ...
@pytest.mark.usefixtures("_patch_something")
def test_foo(): ...