Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cant unittest.TestCases see my py.test fixtures?

I'm trying to use py.test's fixtures with my unit tests, in conjunction with unittest. I've put several fixtures in a conftest.py file at the top level of the project (as described here), decorated them with @pytest.fixture, and put their names as arguments to the test functions that require them.

The fixtures are registering 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 appears to only occur when I use subclasses of unittest.TestCase—but the py.test docs seem to say that it plays well with unittest.

Why can't the tests see the fixtures when I use unittest.TestCase?


Doesn't 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)

Works:

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'm asking this question more out of curiosity; for now I'm just ditching unittest altogether.

like image 325
Will Avatar asked Mar 27 '14 03:03

Will


People also ask

Can you use pytest fixtures with Unittest?

pytest supports running Python unittest -based tests out of the box. It's meant for leveraging existing unittest -based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features.

Where do I put pytest fixtures?

Fixtures and their visibility are a bit odd in pytest. They don't require importing, but if you defined them in a test_*. py file, they'll only be available in that file. You can however put them in a (project- or subfolder-wide) conftest.py to use them in multiple files.

How do you use pytest fixtures?

To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter. It then executes the fixture function and the returned value is stored to the input parameter, which can be used by the test.


2 Answers

While pytest supports receiving fixtures via test function arguments for non-unittest test methods, unittest.TestCase methods cannot directly receive fixture function arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.

From the note section at the bottom of: https://pytest.org/en/latest/unittest.html

It's possible to use fixtures with unittest.TestCasees. See that page for more information.

like image 112
Frank T Avatar answered Nov 05 '22 20:11

Frank T


You can use pytest fixtures in unittest.TestCase with pytest autouse :

conftest.py

@pytest.fixture
def my_fixture():
    return 'This is some fixture data'

test_stuff.py

import unittest
import pytest

class TestWithFixtures(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def test_with_a_fixture(self, my_fixture):
         print(my_fixture)

like image 26
Radhwane Chebaane Avatar answered Nov 05 '22 19:11

Radhwane Chebaane