假设以下结构:
class SetupTestParam(object):
def setup_method(self, method):
self.foo = bar()
@pytest.fixture
def some_fixture():
self.baz = 'foobar'
我SetupTestParam
用作测试类的父类。
class TestSomething(SetupTestParam):
def test_a_lot(self, some_fixture):
with self.baz as magic:
with magic.fooz as more_magic:
blah = more_magic.much_more_magic() # repetative bleh
... # not repetative code here
assert spam == 'something cool'
现在,编写测试变得重复(使用语句),我想编写一个装饰器来减少代码行数。但是 pytest 和函数签名存在问题。
我发现了应该有用的库,但我无法让它工作。
我classmethod
在SetupTestParam
课堂上做了一个。
@classmethod
@decorator.decorator
def this_is_decorator(cls, f):
def wrapper(self, *args, **kw):
with self.baz as magic:
with magic.fooz as more_magic:
blah = more_magic.much_more_magic() # repetative bleh
return f(self, *args)
return wrapper
装饰test_a_lot
方法后,我收到错误TypeError: transaction_decorator() takes exactly 1 argument (2 given)
有人可以解释一下我在做什么错吗?(我假设测试方法有问题self
?)