找不到夹具 pytest
Fixture not found pytest
嘿,我做了一个简单的测试,但没有找到修复程序。我在 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
添加到非测试方法,以便测试可以使用该夹具。代码可能如下所示:
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
应用于 non-test 方法)。然后,第三种方法使用 graph
fixture。根据需要进行任何其他更改。
嘿,我做了一个简单的测试,但没有找到修复程序。我在 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
添加到非测试方法,以便测试可以使用该夹具。代码可能如下所示:
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
应用于 non-test 方法)。然后,第三种方法使用 graph
fixture。根据需要进行任何其他更改。