Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用decorator提供单元测试数据时的准确测试计数_Python_Unit Testing_Decorator - Fatal编程技术网

Python 使用decorator提供单元测试数据时的准确测试计数

Python 使用decorator提供单元测试数据时的准确测试计数,python,unit-testing,decorator,Python,Unit Testing,Decorator,我使用函数装饰器通过数据提供程序函数将数据提供给python单元测试。我的解决方案与我的非常相似。每件事都很好,只有一个次要的,但非常恼人的例外: 在phpUnit中,当使用数据提供程序时,所提供的每个数据集的测试计数都会增加。当我添加一个数据集并且测试通过时,我可以很容易地看到新的数据集已经运行,因为我的测试计数增加了。在python中,使用decorator,测试计数保持不变。到目前为止,我一直在做的是确保在测试通过时运行数据集,这是故意引入错误,以看到测试失败。这还不是世界末日,但这已经够

我使用函数装饰器通过数据提供程序函数将数据提供给python单元测试。我的解决方案与我的非常相似。每件事都很好,只有一个次要的,但非常恼人的例外:

在phpUnit中,当使用数据提供程序时,所提供的每个数据集的测试计数都会增加。当我添加一个数据集并且测试通过时,我可以很容易地看到新的数据集已经运行,因为我的测试计数增加了。在python中,使用decorator,测试计数保持不变。到目前为止,我一直在做的是确保在测试通过时运行数据集,这是故意引入错误,以看到测试失败。这还不是世界末日,但这已经够烦人的了,我想在这里寻求帮助


有没有办法增加通过decorator提供的每个数据集的测试计数?

下面的代码片段展示了如何在decorator中使用计数器。 我希望你能把它转换成你的代码

def log(func):
    def inner(*args, **kwargs):
        print('{} {} {} {}'.format(str(inner.count), str(func), args, kwargs))
        inner.count += 1
        return func(args, kwargs)
    inner.count = 1
    return inner


@log
def foo(*args, **kwargs):
    print('{}{}'.format(args, kwargs))


def main():
    foo(1, 2, 3, 4)
    foo('a')
    foo('asdf', 'asdf')


if __name__ == '__main__':
    main()
此示例将打印:

1 <function foo at 0x2711848> (1, 2, 3, 4) {}
((1, 2, 3, 4), {}){}
2 <function foo at 0x2711848> ('a',) {}
(('a',), {}){}
3 <function foo at 0x2711848> ('asdf', 'asdf') {}
(('asdf', 'asdf'), {}){}
1(1,2,3,4){
((1, 2, 3, 4), {}){}
2('a',){}
((‘a’,),{}){}
3('asdf','asdf'){}
(('asdf','asdf'),{}){}

这是增加通过decorator提供的每个数据集的测试计数的一种方法。

如果不可能将每个数据集注册为唯一的测试,这将是一个很好的解决方法!