gpt4 book ai didi

python-3.x - 找不到 fixture pytest

转载 作者:行者123 更新时间:2023-12-05 03:30:33 26 4
gpt4 key购买 nike

嘿,我做了一个简单的测试,但没有找到修复程序。我在 vsc 中编写并使用 windows cmd 运行 pytest。

def test_graph_add_node(test_graph):
E fixture 'test_graph' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.

这是我得到的错误,这是测试代码:

import pytest
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'giddeon1.settings')
import django
django.setup()

from graphs.models import Graph, Node, Tag


@pytest.fixture
def test_graph():
graph = Graph.objects.get(pk='74921f18-ed5f-4759-9f0c-699a51af4307')
return graph

def test_graph():
new_graph = Graph()
assert new_graph

def test_graph_add_node(test_graph):
assert test_graph.name == 'Test1'

我使用的是 python 3.9.2,pytest 6.2.5。我见过一些类似的问题,但它们都处理更广泛或更大的问题。

最佳答案

您似乎定义了两次 test_graph,这意味着第二个定义将覆盖第一个。并且你在使用它时将 @pytest.fixture 添加到 test_ 方法,但是 @pytest.fixture 应该添加到非测试方法所以该测试可以使用该 fixture 。代码可能如下所示:

import pytest
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'giddeon1.settings')
import django
django.setup()

from graphs.models import Graph, Node, Tag


@pytest.fixture
def graph():
graph = Graph.objects.get(pk='74921f18-ed5f-4759-9f0c-699a51af4307')
return graph

def test_graph():
new_graph = Graph()
assert new_graph

def test_graph_add_node(graph):
assert graph.name == 'Test1'

上面,第一个方法已重命名为 graph,以便下一个方法不会覆盖它(现在 @pytest.fixture 应用于非测试方法)。然后,第三种方法使用 graph fixture。根据需要进行任何其他更改。

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

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