【发布时间】:2013-05-28 14:14:57
【问题描述】:
假设如下结构:
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和函数签名有问题。
我发现 library 应该会有所帮助,但我无法让它发挥作用。
我在SetupTestParam 班级中创建了一个classmethod。
@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 有问题?)
【问题讨论】: