concept

What is an interface test?

The explanation given by Baidu Encyclopedia is: interface test is a test to test the indirect interface of system components. Interface testing is mainly used to detect the interaction points between external systems and internal subsystems. The focus of the test is to check the data exchange, transfer and control management process, as well as the logical interdependence between systems.

Interfaces include internal interfaces and external interfaces:

Internal interfaces: Interfaces developed by developers to their own systems.

External interface: development system calls external, such as: wechat, Alipay, other interfaces.

In software testing, interfaces are called apis to realize data interaction. The essence of an interface test is to test whether the interface can normally interact with data, control permissions, and perform abnormal scenarios.

Common interface testing tools

The commonly used interface testing tools are Postman and JMeter

Interface return data

The format of data returned by an interface is JSON, HTML, and XML

1. Json format (market share more than 85%)

Generally there are three sets of data: {” code “: 200, the” message “:” request is successful, “” dataMap” : {“…” }

Code: Indicates the return status code

Message: an explanation of returned information or a status error

DataMap: The actual returned data

2. HTML format

<html>
    <title></title>
    <body>
        <code>200</code>
        ......
    </body>
</html>
Copy the code

3. XML format

<? xml? Version = "1.0" encoding = "utf-8" > 200 < / code > < code >... </xml>Copy the code

Interface test protocol

1. The webservice protocol:

Interface address: http://…… ? wsdl

2. The dubbo protocol:

The interface address is dubbo://……

Suitable for transmission of small amounts of data

3. HTTP (90%) :

Interface address: http://…..

The HTTP port is 80

HTTPS = HTTP + SSL. The port number is 443

HTTP protocol is mainly used for data transmission in the market, so we mainly learn this protocol.

What is the HTTP protocol?

HTTP is a hypertext transfer protocol used to transfer data between a browser and a server. The interaction consists of two parts: request and response.

Requests: GET, POST, PUT, delete

The request section typically contains:

1. Request line: request mode, request address and protocol

2. Request headers: The HTTP protocol uses HTTP headers to pass meta information about the request. The HTTP header is a colon-delimited name/value pair preceded by the HTTP hair name and followed by the HTTP value, for example:

Accept: Application /json — Data format that can be received by the client

X-requested-with: XMLHttpRequest — asynchronous request

User-agent: — Indicates the user of the client

Host:– Address of the requested Host

Cookie: — Cookie information (requested)

Accept-encoding: gzip, deflate, BR — Compression mode

3, empty line: send return and return, notify the server that there is no longer a request header;

4, message body: HTTP request with query string, if it is GET method, query character or form data added value request line, then there is no content in the message body; If the POST method is used, the string or form data is queried and added to the message body.

Response: The response status code is described here

2xx: indicates that the request is successfully sent.

3xx: Resources are transferred, commonly known as redirection.

4xx: indicates that the interface path cannot be found and the client is faulty.

5XX: indicates an internal system exception, such as interface defects, incorrect request content, or server errors.

Part of the response:

1. Response line: protocol, response code and response information

2. Response header:

Server: nginx – services

The date – time

content-type: application/json; charset=UTF-8

Set-cookie: — responsive

3. Empty one line

4. Specific content of the response

Install the postman

1, enter the postman’s official website www.postman.com/downloads/

2, according to their own computer configuration download the relevant installation package

3. The installation is successfully opened

To refine the postman page:

Params: Used to pass parameters in GET requests

Authorization: Postman Authorization function

Headers: request head

Body: Post requests parameters to be passed

— none: No arguments

— form-data: both files and key-value pairs

X-www-form-urlencoded: Only key-value pairs are transmitted

— RAW: Create JSon, TXT, XML, HTML, JS transfers

— binary: Transfers the file in binary mode

Pre-request Script: a Script written in JS format before the interface requests it

Tests: code for assertions

The response part

Body: Returned data

— Pretty: display in JSON format

— Raw: Displays in text format

— Preview: Show it as a web page

Cookie: Returned cookie information

Header: Response Header

Test Results: Asserts the result

Built-in dynamic parameters

{{$timestamp}}

{{$randomInt}}

Generate a GUID string: {{$GUID}}

Environment variables and global variables

Among enterprises, general development environment, test environment and the online environment, some enterprises have pre-release environment, interface test will find that we were doing, these environments the address in addition to the IP address of the interface is different, other places are the same, we in the actual test, can’t write a test each environment logic, In this case, you need to use environment variables, which are global variables.

Add the corresponding IP address variables to the environment variables of three new environments:

Parameterize the IP address in the interface address format as {{IP}} and try to run it once:

interface

In practice, some variables change dynamically, and the next interface requests the value of that parameter, such as token.

We need to obtain the token value in the response of the previous interface, and set the value as a global variable, and then pass the value in the form of parameterization in the next interface to realize the interface association.

1. Interface association method 1: JSON extractor

Var jsValue = json.parse (responseBody); Log (jsValue) // Extract the token value and save it to the global variable pm.globals.set("token", jsvalue.token);Copy the code

Postman also provides common Tests writing methods, as shown in the following figure:

2. Interface association method 2: regular expression extractor

{"id": 0, "id": 0, "id": 0}}Copy the code

