• Python+Requests+Excel接口测试实战


    1、EXCEL文件接口保存方式,如图。

    2、然后就是读取EXCEL文件中的数据方法,如下:

    1. 1 import xlrd
    2. 2
    3. 3
    4. 4 class readExcel(object):
    5. 5 def __init__(self, path):
    6. 6 self.path = path
    7. 7
    8. 8 @property
    9. 9 def getSheet(self):
    10. 10 # 获取索引
    11. 11 xl = xlrd.open_workbook(self.path)
    12. 12 sheet = xl.sheet_by_index(0)
    13. 13 return sheet
    14. 14
    15. 15 @property
    16. 16 def getRows(self):
    17. 17 # 获取行数
    18. 18 row = self.getSheet.nrows
    19. 19 return row
    20. 20
    21. 21 @property
    22. 22 def getCol(self):
    23. 23 # 获取列数
    24. 24 col = self.getSheet.ncols
    25. 25 return col
    26. 26
    27. 27 # 以下是分别获取每一列的数值
    28. 28 @property
    29. 29 def getName(self):
    30. 30 TestName = []
    31. 31 for i in range(1, self.getRows):
    32. 32 TestName.append(self.getSheet.cell_value(i, 0))
    33. 33 return TestName
    34. 34
    35. 35 @property
    36. 36 def getData(self):
    37. 37 TestData = []
    38. 38 for i in range(1, self.getRows):
    39. 39 TestData.append(self.getSheet.cell_value(i, 1))
    40. 40 return TestData
    41. 41
    42. 42 @property
    43. 43 def getUrl(self):
    44. 44 TestUrl = []
    45. 45 for i in range(1, self.getRows):
    46. 46 TestUrl.append(self.getSheet.cell_value(i, 2))
    47. 47 return TestUrl
    48. 48
    49. 49 @property
    50. 50 def getMethod(self):
    51. 51 TestMethod = []
    52. 52 for i in range(1, self.getRows):
    53. 53 TestMethod.append(self.getSheet.cell_value(i, 3))
    54. 54 return TestMethod
    55. 55
    56. 56 @property
    57. 57 def getUid(self):
    58. 58 TestUid = []
    59. 59 for i in range(1, self.getRows):
    60. 60 TestUid.append(self.getSheet.cell_value(i, 4))
    61. 61 return TestUid
    62. 62
    63. 63 @property
    64. 64 def getCode(self):
    65. 65 TestCode = []
    66. 66 for i in range(1, self.getRows):
    67. 67 TestCode.append(self.getSheet.cell_value(i, 5))
    68. 68 return TestCode

     3、EXCEL中的数据读取成功后,然后我们需要对于读出来的数据进行相应的处理。如下。当然示例中只是简单列了一下关于POST,GET等二种方式,实际还有很多其它方式,如put,delete等,请求中也还会包括headers,这些都可以自已添加上去。

    1. 1 import requests
    2. 2 import json
    3. 3 from xl.read_xl import readExcel
    4. 4 from pubulic_way.get_token import get_token
    5. 5
    6. 6
    7. 7 class testApi(object):
    8. 8 def __init__(self, method, url, data):
    9. 9 self.method = method
    10. 10 self.url = url
    11. 11 self.data = data
    12. 12
    13. 13
    14. 14 @property
    15. 15 def testApi(self):
    16. 16 # 根据不同的访问方式来访问接口
    17. 17 try:
    18. 18 if self.method == 'post':
    19. 19 r = requests.post(self.url, data=json.dumps(eval(self.data)))
    20. 20 elif self.method == 'get':
    21. 21 r = requests.get(self.url, params=eval(self.data))
    22. 22 return r
    23. 23 except:
    24. 24 print('失败')
    25. 25
    26. 26 def getCode(self):
    27. 27 # 获取访问接口的状态码
    28. 28 code = self.testApi.json()['error']
    29. 29 return code
    30. 30
    31. 31 def getJson(self):
    32. 32 # 获取返回信息的json数据
    33. 33 json_data = self.testApi.json()
    34. 34 return json_data

    4、最后我们就可以调用之前准备好的方法进去测试了。

    1. 1 from base.base_test import baseTest
    2. 2 from xl.read_xl import readExcel
    3. 3 from pubulic_way.test_api_way import testApi
    4. 4 import unittest
    5. 5
    6. 6
    7. 7 class testLoginApi(unittest.TestCase):
    8. 8 def testLoginApi(self):
    9. 9 '''测试发布评伦接口。'''
    10. 10 excel = readExcel(r'F:\path\add_thread_data.xlsx')
    11. 11 name = excel.getName
    12. 12 data = excel.getData
    13. 13 url = excel.getUrl
    14. 14 method = excel.getMethod
    15. 15 uid = excel.getUid
    16. 16 code = excel.getCode
    17. 17 row = excel.getRows
    18. 18 for i in range(0, row - 1):
    19. 19 api = testApi(method[i], url[i], data[i])
    20. 20 apicode = api.getCode()
    21. 21 apijson = api.getJson()
    22. 22 if apicode == code[i]:
    23. 23 print('{}、{}:测试成功。json数据为:{}'.format(i + 1, name[i], apijson))
    24. 24 else:
    25. 25 print('{}、{}:测试失败'.format(i + 1, name[i]))
    26. 26
    27. 27
    28. 28 if __name__ == '__main__':
    29. 29 unittest.main(verbosity=2)

    5、最后还需要把我们的结果展示出来,这个就很简单了,利用htmltestrunner来展示。展示一张报告的切图。


    下面是配套学习资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

  • 相关阅读:
    物理信息驱动深度学习相关报告总结
    ArcGIS创建格网
    动物实验文献检索——Sci
    Flink-时间流与水印
    免费记课时小程序-全优学堂
    【教程】uni-app iOS打包解决profile文件与私钥证书不匹配问题
    CSS特效009:音频波纹加载律动
    大数据平台三大优势详解-行云管家
    【SpringBoot教程】SpringBoot+MybatisPlus数据库连接测试 用户收货信息接口开发
    PCB设计---焊接工艺
  • 原文地址:https://blog.csdn.net/qq_73332379/article/details/132852724