我正在使用 pytest 的参数化注释将参数传递给一个类。我可以在测试方法中使用参数,但是,我不知道如何在 setup_class 方法中使用参数。
import pytest
params = ['A','B','C']
@pytest.mark.parametrize('n', params)
class TestFoo:
def setup_class(cls):
print ("setup class:TestFoo")
# Do some setup based on param
def test_something(self, n):
assert n != 'D'
def test_something_else(self, n):
assert n != 'D'
我尝试像测试方法一样添加'n'作为参数,如下所示:
def setup_class(cls, n):
print ("setup class:TestFoo")
# Do some setup based on param
这会导致错误:
self = <Class 'TestFoo'>
def setup(self):
setup_class = xunitsetup(self.obj, 'setup_class')
if setup_class is not None:
setup_class = getattr(setup_class, 'im_func', setup_class)
setup_class = getattr(setup_class, '__func__', setup_class)
> setup_class(self.obj)
E TypeError: setup_class() takes exactly 2 arguments (1 given)
在 setup_class 方法中是否有其他使用参数的方法?