minimum example:
import unittest
import pytest
xy1 = (1, 2)
xy2 = (3, 4)
@pytest.fixture(scope="class", params=[xy1, xy2])
def xy(request):
request.cls.x, request.cls.y = request.param
@pytest.mark.usefixtures("xy")
class TestClassTestCase(unittest.TestCase): # Failed: The requested fixture has no parameter defined for the current test.
def test_method1(self):
print('access fixture attributes', self.x, self.y)
@pytest.mark.usefixtures("xy")
class TestClassObject(object): # ok!
def test_method1(self):
print('access fixture attributes', self.x, self.y)
motivation of such example:
pytest allows @pytest.mark.parametrize decorated on test functions on module level. One could have developed tests as
import pytest
xy1 = (1, 2)
xy2 = (3, 4)
xy = [xy1, xy2]
@pytest.mark.parametrize('x,y', xy)
def test_fixture(x, y):
print('access fixture attributes', x, y)
my incentive on applying fixture onto class level is to leverage predefined test methods in super class, such as assertRaises in unittest.TestClass. if @pytest.mark.usefixtures cannot decorate on general classes, the use of it is less obvious, as @pytest.mark.parametrize functions the same and easier to write.
minimum example:
motivation of such example:
pytest allows @pytest.mark.parametrize decorated on test functions on module level. One could have developed tests as
my incentive on applying fixture onto class level is to leverage predefined test methods in super class, such as assertRaises in unittest.TestClass. if @pytest.mark.usefixtures cannot decorate on general classes, the use of it is less obvious, as @pytest.mark.parametrize functions the same and easier to write.