pytest:参数化 class 基于固定装置的测试(pytest-django)
pytest: parametrize class based tests with fixtures (pytest-django)
我正在尝试像这样参数化我的 class 测试:
@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
但是,我遇到了错误:
fixture 'current_user' not found
test_profile_premium
和 test_profile_free
都是 conftest.py
中现有的有效灯具。我需要这个基于 class 的套件中的所有功能(测试)到 运行 针对 test_profile_premium
和 test_profile_free
.
您不能将灯具作为参数化参数传递,有关详细信息,请参阅 open issue #349。作为解决方法,在您的示例中,您可以引入一个 current_user
夹具,它根据夹具名称执行夹具选择:
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