【发布时间】:2017-04-13 11:48:44
【问题描述】:
def register_processor2(processor_name='SomeProcessor'):
def decorator(func):
class SomeProcessor(GenericPaymentProcessor, TriggeredProcessorMixin):
name = processor_name
transaction_class = Transaction
@staticmethod
def setup(data=None):
pass
@wraps(func)
def func_wrapper(*args, **kwargs):
PaymentProcessorManager.register(SomeProcessor)
result = func(*args, **kwargs)
PaymentProcessorManager.unregister(SomeProcessor)
return result
return func_wrapper
return decorator
def register_processor(func):
class SomeProcessor(GenericPaymentProcessor, TriggeredProcessorMixin):
name = 'SomeProcessor'
transaction_class = Transaction
@staticmethod
def setup(data=None):
pass
@wraps(func)
def func_wrapper(*args, **kwargs):
PaymentProcessorManager.register(SomeProcessor)
result = func(*args, **kwargs)
PaymentProcessorManager.unregister(SomeProcessor)
return result
return func_wrapper
class TestPaymentMethodEndpoints(APITestCase):
@register_processor
def test_put_detail_cannot_change_processor(self):
self.assertEqual(True, False)
好的,所以装饰器register_processor 按预期工作。并且测试失败了,但是我想让内部类的名称可以自定义,所以我选择了一个装饰器工厂实现。
问题是在运行带有register_processor2 装饰的测试时,我得到以下信息:
AttributeError: 'TestPaymentMethodEndpoints' object has no attribute '__name__'
这是来自@wraps(func),我的问题是为什么func 这里是TestPaymentMethodEndpoints 的实例,而不是绑定方法?
此外,如果我删除 @wraps 装饰器,那么 测试会运行并通过。
我希望不会发现测试,因为 func_wrapper 不是以 test_* 开头的,即使发现它也应该失败。
对正在发生的事情以及我将如何做这件事有任何见解吗?
编辑
所以我想通了,即使装饰器工厂的参数具有默认值,您仍然需要在调用它时放置 ()。
但仍然希望听到有关测试通过/被发现时发生的情况的解释。
class TestPaymentMethodEndpoints(APITestCase):
@register_processor()
def test_put_detail_cannot_change_processor(self):
self.assertEqual(True, False)
现在想想就明白了:D,天哪,你每天都能学到新东西!
【问题讨论】:
标签: python python-unittest python-decorators