如何在 python 中对装饰器工厂输入进行单元测试
Posted
技术标签:
【中文标题】如何在 python 中对装饰器工厂输入进行单元测试【英文标题】:How to unit test decorator factory inputs in python 【发布时间】:2019-11-25 12:53:01 【问题描述】:我想对多个装饰函数的装饰器工厂输入进行单元测试。
我曾尝试模拟装饰器工厂,但这并没有帮助我将特定的装饰器工厂调用关联到特定的装饰函数。相反,我只是按照在文件中调用的顺序获得装饰工厂调用的列表。
# decorator.py
from decorator_def import decorator_factory
@decorator_factory(2)
def say_goodbye():
print 'goodbye'
@decorator_factory(4)
def say_what():
print 'what'
import functools
def decorator_factory(input=1):
def decorator(func):
@functools.wraps(func)
def say_hello(*_args):
print 'hello '*input
_result = func(*_args)
return _result
return say_hello
return decorator
我想要这样的东西:
#pseudo
def test_decorated_say_goodbye():
assert decorator_factory_input_for_say_goodbye == 2
def test_decorated_say_what():
assert decorator_factory_input_for_say_what == 4
我最接近的是下面。 (基于这个答案https://***.com/a/37890916/8196029。) 这不是我想要的,因为它只是一个与修饰函数名称无关的调用列表:
import unittest
import imp
from mock import patch, MagicMock, call
import decorator
class TestDecorated(unittest.TestCase):
def setUp(self):
def kill_patches():
patch.stopall()
imp.reload(decorator)
self.addCleanup(kill_patches)
self.mocked_df = MagicMock()
patch('decorator_def.decorator_factory', self.mocked_df).start()
imp.reload(decorator)
def test_decorated_order(self):
df_call_args_expected = [
call(2), # say_goodbye
call(4) # say_what
]
assert self.mocked_df.call_args_list == df_call_args_expected
【问题讨论】:
【参考方案1】:找到了一条路:
from decorator import say_what, say_goodbye
def test_decorated_say_what():
assert say_what.__code__.co_freevars[1] == 'input'
assert say_what.__closure__[1].cell_contents == 4
def test_decorated_say_goodbye():
assert say_goodbye.__code__.co_freevars[1] == 'input'
assert say_goodbye.__closure__[1].cell_contents == 2
【讨论】:
以上是关于如何在 python 中对装饰器工厂输入进行单元测试的主要内容,如果未能解决你的问题,请参考以下文章
在 python 脚本中,如何忽略 Apache Pig 的 Python 装饰器以进行独立单元测试