L&D series: Trying out simple API tests with Postman
Learning and Development chapter 10.
Many tools for automation. One of it is Postman. Okay let’s get started. We need an endpoint to do this. I’m using https://crudcrud.com/ (I already use this when I create my article here).
Please pay attention to the right, there are snippets. Just click one of those, for an example Status code: Code is 200
. There will be a code displaying in left field. The code will looks like this then try to click Send
button.
pm.test("Status code is 200", function () { pm.response.to.have.status(200);})
Try change the status to be 500 instead of 200.
That’s a simple example to start testing in Postman.
Old syntax
This syntax is deprecated but we can still use this.
tests["Successful POST request"] = responseCode.code === 200;
Syntax example
pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});pm.test("Status code is 201", () => {
pm.expect(pm.response.code).to.eql(201);
});pm.test("The response has all properties", () => {
const responseJson = pm.response.json(); pm.expect(responseJson.type).to.eql('automation');
pm.expect(responseJson.name).to.be.a('api');
pm.expect(responseJson.id).to.have.lengthOf(1);
});
You can check the full syntax example at https://learning.postman.com/docs/writing-scripts/script-references/test-examples/.