import unittest, requests, json;
class Test_interface_py_cm(unittest.TestCase):
def setUp(self):
print("<<<<<< start test >>>>>>")
self.base_url = "http://192.168.11.67:8063/api/"
self.s = requests.Session()
self.headers = {'content-type': 'application/json;charset=UTF-8'}
def tearDown(self):
print("<<<<<< end test >>>>>>")
def test1(self, arg_page, arg_pagesize):
data = {"minTimes": 9}
r = self.s.post(
self.base_url + "mining/analysis/community/stranger/list/page/" + arg_page + "/pagesize/" + arg_pagesize,
data=json.dumps(data), headers=self.headers)
result = r.json()
self.assertEqual(r.status_code, 200)
self.assertEqual(result["errCode"], 0)
self.assertEqual(result['data']['name'], '31598')
r.connection.close()
if __name__ == '__main__':
unittest.main()
当我想将参数传递给test1
方法时,在main()
方法中,如何将参数传递给test1
(必须使用unittest
测试框架来调用)
答案 0 :(得分:0)
如果要使用arg_page
和arg_pagesize
参数来参数化测试,可以像这样动态构建测试方法。
class Test_interface_py_cm(unittest.TestCase):
def setUp(self):
print("<<<<<< start test >>>>>>")
self.base_url = "http://192.168.11.67:8063/api/"
self.s = requests.Session()
self.headers = {'content-type': 'application/json;charset=UTF-8'}
def tearDown(self):
print("<<<<<< end test >>>>>>")
if __name__ == '__main__':
def create_test(name, arg_page, arg_pagesize):
def test1(self):
data = {"minTimes": 9}
r = self.s.post(
self.base_url + "mining/analysis/community/stranger/list/page/" + arg_page + "/pagesize/" + arg_pagesize,
data=json.dumps(data), headers=self.headers)
result = r.json()
self.assertEqual(r.status_code, 200)
self.assertEqual(result["errCode"], 0)
self.assertEqual(result['data']['name'], '31598')
r.connection.close()
setattr(Test_interface_py_cm, name, test1)
create_test(name='test1', arg_page='page1', arg_pagesize=10)
create_test(name='test2', arg_page='page1', arg_pagesize=20)
create_test(name='test3', arg_page='page2', arg_pagesize=10)
create_test(name='test4', arg_page='page2', arg_pagesize=20)
unittest.main()
在上面的示例中,通过调用create_test
将测试方法动态添加到测试类中。每种方法都使用不同的arg_page
和arg_pagesize
值运行。您提供的名称当然必须是唯一的。