The use of interface testing tools in Python interface testing

Thanks for taking out

.

.

read this article

1. Use of packet capture tools

1. Introduction to Packet Capture Tool

  • Chrome/Firefox Developer Tools: Built-in browser, easy to use

  • Fiddler/Charles: proxy-based packet capture, powerful, can capture packets on mobile phones, simulate weak networks, intercept requests, and customize responses

  • Fiddler: Free, only supports Win

  • Charles: Free, support Win/Linux/Mac

  • wireshark/tcpdumps: Packet capture for the network card layer, with a large amount of data, can capture packets of various protocols such as tcp/udp (requires filtering)

  • wireshark: Support Win/Linux/Mac

  • tcpdumps: Linux packet capture command, powerful, often used for server packet capture

1.1. What is a proxy

forward proxy

Forward proxy and reverse proxy

  • In forward proxy, the proxy and client are in a local area network, transparent to the server

  • In a reverse proxy, the proxy and the server are in a local area network, transparent to the client

  • For example, using a proxy to access Google is a forward proxy, and requesting different websites from the same server through Nginx through different domain names is a reverse proxy

  • Reverse proxy can do load balancing

  • The reverse proxy interface test generally needs to bind the local hosts first

1.2. How to manually add a proxy

  • Start the proxy server, such as opening Postman's proxy service (local ip, default port 5555)

  • Win settings -> proxy -> configure proxy ip and domain name

1.3. Mobile phone setting Internet proxy (mobile phone packet capture)

  • Laptop and mobile use the same wifiInternet access

  • Start the proxy server on the laptop, such as Postmanthe open proxy service (local machine ip, default port 5555)

  • Long press on the phone to wifi->select Manage Network or Advanced ->Manual Proxy ->Configuration Configure proxy ipand domain name

1.4. Bind hosts (for reverse proxy)

  • Win:notepad C:\Windows\System32\drivers\etc\hosts

  • Linux: vim /etc/hosts(format: ip domain name)

2. Chrome Developer Tools

  • Elements: HTML element panel, used to locate and view the source code of elements;

  • Console: js console panel, js command line, view front-end logs;

  • Sources: resource panel, used to debug js with breakpoints;

  • Network: Request information panel, view request and response information;

  • Timeline: A timeline panel that records various events that occur during the website's life cycle;

  • Profiles: event details panel;

  • Application: local storage, session storage and other resource information;

  • Security: Determine whether the current web page is safe;

  • Audits: Network performance diagnosis;

1. Network panel

Network panel

1.1.Console: Appearance and function control

  • Record: Record or stop recording request

  • Clear: clear all requests

  • Grab Snapshots: Capture screen events frame by frame

  • Filter: Please use to turn off the filter function

  • search: search request

  • Group by frame: Group by frame

  • Preserve log: Preserve requests when the page is reloaded

  • Disable cache: disable cache

  • Offline: Network disconnection and weak network simulation

  • Filters: request filters

  • Overview: Resource Timeline

1.2.Filters: request filters

1.3.Overview: Resource Timeline

1.4.Requests Table: Request list

  • Name: resource name

  • Status: HTTP status code

  • Initiator: request source

  • Size: Downloaded file and requested resource size from the server. If the resource is obtained from the cache, this column will display (from cache)

  • Timeline: Displays all network request timeline status axis

1.5.Summary: total number of requests, data transfer volume, loading time information

  • DOMContentLoaded: The DOM on the page is completely loaded and parsed;

  • load: all DOM, CSS, JS, and images on the page are completely loaded;

导出请求:右击请求 -> Copy -> Copy as fetch / Copy as cUrl

3. Introduction to Fiddler

Fiddler下载:https://www.telerik.com/download/fiddler

1. Why use Fiddler

  • The request data can be captured, and the Rawformat/form format can be viewed /Json/XML;

  • Can intercept and modify requests;

  • more powerful filters;

  • PostmanThe request sent by the script/interface script can be captured , which is convenient for debugging;

  • Can capture mobile phone requests…

2.Fiddler main interface

