Python pytest:使用fixture参数化基于类的测试(pytest django)

Python pytest:使用fixture参数化基于类的测试(pytest django),python,pytest,pytest-django,Python,Pytest,Pytest Django,我正在尝试将我的类测试参数化,如下所示: @pytest.mark.parametrize('current_user', ["test_profile_premium", "test_profile_free"], indirect=True) class TestFeedItemsType: @pytest.fixture(autouse=True) def setup(self, current_user, logged_in_client, dummy_object):

我正在尝试将我的类测试参数化,如下所示:

@pytest.mark.parametrize('current_user', ["test_profile_premium", "test_profile_free"], indirect=True)
class TestFeedItemsType:

    @pytest.fixture(autouse=True)
    def setup(self, current_user, logged_in_client, dummy_object):
        self.client = logged_in_client
        self.test_profile = current_user
        self.object = dummy_object
但是,我得到了一个错误:

未找到设备“当前用户”


test\u profile\u premium
test\u profile\u free
都是
conftest.py
中现有的有效装置。我需要此基于类的套件中的所有函数(测试)同时针对
test\u profile\u premium
test\u profile\u free

无法将夹具作为参数化参数传递,有关详细信息,请参阅。作为一种解决方法,在您的示例中,您可以引入一个
当前用户
夹具,该夹具根据夹具名称执行夹具选择:

import pytest


@pytest.fixture
def current_user(request):
    return request.getfixturevalue(request.param)


@pytest.fixture
def test_profile_premium():
    return "premiumfizz"


@pytest.fixture
def test_profile_free():
    return "freefizz"


@pytest.mark.parametrize('current_user', ["test_profile_premium", "test_profile_free"], indirect=True)
class TestFeedItemsType:

    @pytest.fixture(autouse=True)
    def setup(self, current_user):
        self.test_profile = current_user

    def test_spam(self):
        assert self.test_profile in ("premiumfizz", "freefizz")

    def test_eggs(self):
        assert self.test_profile in ("premiumfizz", "freefizz")
运行此示例将产生四个测试:

test_spam.py::TestFeedItemsType::test_spam[test_profile_premium] PASSED
test_spam.py::TestFeedItemsType::test_spam[test_profile_free] PASSED
test_spam.py::TestFeedItemsType::test_eggs[test_profile_premium] PASSED
test_spam.py::TestFeedItemsType::test_eggs[test_profile_free] PASSED
非常感谢。这和我最后做的很接近。我需要在参数化中定义
预期的
结果,所以我使用元组,然后使用
间接=[“profile”]