Responsebody.match (new RegExp(‘”id”:(.+?)); ,’)), this method matches “id”: and before, if the result is printed to the console, as an array: Responsebody. match(new RegExp(‘”timestamp”:(.+?)) [1], ‘)).

3. Interface association method 2: Cookie extractor

As shown in the figure below, we need to extract the values in the response result Cookies and print the results to the console.

The method is as follows:

/ / in the extraction of Cookies SERVERID Value Value and stored in cookies_serverid variables var cookies_serverid = postman. GetResponseCookie (" SERVERID "). The Value // Print the value in the variable cookies_serverID to console console.log(cookies_serverID)Copy the code

Batch execution

Through the above operations, we can realize the association between interfaces, to fully achieve automation, need to perform batch execution.

assertions

Postman uses Tests to make assertions. Postman uses Tests to make assertions. There are eight ways to make assertions:

Eight ways to assert:

/ / 1, return a Status code of 200 PM. The test (" Status code is 200, "the function () {PM. The response. To. Have. The Status (200); }); Pm. test("Body matches String "); function () { pm.expect(pm.response.text()).to.include("string_you_want_to_search"); }); Pm.test ("Your test name", function () {var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); }); Pm.test ("Body is correct", function () {pm.response.to.have. Body ("response_body_string"); }); / / 5, and assert that the response header contains a specified response headers PM. The test (" the content-type is the present ", function () {PM. The response. To. Have. The header (" the content-type "); }); Pm. test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); }); Pm. test("Successful Post request", function () { pm.expect(pm.response.code).to.be.oneOf([201, 202]); }); / / 8, assert that return a Status code information contained in the specified string PM. The test (" Status code name from the string ", function () {PM. The response. To. Have. The Status (" Created "); });Copy the code

Global assertion

In general, each interface will assert that the status code returned is 200, so we can set this assertion to global.

Newman

What is Newman?

Newman is the Newman phone. Ha ha, just kidding. Newman is simply the Postman of the COMMAND-LINE Interface.

Newman can be run directly from the command line using Postman’s exported collection files, replacing Postman’s interface operations with the command line. Since it is command line operation, we can cooperate with Jenkins to do automatic interface test.

Install Newman

Newman is written in pure JS, so it needs the NodeJS runtime to run it, so you have to install NodeJS on your computer before downloading Newman.

Command: NPM install -g Newman

How to use it?

To run interface test cases in Postman, you need to export the set of interface test cases, export environment variables and global variables, and save the exported file locally.

Go to the CLI and run the following command:

newman run "E:\testApi\yongli.json" -e "E:\testApi\hunjingbl.json" -g "E:\testApi\quanjubl.json" -r cli,html,json,junit --reporter-html-export "E:\testApi\report.html"

Note: Newman Run is directly followed by test case files, -e by environment variable files, -g by global variable files, and -r by test report output format and file path of output report.

This is the command window that runs successfully:

Open the output test report file:

Jenkins continuous integration

You can search for Jenkins installation on your own baidu

After the installation is successful, create a new project

The project configuration page is displayed

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")

Jenkins can also configure mail, as described in the articleJuejin. Cn/post / 684490…

Afterword.

There are many other aspects of interface testing, such as Python automatic test interface, and the corresponding testing framework, etc., to be successful in the workplace, you must continue to learn, the above content is a simple overview of me as a test master, I hope you can give me more advice.

If this article is of any use to you, please give it a thumbs up!! Refueling oh