Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixture not found pytest

Hey I got a simple test where the fixure is not found. I am writting in vsc and using windows cmd to run 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.

This is the error I get, here is the test code:

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'

im using python 3.9.2, pytest 6.2.5. I have see some similar questions but they all handle wider or bigger problems.

like image 334
Pelle Martin Hesketh Avatar asked Sep 12 '25 19:09

Pelle Martin Hesketh


1 Answers

You appear to be defining test_graph twice, which means that the second definition will overwrite the first. And you added @pytest.fixture to a test_ method when you used it, but @pytest.fixture should be added to non test methods so that tests can use that fixture. Here's how the code should probably look:

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'

Above, the first method has been renamed to graph so that the next method doesn't override it (and now @pytest.fixture is applied to a non-test method). Then, the 3rd method uses the graph fixture. Make any other changes as needed.

like image 111
Michael Mintz Avatar answered Sep 15 '25 09:09

Michael Mintz