自動化 | PYTHON接口自動化測試--使用EXCEL進行測試用例執行與測試結果寫入


轉自:https://www.freesion.com/article/3046426995/

1.為了更好的實現讀取excel文件進行接口自動化測試,將操作excel文件方法封裝:


   
   
  
  
          
  1. # coding:utf-8
  2. import xlrd
  3. from xlutils.copy import copy # 導入xlutils的copy方法
  4. class HandleExcel:
  5. """封裝操作excel的方法"""
  6. def __init__(self, file='E:/PyChram項目集合/interfacetest/excel/30.xls', sheet_id=0):
  7. self.file = file
  8. self.sheet_id = sheet_id
  9. self.data = self.get_data()
  10. # 為了在創建一個實例時就獲得excel的sheet對象,可以在構造器中調用get_data()
  11. # 因為類在實例化時就會自動調用構造器,這樣在創建一個實例時就會自動獲得sheet對象了
  12. # 獲取某一頁sheet對象
  13. def get_data(self):
  14. data = xlrd.open_workbook(self.file)
  15. sheet = data.sheet_by_index(self.sheet_id)
  16. return sheet
  17. # 獲取excel數據行數
  18. def get_rows(self):
  19. rows = self.data.nrows
  20. # t = self.get_data() # 調用get_data()取得sheet對象(如果不在構造器獲取sheet對象,就需要在方法內先獲取sheet對象,再進行下一步操作,每個方法都要這樣,所以還是寫在構造器中方便)
  21. # rows = t.nrows
  22. return rows
  23. # 獲取某個單元格數據
  24. def get_value(self, row, col):
  25. value = self.data.cell_value(row, col)
  26. return value
  27. # 向某個單元格寫入數據
  28. def write_value(self, row, col, value):
  29. data = xlrd.open_workbook(self.file) # 打開文件
  30. data_copy = copy(data) # 復制原文件
  31. sheet = data_copy.get_sheet( 0) # 取得復制文件的sheet對象
  32. sheet.write(row, col, value) # 在某一單元格寫入value
  33. data_copy.save(self.file) # 保存文件
  34. # 封裝excel的列名常量
  35. def get_caseseq():
  36. """獲取caseSeq"""
  37. caseSeq = 0
  38. return caseSeq
  39. def get_apitype():
  40. """獲取apiType"""
  41. apiType = 1
  42. return apiType
  43. def get_apiseq():
  44. """獲取apiSeq"""
  45. apiSeq = 2
  46. return apiSeq
  47. def get_apiName():
  48. """獲取apiName"""
  49. apiName = 3
  50. return apiName
  51. def get_priority():
  52. """獲取priority"""
  53. priority = 4
  54. return priority
  55. def get_url():
  56. """獲取url"""
  57. url = 5
  58. return url
  59. def get_method():
  60. """獲取method"""
  61. method = 6
  62. return method
  63. def get_header():
  64. """獲取header"""
  65. header = 7
  66. return header
  67. def get_purpose():
  68. purpose = 8
  69. return purpose
  70. def get_params():
  71. """獲取params"""
  72. params = 9
  73. return params
  74. def get_expectvalue():
  75. """獲取expectValue"""
  76. expect = 10
  77. return expect
  78. def get_resultvalue():
  79. result = 11
  80. return result
  81. if __name__ == '__main__':
  82. test = HandleExcel()
  83. print(test.get_data())
  84. print(test.get_rows())
  85. print(test.get_value( 0, 0))

2.將操作excel的方法封裝好后,准備接口用例來遍歷循環

其中:

        caseSeq:用例編號

        apiType:接口類型

        apiSeq:接口編號

        apiName:接口名稱

        priority:優先級

        url:接口調用地址

        method:協議方法

        header:請求頭

        purpose:用例描述

        params:接口參數

        expectValue:期望結果

        resultValue:測試結果

3.編寫主函數代碼,實現接口用例遍歷並將測試結果寫入excel表中


   
   
  
  
          
  1. # coding:utf-8
  2. from mylib.run_method import RunMain
  3. from mylib.handle_excel import *
  4. import json
  5. class RunTestCase:
  6. def __init__(self):
  7. self.Runmain = RunMain() # 實例化調用get/post請求基類
  8. self.data = HandleExcel() # 實例化操作excel文件類
  9. def go_run(self):
  10. rows_count = self.data.get_rows() # 獲取excel行數
  11. for i in range( 1,rows_count): # 利用行數進行迭代處理每個接口
  12. url = self.data.get_value(i, get_url()) # 循環獲取url的值
  13. print(url)
  14. method = self.data.get_value(i, get_method()) # 循環獲取method的值
  15. print(method)
  16. if self.data.get_value(i, get_params()) == '':
  17. data = None
  18. else:
  19. data = json.loads(self.data.get_value(i, get_params())) # 循環獲取請求參數,並將得到的數據反序列
  20. expect = self.data.get_value(i, get_expectvalue()) # 循環獲取期望輸出
  21. is_run = self.data.get_value(i, get_priority()) # 獲取是否運行,即判斷excel中priority是不是"H"
  22. if is_run == 'H':
  23. res = self.Runmain.run_main(url, method, data) # 調用get/post主函數
  24. print(res)
  25. if expect in res:
  26. print( '測試成功')
  27. self.data.write_value(i, get_resultvalue(), 'pass')
  28. else:
  29. print( '測試失敗')
  30. self.data.write_value(i, get_resultvalue(), 'fail')
  31. if __name__ == '__main__':
  32. run = RunTestCase()
  33. run.go_run()

運行結果如下:

excel表格中填入如下:

       本篇簡單的使用excel進行接口測試用例的執行,在測試過程中最重要的是測試用例的編寫,在遇到不同的測試用例執行時會出現不同的問題,還需要多使用多練習,按照實際進行Python接口自動化測試用例編寫。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM