做python接口自動(dòng)化的時(shí)候,需要對數(shù)據(jù)進(jìn)行參數(shù)化,所以選擇用paramunittest模塊做參數(shù)化,使用修飾器@paramunittest.parametrized的時(shí)候發(fā)現(xiàn)只能傳入固定參數(shù),無法做到動(dòng)態(tài)傳參。如果需要傳入一個(gè)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
測試代碼(傳入一個(gè)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()
運(yùn)行成功:
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