[英]Pytest Django Discovers Fixture but Says "Not Found" on the Run
我有一个项目,其中每个应用程序都有一个tests
模块,如下所示:
app1/
tests/
__init__.py
# whatever test files are
app2/
tests/
__init__.py
# whatever test files are
并且在项目根目录中也有项目包,您可以猜到。 它有一个testing
子包,其中包含模型和字段的基本测试用例,当然还有如下固定装置:
myproject/
__init__.py
# settings and related files, you know
testing/
__init__.py
fixtures.py # for fixtures
# other modules for base test classes and stuff
所以,我的整体项目结构是:
myproject/ # contains standard django stuff and a testing subpackage
app1/ # contains app and tests
app2/ # so as above
# so on and so forth
项目根目录中的pytest.ini
内容如下:
[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings.development
python_files =
test_*.py
myproject/testing/fixtures.py
addopts = -s
console_output_style = count
log_cli_level = DEBUG
log_print = True
因此,假设myproject/testing/fixtures.py
包含一个夹具,如下所示:
# assuming i've imported related stuff
@pytest.fixture # might have different scope
def foo():
"""does foo"""
return "foo"
稍后,我执行pytest --fixtures
以查看 pytest 是否正确地发现了我的夹具,并且确实如此,这意味着应该没有问题。
但是,当我在项目中运行测试时,出现以下错误:
E fixture 'foo' not found
这很奇怪,因为我可以在pytest --fixtures
清楚地看到它。 我不知道为什么会发生这种情况或导致这种情况的原因。
pytest-django
3.4.8如果你有固定装置应该保存在一个单独的模块中并且可以被整个测试套件访问,最好将模块注册为插件以确保它在测试收集阶段之前加载。 在项目根目录中创建一个名为conftest.py
的文件,内容为
pytest_plugins = ['myproject.testing.fixtures']
当然,您可以只使用conftest.py
而不是fixtures.py
并将fixtures.py
放在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.