# How to use files to store data used by test cases

**URL:** <https://community.postman.com/t/how-to-use-files-to-store-data-used-by-test-cases/55687>\
**Category:** Help Hub\
**Tags:** tests, file\
**Created:** [December 14, 2023, 7:51pm UTC](https://community.postman.com/t/how-to-use-files-to-store-data-used-by-test-cases/55687 "2023-12-14T19:51:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickgrenier](https://avatars.discourse-cdn.com/v4/letter/p/5daacb/32.png) [@patrickgrenier](https://community.postman.com/u/patrickgrenier)\
**Post date:** [December 14, 2023, 7:51pm UTC](https://community.postman.com/t/how-to-use-files-to-store-data-used-by-test-cases/55687/1 "2023-12-14T19:51:34Z")

</div>

I want to run a test case, where the result of the call should be compared to another result.

```auto
// Check that the response body matches the required response
pm.test("body is correct", function () {
    pm.expect(pm.response.json()).to.eql("Response Test");
});

```

However, this response text is a very big string and should probably not be simply pasted in the test code, but saved in a file.

How can I do something like this?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [December 15, 2023, 9:53am UTC](https://community.postman.com/t/how-to-use-files-to-store-data-used-by-test-cases/55687/2 "2023-12-15T09:53:01Z")

</div>

Hey @patrickgrenier 👋

Welcome to the Postman Community! :postman:

You _could_ store that in a variable, away from the test code and reference that in there?

For example:

```auto
let matchedString = pm.variables.get('matchedString');
// Check that the response body matches the required response
pm.test("body is correct", function () {
    pm.expect(pm.response.json()).to.eql(matchedString);
});

```

You could add it to an external _datafile_ and have a placeholder reference in the test but that’s only going to work in the Collection Runner context.

> **[Run collections using imported data | Postman Learning Center](https://learning.postman.com/docs/collections/running-collections/working-with-data-files/)**
>
> Run collections using imported data: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

Using [Newman as a library](https://github.com/postmanlabs/newman?tab=readme-ov-file#using-newman-as-a-library) and creating a script could be another option, the response could be in a separate file and you can inject that into the Collection with the Newman script. That’s a bit more _involved_ though.

---

<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:** [December 15, 2023, 10:17am UTC](https://community.postman.com/t/how-to-use-files-to-store-data-used-by-test-cases/55687/3 "2023-12-15T10:17:02Z")

</div>

Testing the whole response like this in one go can be difficult to troubleshoot if\when it fails.

If there is more than one issue in the response, you won’t get the feedback on all of the elements that have issues.

Even if you only have one issue, that will probably be also difficult to ascertain from the failing test where exactly its failing.

All you will really know is that the two don’t match somewhere.

A well designed test should tell you exactly what is wrong.

You might want to consider jsonSchema tests to confirm that the structure of the response is correct.

Personally, I would have separate tests for each element in the response that is important to me.

I would minify the JSON for the expected result, and then store it as a collection variable.

Depending on the JSON structure, it might be one loop or multiple loops through that collection variable checking each element. This will then make the tests individual and provide feedback on all failing elements.

Of course, I don’t know how large your response is, so this might be unfeasible due to size.

Do you have an example response, so I can show how this might work in practice?

---

<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:** [January 14, 2024, 10:17am UTC](https://community.postman.com/t/how-to-use-files-to-store-data-used-by-test-cases/55687/4 "2024-01-14T10:17:40Z")

</div>

Closed on January 14, 2024, 10:17am UTC

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