Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement in Postman tests

I work in Postman and I want to build the test construction like that:

var login_status = pm.environment.get("LOGIN_STATUS");
var jsonData = pm.response.json();
pm.test("TESTS", function () {
    if (login_status === 1) {
        pm.expect...(test 1);
    }else if (login_status === 2) {
                pm.expect...(test 2);
    }else if (login_status === 3) {
                pm.expect...(test 3);

but it doesn't work for me as I want.

In my logic, Postman should check the login status and use only one pm.expect... accordingly to login_status value.

How can I use the if statement in Postman?

like image 391
Evgenii Truuts Avatar asked Aug 08 '18 10:08

Evgenii Truuts


People also ask

How do you write if condition in Postman test?

You could make use of pm.environment.name here to check the active environment name, rather that a value within it: if(pm.environment.name === 'local') { // do something... } else if (pm.environment.name === 'develop') { // do something... } else { // do not do anything... }

Can we do assertions in Postman?

Postman tests can use Chai Assertion Library BDD syntax, which provides options to optimize how readable your tests are to you and your collaborators. In this case, the code uses BDD chains to.have to express the assertion. This test checks the response code returned by the API.

How do you test a function in Postman?

You can add tests to individual requests, collections, and folders in a collection. Postman includes code snippets you add and then change to suit your test logic. To add tests to a request, open the request and enter your code in the Tests tab. Tests will execute after the request runs.

How do you validate a response body in the Postman?

Go to the Tests tab on the request page. Now write the tests for validating the GET request: Let us consider an example for writing a test to this GET request, we have to check the response code is 200, and check the response body has the first name “Janet” present. tests[“Validate Status Code”] =responseCode.


1 Answers

The comparison operator you are using is returning false as it's checking for the same type, try using == rather than ===.

The values stored in the Postman environment file is a string and you're doing a strict equal against a number, in the test.

You could also try changing the number value to a string value like this login_status === "1" or use strict inequality so the type you use doesn't matter, like this login_status !== 2.

You could even just use a parseInt() function when you get the value from the file.

parseInt(pm.environment.get("LOGIN_STATUS"))

It's completely up to you and the way you want to construct the check.

More information about the comparison operator can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity

like image 177
Danny Dainton Avatar answered Oct 02 '22 08:10

Danny Dainton