上述代码中的 register()为注册接口函数,test_register()为测试注册接口函数,testData 为测试数据,这里没有完全做到测试脚本与测试数据分离。为了实现测试数据与测试脚本分离,可以将 testData 列表单独写在文本文件或者数据库中,运行测试脚本时再去加载这些数据,就能实现测试脚本与测试数据的分离。
4.2 多接口测试
- class TestApiSample(ExtendTestCaseParams):
-
- def setUp(self):
-
- pass
-
- def tearDown(self):
-
- pass
-
- def register(self, ip, name, desc):
-
- url = 'https://%s/api/v1/reg' % ip
-
- headers = {"Content-Type": "application/x-www-form-urlencoded"}
-
- para = {"app_name": name, "description": desc}
-
- req = self.Post(url, para, headers)
-
- return req
-
- def oauth2_basic(self, ip, name, desc):
-
- apps = self.register(ip, name, desc)
-
- apps = apps.json()
-
- url = 'http://%s/api/v1/basic' % ip
-
- data = {"client_id":apps['appId'], "client_secret":apps['appKey']}
-
- headers = None
-
- req = requests.post(url, data, headers)
-
- basic = str(req.content, encoding='utf-8')
-
- return apps, basic, req
-
- def test_oauth2_basic(self):
-
- count = 0
-
- for i in self.param:
-
- count += 1
-
- self.ip = self.param[1]
-
- self.name = self.param[2]
-
- self.desc = self.param[3]
-
- self.expected = self.param[4]
-
- apps, basic, req = self.oauth2_basic(self.ip, self.name, self.desc)
-
- self.assertIn(req.status_code, self.expected, msg="Grant Failed.")
-
- if __name__=='__main__':
-
- import random
-
- import string
-
- ipAddr = '172.36.17.108'
-
- testData = [
-
- (1, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
-
- (2, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
-
- (3, ipAddr, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200)
-
- ]
-
- suite = unittest.TestSuite()
-
- for i in testData:
-
- suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample, 'test_oauth2_basic',
-
- canshu=i))
-
- currentTime = time.strftime("%Y-%m-%d %H_%M_%S")
-
- path = '../Results'
-
- if not os.path.exists(path):
-
- os.makedirs(path)
-
- report_path = path + '/' + currentTime + "_report.html"
-
- reportTitle = '接口测试报告'
-
- desc = u'接口测试报告详情'
-
- with open(report_path, 'wd') as f:
-
- runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
-
- runner.run(suite)
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|