做python接口自动化的时候,需要对数据进行参数化,所以选择用paramunittest模块做参数化,使用修饰器@paramunittest.parametrized的时候发现只能传入固定参数,无法做到动态传参。如果需要传入一个list就需修改下paramunittest.py文件的源码
修改后的paramunittest.py文件源码:
def _process_parameters(parameters_seq):
processed_parameters_seq = []
for i in parameters_seq:
for parameters in i:
if isinstance(parameters, collections.Mapping):
processed_parameters_seq.append((tuple(),
dict(parameters)))
elif (len(parameters) == 2
and isinstance(parameters[0], collections.Sequence)
and isinstance(parameters[1], collections.Mapping)):
processed_parameters_seq.append((tuple(parameters[0]),
dict(parameters[1])))
else:
processed_parameters_seq.append((tuple(parameters),
dict()))
return processed_parameters_seq
测试代码(传入一个list给@paramunittest.parametrized):
import unittest
import paramunittest
a = [{'url': 'http://admin'},{'url': 'http://user'}]
@paramunittest.parametrized(a)
class Test(unittest.TestCase):
def setParameters(self, url):
self.url = url
def test_check(self):
print("url:", self.url)
if __name__ == "__main__":
unittest.main()
运行成功:
C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe C:/Users/Administrator/Desktop/pt/test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
url: 127.0.0.1
url: 0.0.0.0
Process finished with exit code 0