This is the 12th day of my participation in Gwen Challenge
The higher your test coverage, the more flexible and bug-resistant your code will be, and the less time you’ll spend debugging hot fixes in production.
The higher the test coverage, the more flexible the code, and the less time it takes to debug fixes in production.
First, it’s a shame that Postman isn’t great for concurrent testing, but it does support serial testing with specified times and intervals.
Official documentation link: Postman Test Scripts
Postman request process
Postman test configuration
Testing allows configuration in the Collections/Folder/Request configuration in the Collections/Folder we unified for more convenient interface for testing.
A Postman Test is a piece of JavaScript code that executes after receiving a Response from a request. Postman runs the test script after sending the request and receiving the response from the server. In the Postman Request Builder, the Request section contains a Tests tag and the return section contains a Test Result tag. On the right side of the Tests TAB, some common code snippets are listed to aid in writing.
Write test code using the PM API: PM.* API
pm.test()
- use
pm.test()
The function can be inPostman test SandboxTo write test specifications. Using this function allows you to name certain tests accurately and ensures that one error in the test script does not block the rest of the execution. pm.test()
Two arguments are accepted :(string testName, A Function witch resturn Boolean value).
// example using pm.response.to.have
pm.test("response is ok".function () {
pm.response.to.have.status(200);
});
// example using pm.expect()
pm.test("environment to be production".function () {
pm.expect(pm.environment.get("env")).to.equal("production");
});
// example using response assertions
pm.test("response should be okay to process".function () {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody("");
pm.response.to.not.have.jsonBody("error");
});
// example using pm.response.to.be*
pm.test("response must be valid and have a body".function () {
// assert that the status code is 200
pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants
// assert that the response has a valid JSON body
pm.response.to.be.withBody;
pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed
});
Copy the code
PM.* Auxiliary functions
- The Pm.expect () assertion function builds on the popular JavaScript test library, ChaiJS BDD, to write readable tests.
- The pm.response.to.be.* function simplifies assertions. Use this set of assertions to simplify testing of response state types and body variants.
After Test
- After executing the request, under the TestResutl TAB, you can see if the test passed.
- Use Collection Runner to see if the request passes the test in real time.
Automated testing
You need command line tools to integrate with continuous integration tools or continuous delivery tools (such as Jenkins or Travis CI) to automate your tests.
Test the code sample
// Set environment variables
pm.environment.set("variable_key"."variable_value");
// Set the nested object as an environment variable
var array = [1.2.3.4];
pm.environment.set("array".JSON.stringify(array, null.2));
var obj = { a: [1.2.3.4].b: { c: 'val'}}; pm.environment.set("obj".JSON.stringify(obj));
// Get environment variables
pm.environment.get("variable_key");
// Get the environment variable (whose value is a stringed object)
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.
var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));
// Clear environment variables
pm.environment.unset("variable_key");
// Set global variables
pm.globals.set("variable_key"."variable_value");
// Get global variables
pm.globals.get("variable_key");
// Clear global variables
pm.globals.unset("variable_key");
// Get variables: This function searches for variables in global variables and in the active environment
pm.variables.get("variable_key");
// Check whether the response body contains a string
pm.test("Body matches string".function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
// Check whether the response body is equal to the string
pm.test("Body is correct".function () {
pm.response.to.have.body("response_body_string");
});
// Check the JSON value
pm.test("Your test name".function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
// Whether the content-type exists
pm.test("Content-Type is present".function () {
pm.response.to.have.header("Content-Type");
});
// The response time is less than 200 ms
pm.test("Status code is 200".function () {
pm.response.to.have.status(200);
});
//Code name contains a string: checks that Code name contains the specified string. Network basic knowledge is not good, do not quite understand this sentence Orz
pm.test("Status code name has string".function () {
pm.response.to.have.status("Created");
});
// Successful POST request status code
pm.test("Successful POST request".function () {
pm.expect(pm.response.code).to.be.oneOf([201.202]);
});
// Get JSON data using TinyValidator
var schema = {
"items": {
"type": "boolean"}};var data1 = [true.false];
var data2 = [true.123];
pm.test('Schema is valid'.function() {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});
// Decodes base64 encoded data
var intermediate,
base64Content, // assume this has a base64 encoded value
rawContent = base64Content.slice('data:application/octet-stream; base64,'.length);
intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
pm.test('Contents are valid'.function() {
pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
});
// Send an asynchronous request
pm.sendRequest("https://postman-echo.com/get".function (err, response) {
console.log(response.json());
});
// Convert the XML body to a JSON object
var jsonObject = xml2Json(responseBody);
Copy the code