# Negative testing with json data file

**URL:** <https://community.postman.com/t/negative-testing-with-json-data-file/57854>\
**Category:** Help Hub\
**Tags:** collections\
**Created:** [February 20, 2024, 10:19am UTC](https://community.postman.com/t/negative-testing-with-json-data-file/57854 "2024-02-20T10:19:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atish.Sonkusale](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@Atish.Sonkusale](https://community.postman.com/u/Atish.Sonkusale)\
**Post date:** [February 20, 2024, 10:19am UTC](https://community.postman.com/t/negative-testing-with-json-data-file/57854/1 "2024-02-20T10:19:20Z")

</div>

Hi

I am testing basic login authentication API

=== API ===

```auto
{
   "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===

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

```

=== response body ==

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

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 20, 2024, 12:01pm UTC](https://community.postman.com/t/negative-testing-with-json-data-file/57854/2 "2024-02-20T12:01:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Atish.Sonkusale](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@Atish.Sonkusale](https://community.postman.com/u/Atish.Sonkusale)\
**Post date:** [February 21, 2024, 5:28am UTC](https://community.postman.com/t/negative-testing-with-json-data-file/57854/3 "2024-02-21T05:28:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 21, 2024, 11:18am UTC](https://community.postman.com/t/negative-testing-with-json-data-file/57854/4 "2024-02-21T11:18:53Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [March 22, 2024, 11:19am UTC](https://community.postman.com/t/negative-testing-with-json-data-file/57854/5 "2024-03-22T11:19:04Z")

</div>

Closed on March 22, 2024, 11:19am UTC

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