Benefits of testing

Everyone agrees that testing is important, but not everyone does it. Whenever you add new code, testing ensures that your API works as expected. With Postman, you can write and run test scripts for all apis.

Tests in Postman

In Postman, you can use dynamic variables, pass data between requests, and write tests by adding scripts to requests. Code written in pre-request Scripttab is executed before the request is sent, while code written in Teststab is executed after the response arrives.

Test Results

Write the test

Example:

pm.test("response is ok".function () {
    pm.response.to.have.status(200);
});


pm.test("environment to be production".function () { 
    pm.expect(pm.environment.get("env")).to.equal("production");
});

pm.test("response must be valid and have a body".function () {
     pm.response.to.be.ok;
     pm.response.to.be.withBody;
     pm.response.to.be.json;
});

var schema = {
	type: 'object',
	properties: {
		appID: {
			type: 'number',
		},
		avatar: {
			type: 'string',
		}
	}
}
pm.test('Schema is valid'.function() {
  var jsonData = pm.response.json().data;
  pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
Copy the code

pm.test()

To comply with postman’s specification, all test code must be written inside the function of pm.test(). The first argument is the name of the test, and the second argument is a function that returns a Boolean value. That is, if this function returns true, the test passes, otherwise the test fails.

pm.expect()

This assertion function is built on a JavaScript test library, ChaiJS BDD. With similar syntax, you can easily assert data or variables in a response.

pm.response.to.be. *

This object provides common checks on the response. For example, whether the status code meets the expectation.

tv4.validate()

The first parameter is the data to be validated, and the second parameter is the JSON Schema. Returns true on pass, false on failure. If you want to learn quickly the json schema specification, you can refer to https://www.jianshu.com/p/8278eb2458c4?winzoom=1 tv4’s postman a built-in json validator. It is based on JSON-Schema Draft V4 and uses rich validation syntax to validate simple values or complex JSON objects.


Commonly used API see https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference for details

Run the test

We usually run tests for multiple apis, because we have many apis, or a test scenario consists of multiple apis. So we’ll add them to a collection to run.

Postman (User Interface)

Notice here that the requests are executed linearly in the order of Collections. If you want to change the execution order, you can change the order in Collections or use postman.setNextrequest (“request_name”); If you want to stop the entire process, you can execute postman.setNextrequest (null);

Newman (Command Line Tools)

1. Export Collections in JSON format from Postman

npm install -g newman
newman run sample-collection.json

See Newman for details

Combined with Jenkins automation

A common solution is to execute Newman -c jenkins_demo.postman_collection –exitCode 1 after the code is committed or merged into the repository branch. Note the –exitCode argument, which tells Jenkins that if the test fails, an exitCode of 1 will be returned. That way Jenkins can see if the test run was successful or not.

conclusion

We all know that to do a good job, you must first sharpen your tools. Postman is a great tool for API-based testing. Postman is also a popular HTTP debugging tool for Web developers. Mastery of it can also improve our work efficiency to a certain extent.