问题描述
我们正在使用flake8
来测试我们的代码,并且我们正在将pytest
与固定装置一起使用.以下代码:
We're using flake8
to test our code, and we're using pytest
with fixtures. The following code:
from staylists.tests.fixtures import fixture1 # noqa: F401
def test_case(fixture1): # noqa: F811
# Test goes here
assert 1 == 1
在掉毛过程中生成lib/python/test.py:3:1: F811 redefinition of unused 'fixture1' from line 1
错误.
Generates a lib/python/test.py:3:1: F811 redefinition of unused 'fixture1' from line 1
error during linting.
- 为什么它会忽略noqa标志?
- 是否有更好的方法来避免标记此错误?
- Why does it ignore the noqa flag?
- Is there a better way to avoid flagging this error?
推荐答案
通过将所有灯具移至 conftest.py 文件中,可以避免F401和F811错误. Pytest会自动加载此文件,并使所有测试中的所有固定装置都可用,即使没有显式导入语句也是如此.
The F401 and F811 errors can be avoided by moving all fixtures into the conftest.py file. Pytest loads this file automatically and makes all fixtures inside available in all tests, even without explicit import statements.
有关文件的更多讨论可以在这里找到:
More discussion about the file can be found here: In py.test, what is the use of conftest.py files?
这篇关于如何使Flake8出现F811错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!