Search code examples
pythonpython-3.xpytestpython-unittestfixtures

How to use fixtures in a Class which is inherting UnitTest class in python pytest


conftest.py

import pytest

@pytest.fixture(scope="session")
def client():
    env_name = 'FLASK_ENV'
    return env_name

@pytest.fixture(scope="session")
def client_1():
    env_name = 'FLASK_ENV_1'
    return env_name

test_run.py

import pytest
import unittest

@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("client_1")
class TestStaticPages(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.value = "ABC"

    def test_base_route(self, client, client_1):
        response = client
        assert response  == 'FLASK_ENV'
        assert client_1  == 'FLASK_ENV_1'

I am new to UnitTest in python. When I am trying to use fixtures in the test class it is failing. Could you please help me with the solution. Thanks in advance.enter image description here


Solution

  • Unfortunately, I don't think there is a way to do this. In the pytest documentation, it is stated that:

    unittest.TestCase methods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.

    So your client and client_1 functions can be called when the test case is run, but you can't get access to their return values.