Your First Test

Your First Test

In Postman, you should see a tab named Tests. When you click on that tab, you should see an editor where you can write tests. Postman provides us with Snippets so that you don’t actually write a single line of test code. In this example, let’s test hackerhero.com for a status code of 200.

Scroll down to Status code: Code is 200 snippet. Click on the snippet. See screenshot below:

Clicking on the snippet will paste in a test code in the editor. Click Send and see the test result in the Test Results tab:

But in writing tests, we want to make sure that tests FAIL. Let’s change the URL to: https://www.hackerhero.com/foo/bar/baz and hit Send. You should be able to see a different Test result this time.

The Syntax

pm.test - This is the function for writing test specifications. All the tests that you will be writing will start with pm.test.

The first parameter of pm.test - In the example above, “Status code is 200” is the name of the test. It is the first parameter of pm.test. It should be a string. 

pm.response - Postman’s assertion API. We can get different kinds of response objects from pm.response like status code, headers, or body.

Using Expect

Using the pm.expect syntax gives your test result messages a different format. The expect syntax kind of reads like Chai’s expect statement. Example:

pm.test(“Status code is 200”, function () {
    pm.expect(pm.response.code).to.equal(200);
});


Next