I am trying to use py.test fixtures with my unit tests in combination with unittest . I placed several elements in the conftest.py file at the top level of the project (as described here ), decorated them with @pytest.fixture and put their names as arguments for test functions that require them.
Records are registered correctly, as shown by py.test --fixtures test_stuff.py , but when I run py.test , I get NameError: global name 'my_fixture' is not defined . This only happens when I use subclasses of unittest.TestCase , but the py.test seem to say that it works well with unittest .
Why are tests not visible when I use unittest.TestCase ?
Does not work:
conftest.py
@pytest.fixture def my_fixture(): return 'This is some fixture data'
test_stuff.py
import unittest import pytest class TestWithFixtures(unittest.TestCase): def test_with_a_fixture(self, my_fixture): print(my_fixture)
Working:
conftest.py
@pytest.fixture() def my_fixture(): return 'This is some fixture data'
test_stuff.py
import pytest class TestWithFixtures: def test_with_a_fixture(self, my_fixture): print(my_fixture)
I ask this question more out of curiosity; while I'm just unittest .
Will
source share