Fiddler main interface

2.1.Inspectors: Inspectors

  • Raw: the raw format of the request

  • WebForm: Requested form format

  • Json: The requested Json format request

  • XML: The requested XML format

2.2.AutoResponsder: Automatic reply, can be used to construct response, Mock, do not modify the server file debugging interface

2.3.Composer: Designer, send and debug requests

2.4.Filters: Filters

  • Hosts: filter by server

  • Clients Process: filter by client program

  • Request Headers: filter by request headers

  • Breakpoints: set breakpoints

  • Response Status Code: filter by status code

  • Response Type and Size: filter by response type and university

  • Response Headers: filter by response headers

3. Automatic breakpoint setting

menu Rules ->  Automatic Breakpoints -> Before Requests/After Requests

4. Mobile phone capture

The installed fiddlernotebook and mobile phone use the same wifi -> long press on the mobile phone wifi, select advanced -> add proxy ipas notebook ip, port is 8888 -> notebook open fiddler, mobile phone access webpage

4. Use of Postman

1. Common interface testing tools

  • Postman: Simple and convenient interface debugging tool for easy sharing and collaboration. With interface debugging, interface set management, environment configuration, parameterization, assertion, batch execution, recording interface, Mock Server, interface documentation, interface monitoring and other functions

  • JMeter: Open source interface testing and stress testing tool, supports Linux and interfaceless operation

  • LR: Commercial interface performance testing tool, easy to use and powerful

  • SoupUI: Open source, commonly used test tool for WebService interface, can also test Rest interface and interface security

3. Introduction to the use of Postman

Official website address: https://www.postman.com/


Postman main interface

3.1. Toolbar

  • New: New, you can create new Request requests, Collection request sets, environments, etc.

  • Import: Import, you can import the request set exported by others

  • Runner: run a set of requests (batch execution)

  • Invite: Invite (requires registration, invites to collaborate)

  • Sync icon: (requires registration, sync your project to the cloud)

  • Packet capture icon: packet capture/capture request, used to enable Postman proxy, after manually setting the proxy (or mobile proxy), you can capture/record request

  • Settings Icon: Postman Settings

  • Message Icons: Official and Assistance Messages

  • Favorite Icons: My Favorites (requires registration)

  • Cloud icon: User cloud data (registration required)

3.2. Interface management area

  • History: Request history, you can query previous request records

  • Collections: An interface set, which is equivalent to an interface project or test plan. An infinite subfolder can be created in the interface set to manage the interfaces in groups.

3.3. Environmental management area

  • Environment switch: used to switch the environment

  • Environment Preview: for a quick preview of all variables in the environment

  • Environment management: used to add and modify environment and environment variables, as well as global variables

3.4. What is the environment

接口完整地址 = 服务地址 + 接口地址,如
www.sojson.com + /open/api/weather/json.shtml

An environment is a set of configurations that contain many environment variables. In the interface test, the server address may be different according to the deployment on different servers, but the interface address of the same interface is unchanged. In order to test the same set of interfaces deployed on different servers, we can create different environments, and the host variables in different environments use different addresses

3.5. Interface Design Area

You can create multiple new requests through the number on the upper tabside . +The interface design area is divided into request area and response area from top to bottom

3.5.1. Request area

  • Request address line: You can select the request method (GET/POST/…), fill in the request address, send the request and save the request to the test set

  • Request data area: divided into authorization, request header, request data, script executed before the request is sent (for preparing data), and script executed after the request (for assertion)

3.5.2. Response area:

  • Response content: You can view Pretty (beautified format), Raw (original format), Preview (HTML preview format)

  • response cookie

  • response header

  • The test result, corresponding to the assertion set in Tests in the request

Postman main interface function

3.5.Collection request set

The test set is Postmanan "overall" unit of interface management in China. Running, exporting, and sharing are all based on the test set.

  • New test set: Newbutton -> Collectionor directly click the new test set button above the test set list

