For personal information only!
1. Basics of interface testing
Interface definition HTTP protocol interface specification
Interface test process API document interpretation Interface use case design
1.1 URL
URL = protocol + server address + port number + resource path + parameters
1.2 HTTP protocol
- The HTTP protocol consists of:
- request: line, header, body
- Response: status line, response header, response body
- Common request methods: query GET add new POST modify PUT delete DELETE
- Common Response Status Codes
- 2XX : The request is successful, such as 200 201 204==
- 4XX : client error, such as 400 401 403 404==
- 5XX : server error, such as 500 503==
1.3 Interface specification
Classification | traditional style | RESTful |
---|---|---|
request method | Mainly use the Post Get method | Follow the HTTP protocol method definition |
URL | Define resource and action mixins | Define only one resource, does not contain operations on the resource |
status code | Both are 200 | Follow the HTTP protocol status code definition |
- traditional interface style
- Restful
2. Tools implement interface testing
Postman interface debugging assertion automatic association
- Get the interface project, test the business interface first , and then consider a single interface
- How to test the business interface
- Sort out the business path according to the business flow chart
- Design test cases to cover every business path
- How to parse interface documentation
- Analyze dependencies between interfaces
- Analyze interface requests (for example: URL request method request header request parameter type request parameter==)
- Analyze the interface response (for example: response status code response data==)
Example: extract test points --> find business path
Analyzing interface documents
Test case design
Writing test cases
POSTMAN Introduction
Download address: https://www.getpostman.com/ postman download link address
How to use postman to send a request and see the response result
Set request method -> Set URL -> Set request header -> Set request data -> Click Send to send request- >Look at the response status code -> look at the response body data
How to use postman to send request multipart/form-data and see the response result
Set request method -> Set URL -> Set request header -> Set request data**[body->form-data->file select upload file]**- > Click Send to send the request -> see the response status code -> see the response body data
POSTMAN automatically associates data
Postman automatic association solves the need to automatically transfer data between interfaces; **
Postman automatic association implementation ideas:
① Extract associated data pm.response.json()
② Save associated data pm.environment.set()
③ Reference associated data {
{ Variable name}}
Example of
how Postman submits query parameters
- set at URL
- Set the Params area
batch execution
Run test cases in batches by running the test set
- Click the "run" button in the test set to run test cases in batches
- The Collection Runner window pops up, click the Run button
- view test results
single interface test
Forward: Required parameters All parameters
Reverse: Empty type error length error rule does not match
Extract test points
Test case
postman for interface testing
Postman asserts
Assertion function: Let the tool replace the manual to automatically judge whether the expected result is consistent with the actual result.
Common assertion methods are:
- response status code
- contains the string
- JSON data assertion
Postman parameterization
Scenario: The test script only tests different data, use parameterization to improve script reuse
Steps:
How to achieve parameterization
- Prepare data files
- reference data file
- Request parameters get { {variable name}}
- Get data. variable name in the code
3. Code implementation interface automation testing
Pyhton Requests Pytest Allure and interface testing framework
Interface automated testing
Interface automation: A technology that uses tools or codes instead of humans to test interfaces
Test purpose: Prevent new problems from being introduced when developing and modifying codes
Test time:
- Interface automation scripts can be written before development and system testing ;
- After the system test is developed and transferred to the test , the execution of the system test case is given priority, and then the interface automation script is written (in preparation for the subsequent regression test)
Interface Automation Framework
Project directory structure
Requests library
A "browser" in Python, a urllib-based HTTP library
// 安装
pip3 install requests
// 验证
pip3 show requests
//操作步骤
// 1 导包
// 2 发送接口请求
// 3 查看响应数据
// 需求 登录
//导包
import requests
//发送请求
url = "http://test.itkaka.net/api/login"
header_data = {
"Content-Type":"application/json"}
login_data = {
"uname" : "admin",
"pwd":"admin123",
"code":2,
"uuid":"xxx"
}
response = requests.post(url=url,headers= header_data,json=login_data)
//查看响应
print(response.status_code)
print(response.json())
encapsulation interface object layer
// 接口封装时,重点是依据接口文档封装接口信息,需使用的测试数据是从测试用例传递,接口
//方法被调用时需要返回对应的响应结果
// 导包
import requests
//创建接口类
class LoginAPI:
// 初始化
def _ _ init _ _(self):
// 指定url基本信息
self.url_verify = ""
self.url_login=""
//验证码
def get_verify_code(self):
return requests.get(url=self.url_verify)
// 登录
def login(self,test_data):
return requests.post(url=self.url_login,json=test_data)
Encapsulation interface test script layer
Test data preparation, assertion and business processing ==
project configuration file
The main function of the configuration file in the :::info
interface automation framework is to: maintain basic information about the project, such as URL and project path =
:::
Allure report
Official help documentation: https://docs.qameta.io/allure
// 生成测试结果文件
//安装
pip install allure-pytest
// 使用 allure 生成测试报告的步骤
// 1. 修改 pytest.ini 配置文件
[pytest]
addopts = s --alluredir report
testpaths = ./scripts
python_files = test*.py
python_classes = Test*
python_functions = Test*
// 2. 运行pytest 命令产生测试结果文件(json文件)
pytest
// 3. 运行allure 命令生成测试报告
allure serve report
Continuous integration CI/CD (to be added)
Jenkins Git GitHub
Advanced (to be added)
PyMysql operation database
Mock test interface encryption and decryption test