Skip to main content
 首页 » 编程设计

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

2025年04月02日16pengyingh

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

文件示例:

 { "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 

我正在检查 nose 参数化:

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

但是不能让它工作:(

提前谢谢你

请您参考如下方法:

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

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

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() 

如果您想从参数集创建多个新测试:

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