New test set

  • Authorization: The interfaces in the test set and its subfolders use this authorization uniformly, and each interface does not need to be set separately

  • Pre-request script: The pre-request script common to each interface of the test set

  • Post-request assertions: post-request scripts common to each interface in the test suite

  • Request set variables: some variables common to the request set

3.6.Collection subfolder

The properties of the subfolder also have description, authorization, pre-request script, and post-request assertion (no variables, unified management of variables of a request set), which implements the Fixture function of different scopes (Scope).

  • Request set export: The request set can be exported and sent to others (without carrying environment information), and others can use your interface by importing

  • Request set sharing: The request set is directly shared with others (both parties need to register

3.7. Environmental management

New environment


We can set multiple variables in the environment for use in the request
Environment variable usage method:
select the environment, use it in the request URLor the request to Bodyuse { {变量名}}the environment variable, the variable can be used in Bodyvarious formats of the request, but not directly in the request Used in pre-scripts Pre-request Scriptand post-request scripts Tests.

Use of environment variables


In the environment management, you can also click "Global" to add global variables. The environment variables will only take effect when the environment is selected. The global variables will take effect in any environment. The variables in the test set will only take effect in the current test set. When the test set variables, environment variables , when the global variable has repeated variable names, the priority is: environment variable > test set variable > global variable.

3.8.Params usage

When there are many parameters in the request URL, it is inconvenient to add and view. You can click URLthe button behind the input box Paramsto add variables and values ​​in the form of a table. After adding from the table, the variables and values ​​will be automatically added to URLthe

Add URL parameters

3.8.1. Request Design

  • Authorization: If the interface requires authorization, you can set the authorization method (type) and authorization information on this page

  • Header: Request header, you can set the request type (Content-Type) and Cookie

  • Body: request data
    form-data: mixed form, supports uploading files
    x-www-form-urlencoded: text form
    raw: raw format, supports JSON/XML format (optional later)
    binary: binary format, used to send binary data stream

  • Pre-request Script: Pre-request script, Javascript syntax, used to generate some dynamic data or do some processing before sending a request

  • Tests: post-request script, Javascript syntax, used to do some processing or assert the result after the request returns

3.8.2. Methods for Postman to send requests in various formats

Note: Selecting a different request, however, will automatically Headeradd Content-Typeinformation in

Hybrid form request

traditional form request

JSON format request

XML request

3.8.3. Tests assertions

  • HTTP Status Code Assertion

tests["HTTP状态码200"]=responseCode.code == 200;
  • Response contains content assertion

tests["状态码200"] = responseBody.has("登录成功");
  • example

Interface example

Assertion method

  • JSON response assertion

var jsonData = JSON.parse(responseBody);
tests["code为200"] = jsonData.code==200
tests["msg为success"] = jsonData.msg == "success"
GET接口样例:
http://www.tuling123.com/openapi/api?key=ec961279f453459b9248f0aeb6600bbe&info=你好

JSON response assertion

3.8.4. Runner test set batch execution

  • Support setting the number of iterations

  • Support loading csv or json test data

  • Create a new Collection, such as the name Demo2

  • Fill in the URL:

    https://demo.fastadmin.net/admin/index/login.htm, selection POSTmethod

  • The request data Bodyformat is selected x-www-form-urlecoded, and the request data is filled in key : username, value:{ {username}}  , key : password, value:{ {password}} , here two variables are used for parameterization

request settings

  • Save the request to Demo2

  • Create a new file on the computer data.csv, the first line is the variable name, and the following is the data, as shown below

data file

  • Click Postmanthe Runnerbutton on the toolbar, Collectionselect Demo2Dataselect the data file data.csv, and click RunDemo2

Runner configuration

operation result

Finally, I would like to thank everyone who has read my article carefully. Watching the rise and attention of fans all the way, there is always a need for ritual exchanges. Although it is not a very valuable thing, if you can use it, you can take it directly.

These materials should be the most comprehensive and complete preparation warehouse for friends who do [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you! Everything should be done as early as possible, especially in the technology industry, and the technical foundation must be improved. I hope to be helpful…….

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/124247797