[Interface automated testing -- part 1]

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
image.png

1.2 HTTP protocol

  1. The HTTP protocol consists of:
  • request: line, header, body
  • Response: status line, response header, response body
  1. Common request methods: query GET add new POST modify PUT delete DELETE
  2. 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==

image.png
image.png
image.png
image.png
image.png
image.png
image.png

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
  1. traditional interface style

image.png

  1. Restful

image.png

2. Tools implement interface testing

Postman interface debugging assertion automatic association

  1. Get the interface project, test the business interface first , and then consider a single interface
  2. How to test the business interface
    1. Sort out the business path according to the business flow chart
    2. Design test cases to cover every business path
  3. How to parse interface documentation
    1. Analyze dependencies between interfaces
    2. Analyze interface requests (for example: URL request method request header request parameter type request parameter==)
    3. Analyze the interface response (for example: response status code response data==)

Example: extract test points --> find business path
image.png

Analyzing interface documents
Test case design
image.png
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
image.png
image.png
image.png

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}}
image.png
image.png
image.png
image.png
Example of
image.png
how Postman submits query parameters

  1. set at URL
  2. Set the Params area

batch execution

Run test cases in batches by running the test set

  1. Click the "run" button in the test set to run test cases in batches
  2. The Collection Runner window pops up, click the Run button
  3. view test results

image.png

single interface test

Forward: Required parameters All parameters
Reverse: Empty type error length error rule does not match

image.png
Extract test points
image.png
Test case
image.png
postman for interface testing
image.png

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

image.png

Postman parameterization

Scenario: The test script only tests different data, use parameterization to improve script reuse
Steps:
image.png
How to achieve parameterization

  1. Prepare data files
  2. reference data file
    1. Request parameters get { {variable name}}
    2. Get data. variable name in the code

3. Code implementation interface automation testing

Pyhton Requests Pytest Allure and interface testing framework
image.png

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)


image.png

Interface Automation Framework

image.png

Project directory structure

image.png

Requests library

A "browser" in Python, a urllib-based HTTP library

// 安装
pip3 install requests

    // 验证
    pip3 show requests

    //操作步骤
    // 1 导包
    // 2 发送接口请求
    // 3 查看响应数据

image.png
image.png
image.png

// 需求 登录

//导包
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)
    

image.png
image.png

image.png

Encapsulation interface test script layer

Test data preparation, assertion and business processing ==
image.png

project configuration file

image.png
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
image.png

// 生成测试结果文件
//安装
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

image.png
image.png

Continuous integration CI/CD (to be added)

Jenkins Git GitHub

Advanced (to be added)

PyMysql operation database
Mock test interface encryption and decryption test

Guess you like

Origin blog.csdn.net/Kaka_csdn14/article/details/131612905