Interface automation that we come into contact with daily can be divided into two major categories based on actual goals:
1. Interface automation for simulating test data
Most of this interface automation is a single execution, with the clear purpose of creating test data for functional testing, saving the time and labor costs of artificial data creation, and improving the testing efficiency of functional testers.
2. Interface automation to detect errors in advance before functional testing
The workflow of this kind of interface automation is the same as that of functional testing. You need to design interface test cases and then execute the interface test cases.
To put it bluntly, it is to perform functional verification on a single interface, including verification of the required parameters of the interface, length character type restrictions, whether the input parameter enumeration values are correct, whether the response data is correct, etc.
This article mainly explains the second type of interface automation testing. The core points of interface automation are condensed for practical use, which may become a bonus point in the interview.
Interface test case design
Code reading interface test case
Use the pytest framework to implement interface automation
Use python-requests to complete interface requests
Use jenkins+allure to achieve continuous building and output test reports
demand analysis
Excel management interface test case
Pytest framework runs test cases
Jenkins integration build
Allure display test report
data preparation
Understand the interface
1. Interface request method: get
2. Interface request header: {"Content-Type": "text/html;charset=utf-8"}
3. Interface request body: {"key": "*******************,"type": ""}
4. Parameter value: type=guoji, keji, yule, default top
5. News request: http://v.juhe.cn/toutiao/index
Note: The key requested by the interface needs to be applied to the aggregation data platform by yourself.
Design interface test cases
The interface test case design idea is consistent with the functional test case design. It basically verifies: required items, character type length limit, input parameter content, etc.
The use case design of the interface part is as follows: (for reference only)
Script directory design
A complete interface automation test is generally divided into four basic folders: data, public configuration, interface scripts, and reports.
Data: The Data folder is used to store data, such as test case documents
Public scripts: The Common folder is used to store code files for public methods, such as reading test case code files, etc.
Interface script: The Request folder is used to store code files for different business interfaces, such as: getting news headlines interface code files, etc.
Report: Report folder, used to store test result reports
Knowledge points related to script design
Read test cases
Knowledge point 1: Basic method of reading excel with xlrd
1. Open the file object: fb=xlrd.open_workbook('xls test case file path')
2. Get the worksheet name: sheetnames=fb.sheet_names ()
3. Open the worksheet where the test case is located: casesheet=fb.get_by_name(''The name of the worksheet where the test case is located)
4. Get the number of test cases (number of table rows): rows=casesheet.nrows
5. Get the cell value in the worksheet: cell_value=casesheet.cell_values(x,y), where x and y are the abscissa and ordinate of the cell
6. Since it needs to be parameterized through the pytest decorator, the cells read need to be combined into a list template.
For example:
case1=[‘url_01’,’method_01’,{body_01 },’expect_01’] case2=[‘url_02’,’method_02’,{body_02 },’expect_02’]
The final data provided to pytest for reading should be:
[[‘url_01’,’method_01’,{body_01 },’expect_01’], [‘url_02’,’method_02’,{body_02 },’expect_02’]]
code segment:
Use the list append method append().
Note: The cell data read through cell_values() defaults to the string str type.
Knowledge point 2: Data transformation
1.url and method are string types, so no conversion is required
2. The header and body need to be converted to json format through json.loads()
Interface request
Knowledge point 1: Basics of requests interface requests
1. Initiate an interface request:
resq=request.get() resq =request.post()
2. Get the response data and convert it to json format: resq.json()
3. Assert: assert
4. Extract response content: jsonpath syntax
response[‘msg’] response[‘data’][‘name’]
Knowledge point 2: pytest framework
Environment installation:
pip install pytest
1. Pytest usage rules: use cases and script files must start with test
2. Parameterized execution use case: Pytest decorator @pytest.mark.aprametrize()
a) Single parameter writing method: @pytest.mark.parametrize('inData',[10,20]), the first parameter is the variable name, and the second is the parameter value. There is no need to set up a loop to read parameter values, because pytest's decorator will automatically read in a loop.
b) How to write multiple parameters: (parameter values are written in tuple form) @pytest.mark.parametrize('first parameter name, second parameter name'), [(first value of the first parameter, the second value of the first parameter), (the first value of the second parameter, the second value of the second parameter)].
c) There is no need to set up a loop to read parameter values, because the pytest decorator will automatically read in a loop.
3. Local debugging and running script:
The interface automatically runs the script to generate the allure-html report, which is divided into two steps:
Step 1: alluredir generates the test report data source file json and stores it in the report folder
pytest.main(['Interface request script file.py','-s','--alluredir=../report/'])
Step 2: Convert the test report data source file json to html report from the report folder
os.system('allure generate ../report/ -o ../report_html/')
Continuous building
Knowledge point: jenkins build + allure test report
1.Jenkins environment setup
2. Download the allure report plug-in
3.Build
a) Build: Use the pytest command to execute the script and generate allure source data
b) Post-build operation: add allure report (jenkins will automatically combine allure source data into an html report)
result
The following are supporting learning materials. For those who are doing [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!