Table of contents
3.2. Judging the response status code
3.3. Determine whether the response body contains a certain string
3.5. Assertion response header
4. Global variables and environment variables
1 Introduction
PostMan, an interface debugging tool.
Features:
-
A history of interface requests can be kept
-
Organization interfaces can be efficiently managed using test set Collections
-
Interface data can be synchronized between teams (for a fee)
2. Manage Use Case Sets
create:
Export:
import:
3. Affirmations
3.1. Overview
Assertion, let the program judge whether the expected result is consistent with the actual result.
PostMan's assertions are written in JavaScript, in the Tests tag.
There are five commonly used assertions:
-
Judgment response status code
-
Determine whether the response body contains a certain string
-
Determine whether the response body is equal to a certain string
-
Assert Json data
-
Assertion response header
3.2. Judging the response status code
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
3.3. Determine whether the response body contains a certain string
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("test");
});
3.4. Assert JSON data
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
josnData.value refers to the key in the json of the response. If it is {"code":200}, it should be written as jsonData.code, and so on.
3.5. Assertion response header
Assert whether a parameter is included in the response header:
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
Assert the value of a parameter in the response header:
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type","application/json;charset=UTF-8");
});
4. Global variables and environment variables
4.1. Overview
Global variables: Globally unique, non-repeatable variables that can be used globally.
Environment variables: PostMan supports defining multiple different environments, and defining environment variables belonging to the current environment in each environment. The environment variables in this environment can only be used when switching to an environment.
4.2. Create environment
Here's how to create an environment:
Click on Environmental Management
Click to add
Add a new environment, and allow to specify the environment variables belonging to the environment when adding.
Once created it can be used:
4.3. Setting variables
Both global variables and environment variables can be manually set on the interface:
Enter Environment Management:
Support manual setting of global or environment variables:
Of course, it can also be implemented by writing code in the tests tag.
Global variables:
pm.globals.set("name",value);
Environment variables:
pm.environment.set("name",value);
4.4. Get variables
Both global variables and environment variables can be obtained through {{variable name}}:
It can also be obtained by writing code in the tests tag:
Get global variables:
var value=pm.globals.get("name");
Get environment variables:
var value=pm.environment.get("name");
5. Pre-script
In Pre-request Script, you can write scripts that are executed before the request:
Here, take sending a timestamp to Baidu as an example. Get the current timestamp in the pre-script, set it into a global variable, and then get the global variable in the parameters and send it.
6. Association
When there is a dependency relationship between interfaces, and interface B depends on the return value of interface A, the PostMan association is used.
The association is realized by using environment variables and global variables. The return value of interface A is stored in the environment variables and global variables, and then interface B can be obtained and used.
case:
Obtain the location according to the mobile phone number, and then check the weather in the area.
View the return result of interface A:
In Tests, use the js script to parse the returned results, and put the city information in the global variable:
Then create another request to request the B interface (weather interface), and the parameters can be taken from the global variables.
7. Batch execution
Before running, it supports configuring some parameters, such as the number of cycles, interval time, etc.
8. Test report
Newman can be used to generate test reports based on postMan, which will not be expanded here. When you want to generate it, just search for newman related content.