Приспособление не найдено 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
применяется к нетестовому методу). Затем, 3-й метод использует приспособление graph
. Внесите любые другие изменения по мере необходимости.