如何从python中的文件(json或yaml)动态创建参数化测试

时间:2016-03-23 01:00:01

标签: python unit-testing nose parameterized

真的需要一个建议如何从python中的文件(json或yaml)动态创建参数化测试。 文件包含网址列表

文件示例:

 { "name":"url_1", "url":"http://check.com","expected": "23"}

或:

{ "url_1", "http://check.com","23"}

这样的例子:

@parameterized(from the file import name, expected)

def testUrl (self, name, url, expected):
    run something (%s) url
    assertTrue( expected = result)

输出:

test_for_url_1_ (test.TestClass) ... ok
test_for_url_2_ (test.TestClass) ... ok

我正在检查鼻子参数化:

# An iterable of params
@parameterized(
    param.explicit(*json.loads(line))
    for line in open("testcases.jsons")
)
def test_from_json_file(...):
    ...

但是不能让它发挥作用:(

提前谢谢

1 个答案:

答案 0 :(得分:1)

如果您只想将params从文件传递到单个函数:

是的,您可以使用装饰器,将文件名传递给它,从文件加载json并用作装饰func的参数:

import json
from functools import wraps


def params_from_file(file):
    """Decorator to load params from json file."""
    def decorator(func_to_decorate):
        @wraps(func_to_decorate)
        def wrapper(self, *args, **kwargs):
            with open(file, 'r') as fh:
                kwargs = json.loads(fh.read())
                return func_to_decorate(self, **kwargs)
        return wrapper
    return decorator

用法:

import unittest


class Test(unittest.TestCase):
    @params_from_file('file.txt')  # {"name":"url_1", "url":"http://check.com", "expected": "23"}
    def test_url(self, name, url, expected):        
        self.assertEqual(name, 'url_1')
        self.assertEqual(url, 'http://check.com')
        self.assertEqual(expected, '23')
        # print(name, url, expected)


if __name__ == "__main__":
    unittest.main()

如果您想要使用params set创建多个新测试:

import json


def create_tests(func_name, file):
    def decorator(cls):
        func = getattr(cls, func_name)
        # Set new funcs to class:
        with open(file, 'r') as fh:
            data = json.loads(fh.read())['data']
            for i, params in enumerate(data):
                def tmp(params=params):  # need for http://stackoverflow.com/q/7546285/1113207
                    def wrapper(self, *args, **kwargs):
                        return func(self, **params)      
                    return wrapper          
                setattr(cls, func_name + '_' + str(i), tmp())
        # Remove func from class:
        setattr(cls, func_name, None)
        return cls
    return decorator

用法:

import unittest


@create_tests('test_url', 'file.txt')
class Test(unittest.TestCase):
    def test_url(self, name, url, expected):
        print(name, url, expected)


if __name__ == "__main__":
    unittest.main(verbosity=2)

输出:

test_url_0 (__main__.Test) ... name1 https://example.com 175
ok
test_url_1 (__main__.Test) ... name2 https://example2.com 15
ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s