[英]pytest fixture of fixture, not found
基于此 stackoverflow: 夹具的 pytest 夹具
我在同一个文件中有以下代码:
@pytest.fixture
def form_data():
return { ... }
@pytest.fixture
def example_event(form_data):
return {... 'data': form_data, ... }
但是当我运行 pytest 时,它抱怨fixture 'form_data' not found
我是 pytest 的新手,所以我什至不确定这是否可能?
对的,这是可能的。
如果您在 1 个文件中包含测试和所有装置: test.py
import pytest
@pytest.fixture
def foo():
return "foo"
@pytest.fixture
def bar(foo):
return foo, "bar"
def test_foo_bar(bar):
expected = ("foo", "bar")
assert bar == expected
并运行pytest test.py
然后成功!!!
======================================= test session starts ========================================
platform darwin -- Python 3.6.8, pytest-4.3.0
collected 1 item
test.py . [100%]
===================================== 1 passed in 0.02 seconds =====================================
但是如果你把夹具放在不同的文件中: test_foo_bar.py
from test import bar
def test_foo_bar(bar):
expected = ("foo", "bar")
assert bar == expected
并运行pytest test_foo_bar.py
期望(像我一样)只导入bar
夹具就足够了,因为在导入时它已经执行了foo
夹具,然后你会得到你得到的错误。
======================================= test session starts ========================================
platform darwin -- Python 3.6.8, pytest-4.3.0
collected 1 item
test2.py E [100%]
============================================== ERRORS ==============================================
__________________________________ ERROR at setup of test_foo_bar __________________________________
file .../test_foo_bar.py, line 3
def test_foo_bar(bar):
.../test.py, line 7
@pytest.fixture
def bar(foo):
E fixture 'foo' not found
> available fixtures: TIMEOUT, bar, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, once_without_docker, pytestconfig, record_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
.../test.py:7
===================================== 1 error in 0.03 seconds ======================================
要解决此问题,还要在test_foo_bar.py
模块中导入foo
夹具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.