Negative testing with json data file

Hi

I am testing basic login authentication API

=== API ===

{
   "username": "string",
   "password": "string"
}

I am testing this API with 4 records with JSON but only one (1st) record have valid username and password.
As a negative testing other three records also valid as they are failing , but when we run the test only 1st record shows as pass and other shows fail (which is expected but in -ve testing it should come as pass )

===test script===

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

=== response body ==

{
  "isAuthenticated": true,
  "userIdentity": "string",
}

how can i perform validation on the test script so that I can get proper result???

You canโ€™t really test for two different outcomes on the same request.

One will always pass, and the other will fail.

You can add conditional logic to your code, but I wouldnโ€™t recommend this approach.

I would however recommend creating two separate but identical requests, one for your positive tests and valid username\password, and one request for your negative tests with invalid authentication details. You will then craft appropriate tests for the relevant status code and response details.

Hey Mike

Thanks for the response.
In that case we need to create the copy of the each API one for positive test and one for negative test.
Is it possible if we can pass extra variable with JSON and that can be pull in the test for comparison , that will make it easy

You donโ€™t need to create a copy of the API, just the request.

However if you are using a data file and the collection runner then you have some flexibility on what you can do.

If you use a CSV, I would create columns for test case name, username, password and expected status code. If you use JSON, then this need to be key\value pairs.

You can then write a test that uses the test case name, and expected status in the assertion.

For example.

pm.test(`${data.testName}`, function () {
    pm.response.to.have.status(${data.statusCode});
});

This uses interpolation to create the custom test case name. The backticks & ${}. You can access the current iteration with the special data variable, so it would be data.columnName or data.keyName.

This would mean that you can test for different status codes with a single request.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.