Why can not unittest.TestCases see my py.test tools?

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 .

+7
python unit-testing python-unittest fixtures pytest
source share
1 answer

Although pytest supports retrieving fixtures via test function arguments for non-unit test methods, unittest.TestCase methods cannot directly receive fixture function arguments as an implementation that may affect the ability to run common unittest.TestCase test packages.

From the notes section below: https://pytest.org/en/latest/unittest.html

You can use lights with unittest.TestCase es. See this page for more information.

+7
source share

All Articles