Python 未找到夹具的pytest夹具

Python 未找到夹具的pytest夹具,python,pytest,Python,Pytest,基于此stackoverflow: 我在同一个文件中有以下代码: @pytest.fixture def form_data(): return { ... } @pytest.fixture def example_event(form_data): return {... 'data': form_data, ... } 但当我运行pytest时,它抱怨没有找到fixture'form\u data 我是pytest新手,所以我甚至不确定这是否可行?是的,这是可能的 如果

基于此stackoverflow:

我在同一个文件中有以下代码:

@pytest.fixture
def form_data():
    return { ... }

@pytest.fixture
def example_event(form_data):
    return {... 'data': form_data, ... }
但当我运行pytest时,它抱怨没有找到
fixture'form\u data

我是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
from test import bar

def test_foo_bar(bar):
    expected = ("foo", "bar")
    assert bar == expected
然后运行
pytest.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\u foo\u bar.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
from test import bar

def test_foo_bar(bar):
    expected = ("foo", "bar")
    assert bar == expected
然后运行
pytest test\u foo\u bar.py
期望(就像我做的那样)只导入
bar
fixture就足够了,因为导入时它已经执行了
foo
fixture,然后您就会得到所得到的错误

======================================= 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\u foo\u bar.py
模块中导入
foo
夹具

您能否在使用fixture的位置共享最终测试的代码
示例_事件
?值得一提的是:pytest支持一个
conftest.py
文件来跨文件共享fixture,请参阅