gpt4 book ai didi

python - pytest fixture 的 fixture ,未找到

转载 作者:行者123 更新时间:2023-12-04 15:39:51 26 4
gpt4 key购买 nike

基于此堆栈溢出:pytest fixture of fixtures

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

@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 个文件中有测试和所有 fixture :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 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 ======================================


要解决这个问题,还要导入 foo test_foo_bar.py 中的固定装置模块。

关于python - pytest fixture 的 fixture ,未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47402435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com