修改为如下代码
import xmlrunner
runner = xmlrunner.XMLTestRunner(output='report')
runner.run(suite)
文章插图
4、接口测试分类
前面大家对接口请求,测试框架和测试结果可视化方面有了深入的了解 。有了前面的基础,对于接下来理解和编写接口测试会有很大帮助 。这里我们先来讲解一下接口测试与单元测试的区别 。单元测试只针对函数进行多组参数测试,包括正常和异常参数组合 。而接口测试是针对某一接口进行多组参数测试 。实际接口测试中,我们又将接口测试分为两种:
1)单接口测试;
2)多接口测试 。
对于单接口测试,只需针对单个接口测试,测试数据根据接口文档中的参数规则来设计测试用例;对多接口测试,首先要确保接口之间调用逻辑正确,然后再根据接口文档中的参数规则来设计用例进行测试 。下面我就根据这两种不同情况的接口测试,用实际项目代码展示一下 。
4.1 单接口测试
class TestApiSample(ExtendTestCaseParams):
def setUp(self):
pass
def tearDown(self):
pass
def register(self, ip, name, desc):
url = 'http://%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 test_register(self):
for index, value in enumerate(self.param):
print('Test Token {0} parameter is {1}'.format(index, value))
self.ip = self.param[1]
self.name = self.param[2]
self.desc = self.param[3]
self.expectedValue = https://www.isolves.com/it/cxkf/yy/Python/2019-07-17/self.param[4]
req = self.grant_register(self.ip, self.name, self.desc)
self.assertIn(req.status_code, self.expectedValue, msg="Test Failed.")
if __name__=='__main__':
import random
import string
ip = '172.36.17.108'
testData = https://www.isolves.com/it/cxkf/yy/Python/2019-07-17/[
(1, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
(2, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
(3, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200)
]
suite = unittest.TestSuite()
for i in testData:
suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample,'test_register',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)
上述代码中的 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 = https://www.isolves.com/it/cxkf/yy/Python/2019-07-17/{"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__':
推荐阅读
- 教你如何优雅地用Python连接MySQL数据库
- 探秘电脑SATA接口:线缆数量减少11倍,传输速度提升5倍
- 怎么用ping命令测试网络承载能力和连通性
- 红茶的茶香味测试吸入红茶香气有缓解压力的作用
- 后端接口都测试什么?怎么测?
- 基于python语言的大数据搜索引擎
- 常见加密方式和Python实现
- 电脑主板上CPU_FAN、SYS_FAN、CHA_FAN、CPU_OPT接口知识科普
- 压力测试工具JMeter使用入门
- 简单函数与温度转换 业余码农成长记——Python学习4