Post the interface automation testing framework (python3+requests+excel)

Git: https://github.com/lilinyu861/Interface-Test
Environment configuration:

Development tools: pycharm2018+Excel

Development framework: python3+requests+excel

Interface automation testing framework introduction:

In this interface testing framework, the user first designs the original test case and writes the use case annotation for the test case, writes the original test case into the excel file and saves it in the data_origin folder, as shown in the figure below, and then calls the generateTestcases in the common insert image description here
folder The generate_testcases() method in .py passes in parameters to the method. The parameters include the location of the original test case excel file, the storage location after generating the test case excel file, and the storage location after generating the test case annotation excel file.

The generated test case excel file is as follows:

insert image description here
The generated test case annotation excel file is as follows:

insert image description here

 

After the test case is generated, you can edit the interface test code, execute the interface test, and then write the data of the test case into an excel file and store it in the reports folder.

Detailed introduction of each module of the interface automation testing framework:

The structure of the entire test framework is shown in the figure below:

insert image description here

  • common stores common methods
  • common_data stores common data
  • data Store the excel file of the test case
  • data_origin stores the excel file of the original test case
  • reports Store the excel file that records the returned message of the interface test case
  • test Test whether the method of the test framework can be executed normally
  • testcase stores the test cases written by the interface test
     

The entire test framework is mainly divided into two parts: test case generation, interface test execution

1. Generation of test cases:

The user puts the excel file storing the original test case into the data_origin folder;

Edit the interface test script, call the generate_testcases method of the generateTestcase.py file in the interface test script, generate test cases and write the test cases into excel files and store them in the data folder.
 

2. Interface test execution

  1. Interface test script writing, first read the test cases in the data file, and generate json format data from the test cases

  2. Call the request method in reqMethod to test the interface

Write the data returned by the interface test into an excel file and save it in the reports folder.


"""
接口测试
读取excel中的原始数据,经处理后生成测试用例,利用测试用例对接口进行测试,将接口测试返回结果存放到excel文件中。
"""
from common.generateTestcases import GenerateTestcases
from common.excelToDic import ExcelToDic
from common_data.interfaceUrl import Url
from common.reqMethod import RequestMethod
import xlwt
 
 
login_url = Url.login_url
# 原始数据表格位置
origin_excel_path = '../test/data_origin/test_read_excel.xlsx'
# 测试用例数据存放位置
excel_case_1 = '../test/data/test_case_01.xls'
# 测试用例解释数据存放位置
excel_case_2 = '../test/data/test_case_02.xls'
save_path = '../test/reports/report.xls'
g = GenerateTestcases()
# 生成测试用例及测试用例解释的excel文件
g.generate_testcases(origin_excel_path, excel_case_1, excel_case_2)
# 写入的excel
book = xlwt.Workbook(encoding="utf-8")
sheet = book.add_sheet('Sheet1', cell_overwrite_ok=True)
# 报文头
headers = {
    'Content-Type': 'application/json'
}
# 将测试用例写入excel文件中
test_cases = ExcelToDic().getExcelData(excel_case_1, 'Sheet1')
print(test_cases)
len = len(test_cases)
for i in range(len):
    print(test_cases[i])
    response = RequestMethod().post(interface_url=login_url,
                                    headers=headers,
                                    data=test_cases[i])
    print(i, response)
 
    # 将数据写入i行j列
    sheet.write(0, 0, 'email')
    sheet.write(0, 1, 'password')
    sheet.write(0, 2, 'response')
    sheet.write(i+1, 0, test_cases[i]['email'])
    sheet.write(i+1, 1, test_cases[i]['password'])
    sheet.write(i+1, 2, response.text)
book.save(save_path)

The generated interface test results are shown in the figure below:

insert image description here

 

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

Guess you like

Origin blog.csdn.net/okcross0/article/details/130406033
Recommended