Python接口测试实战

发表于:2019-7-31 10:09

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:风清扬    来源:51Testing软件测试网原创

  引言:年初参与到一个后台系统开发的项目中,里面涉及了很多接口,我做为项目组测试人员,需要对这些接口进行测试,一开始使用postman工具测试,很是方便。但随着接口数量的增加,不光要执行手动点击测试,而且,一旦接口参数变动,都重新更改接口参数,次数多了,使得测试效率严重下降。
  后来我将目光转向了自动化测试,考虑到项目组对接口质量要求很高,需要快速开发。最终选定python作为脚本开发语言,使用其自带的requests和urllib模块进行接口请求,使用优化后的unittest测试框架编写测试接口函数,测试结果选用HTMLTestRunner框架予以展示,并使用python的ssl模块支持https协议的验证。接下来,本文就章节详细地介绍这些模块,并给出各个模块完整的测试代码。
  1、接口请求
  python特别是python 3.x中的urllib和requests模块,是用来请求url的两个主要模块。这两个模块中,如果仅仅是支持http协议的url请求,推荐使用requests模块。为什么这么说呢?因为爱因斯坦说过一句话:简洁就是美。requests模块对urllib模块又做了一层封装,使用更加方便。该模块支持GET, POST, PUT, DELETE等请求方法。请求返回信息包含状态码和消息体,状态码用三位数字表示,消息体可用字符串,二进制或json等格式表示。下面用一个例子来介绍一下requests模块的使用。代码如下:
   import requests
  def get_method(url, para, headers):
  try:
  req = requests.get(url=url, params=para, headers=headers)
  except Exception as e:
  print(e)
  else:
  if req.status_code == "200":
  return req
  else:
  print("Requests Failed.")
  if __name__=='__main__':
  url = "http://www.google.com"
  req = get_method(url=url, para=None, headers=None)
  print(req.status_code)
  print(req.text)
  输出为:
   200
  <!DOCTYPE html>
  <!--STATUS OK--><html> <head><meta...(省略)
  上述程序输出状态码为200,表明请求成功,返回消息体为网页内容。这里我仅对requests模块中的get请求方法做了封装,其它方法(如post,put,delete等)的封装类似。当让你也可以不用封装,直接使用requests.methodName来直接调用该方法。这里提醒一句,在实际的接口测试中,headers和data都是有值的,要确保这些值的填写正确,大部分请求下的请求失败或返回结果错误,基本上都是由于这些值的缺失或错误造成的。更多关于requests模块的介绍,请参考官方文档。
  2、测试框架优化
  unittest是python中进行单元测试使用广泛的框架,其与java中的单元测试框架junit类似。该框架使用简单,需要编写以test开头的函数,选择unittest框架运行测试函数,测试结果在终端显示。这里举一个简单的例子:
   import unittest
  class ApiTestSample(unittest.TestCase):
  def setUp(self):
  pass
  def tearDown(self):
  pass
  def jiafa(self, input01, input02):
  result = input01 + input02
  return result
  def test_jiafa(self):
  testResult = self.jiafa(input01=4, input02=5)
  self.assertEqual(testResult, 9)
  if __name__=='__main__':
  unittest.main()
  简单解释下这段代码,首先我们创建一个类ApiTestSample,这个类继承自unittest.TestCase类。然后在这个类中写了jiafa函数,它有两个参数input01,input02,返回input01与input02相加的和。接着在test_jiafa方法中,我们对刚才jiafa函数进行了和值校验。通过给jiafa输入两个值,获取其函数返回值,并与真实值做相等判断,以此实现函数单元测试。这里用到了unittest中断言值相等的assertEqual(m, n)函数,上述代码运行结果如下:
   Ran 1 test in 0.000s
  OK
  以上是unittest框架最基本的单元测试应用,但是这个框架有个缺陷,就是不能自己传入参数。对于接口来说,往往需要传入很多参数,并且这每个参数又有很多取值,如果不对原先的unittest框架做改变,不仅无法用来进行接口测试,而且一个个结合参数取值去写测试代码,工作量极其庞大,也没有实现测试数据与脚本没有分离。基于此,我们对该框架做出一下两点优化。
  扩展unittest.TestCase类,支持自定义参数输入;
  测试数据与测试脚本分离,测试数据存储在文件和数据库中,以增强测试脚本复用性;
  以下是对unittest.TestCase类的扩展,使其支持参数化把参数加进去。下面是具体的代码实现过程:
   class ExtendTestCaseParams(unittest.TestCase):
  #扩展unittest.TestCase类,使其支持自定义参数输入
  def __init__(self, method_name='runTest', canshu=None):
  super(ExtendTestCaseParams, self).__init__(method_name)
  self.canshu = canshu
  #静态参数化方法
  @staticmethod
  def parametrize(testcase_klass, default_name=None, canshu=None):
  """ Create a suite containing all tests taken from the given
  subclass, passing them the parameter 'canshu'
  """
  test_loader = unittest.TestLoader()
  testcase_names = test_loader.getTestCaseNames(testcase_klass)
  suite = unittest.TestSuite()
  if default_name != None:
  for casename in testcase_names:
  if casename == defName:
  suite.addTest(testcase_klass(casename, canshu=canshu))
  else:
  for casename in testcase_names:
  suite.addTest(testcase_klass(casename, canshu=canshu))
  return suite
  这里,canshu就是优化后加的自定义参数,参数类型可以是元组或列表。下面使用这个参数化类来改写之前的代码。
   class ApiTestSample(ExtendTestCaseParams):
  def setUp(self):
  pass
  def tearDown(self):
  pass
  def jiafa(self, input01, input02):
  result = input01 + input02
  return result
  def test_jiafa(self):
  input_01 = self.param[0]
  input_02 = self.param[1]
  expectedResult = self.param[2]
  result = self.sub(input_01, input_02)
  print(result)
  self.assertEqual(result, expectedResult)
  if __name__=='__main__':
  testData = [
  (10, 9, 19),
  (12, 13, 25),
  (12, 10, 22),
  (2, 4, 6)
  ]
  suite = unittest.TestSuite()
  for i in testData:
  suite.addTest(ExtendTestCaseParams.parametrize(ApiTestSample, 'test_jiafa', canshu=i))
  runner = unittest.TextTestRunner()
  runner.run(suite)
  执行结果如下:
   ....
  ## 19
  25
  Ran 4 tests in 0.000s
  22
  6
  OK
  通过对unittest框架优化,我们实现了unittest框架的参数化,这样就可以用于接口测试了。虽然我们实现了参数化,但是测试结果的展示不够直观,这个时候需要一个可视化页面来直接显示测试结果。所幸的是,python中有专门展示测试结果的框架:HTMLTestRunner。该框架可以将测试结果转换为HTML页面,并且该框架可以和unittest框架完美的结合起来。接下来我们讲述一下HTMLTestRunner框架的使用。
  3、测试结果可视化
  HTMLTestRunner框架可用来生成可视化测试报告,并能很好的与unittest框架结合使用,接下来我们以一段代码来展示一下HTMLTestRunner的使用。
   if __name__=='__main__':
  from HTMLTestRunner import HTMLTestRunner
  testData = [
  (10, 9, 19),
  (12, 13, 25),
  (12, 10, 22),
  (2, 4, 6)
  ]
  suite = unittest.TestSuite()
  for i in testData:
  suite.addTest(ExtendTestCaseParams.parametrize(ApiTestSample, 'test_jiafa', canshu=i))
  currentTime = time.strftime("%Y-%m-%d %H_%M_%S")
  result_path = './test_results'
  if not os.path.exists(path):
  os.makedirs(path)
  report_path = result_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)
  测试结果如下:
  下面详细讲解一下html报告的生成代码:
  runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc)
  HTMLTestRunner中的stream表示输入流,这里我们将文件描述符传递给stream,title参数表示要输出的测试报告主题名称,description参数是对测试报告的描述。在使用HTMLTestRunner时,有几点需要注意:
  HTMLTestRunner模块非Python自带库,需要到HTMLTestRunner的官网下载该安装包;
  官网的HTMLTestRunner模块仅支持Python 2.x 版本,如果要在Python 3.x中,需要修改部分代码,修改的代码部分请自行上网搜索;
  如果需要生成xml格式,只需将上面代码中的
   runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc)
  runner.run(suite)
  修改为如下代码
   import xmlrunner
  runner = xmlrunner.XMLTestRunner(output='report')
  runner.run(suite)
  4、接口测试分类
  通过前面3节的讲解,大家对接口请求,测试框架和测试结果可视化方面有了深入的了解。有了前面的基础,对于接下来理解和编写接口测试会有很大帮助。这里我们先来讲解一下接口测试与单元测试的区别。单元测试只针对函数进行多组参数测试,包括正常和异常参数组合。而接口测试是针对某一接口进行多组参数测试。实际接口测试中,我们又将接口测试分为两种:
  单接口测试;
  多接口测试。
  对于单接口测试,只需针对单个接口测试,测试数据根据接口文档中的参数规则来设计测试用例;对多接口测试,首先要确保接口之间调用逻辑正确,然后再根据接口文档中的参数规则来设计用例进行测试。下面我就根据这两种不同情况的接口测试,用实际项目代码展示一下。
  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 = 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 = [
  (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 = {"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)
  上述代码中,我们对两个接口进行了函数封装,两个接口之间有依赖关系,oauth2_basic()函数在请求之前必须先去请求register()函数获取数据。对于这种多接口测试,且接口之间存在互相调用的情况,最好是在调用该接口前时,将互相之间有依赖的接口封装进该接口中,保证接口调用逻辑一致。其次再针对该接口的其它参数设计测试用例去测试该接口。

   ......
查看更多精彩内容,请点击下载:

版权声明:本文出自《51测试天地》第五十四期。51Testing软件测试网及相关内容提供者拥有51testing.com内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号