APP端随着版本升级,对应的服务端接口也会增加或修改,为了验证新版本是否影响老版本的接口逻辑和功能,可使用自动化测试进行老版本接口回归。
经分析项目接口包含公共参数和可选参数两部分:
import unittest
import HTMLTestRunner3
class MyProject(object):
"""MyProject包含3条测试用例,拥有公共参数arg,以及可选参数*arg"""
def __init__(self, arg, *args):
super(MyProject, self).__init__()
self.arg = arg
def t_001_create_exam(self):
ret1 = self.arg + '1'
return ret1
def t_002_bind_exam(self):
ret2 = self.arg + '2'
return ret2
def t_003_upload_exam(self):
ret3 = self.arg + '3'
return ret3
class MyTestCase(unittest.TestCase):
"""MyTestCase继承unittest.TestCase,3个测试用例需以test开头"""
# 执行用例前执行,仅执行一次
@classmethod
def setUpClass(cls):
cls.param = 'test'
cls.param1 = '1'
cls.param2 = '2'
cls.param3 = '33' #改为3时test_003结果为pass
cls.myobj = MyProject(cls.param,
(cls.param1, cls.param2, cls.param3)
)
# 执行每个用例前执行
def setUp(self):
pass
# 执行每个用例后执行tearDown方法,恢复测试环境
def tearDown(self):
pass
def test_001(self):
ret = self.myobj.t_001_create_exam()
self.assertEqual(ret, self.param + self.param1)
def test_002(self):
ret = self.myobj.t_002_bind_exam()
self.assertEqual(ret, self.param + self.param2)
def test_003(self):
ret = self.myobj.t_003_upload_exam()
self.assertEqual(ret, self.param + self.param3)
# suite()函数收集待测试的用例
def suite():
suite = unittest.TestSuite()
suite.addTest(MyTestCase('test_001'))
suite.addTest(MyTestCase('test_002'))
suite.addTest(MyTestCase('test_003'))
return suite
if __name__ == '__main__':
file_path = 'TestReport.html'
with open(file_path, 'wb') as fp:
runner = HTMLTestRunner3.HTMLTestRunner(
stream=fp,
title='自动化测试报告',
description='回归测试',
verbosity=2
)
runner.run(suite())
t_long_case.py为脚本名称
1: http://tungwaiyip.info/software/HTMLTestRunner.html
2: https://docs.python.org/3.5/library/unittest.html