2

Recently I started writing tests for our API. However, I have faced some issues which I think it is critical to think at the start.

How should I structure my tests in terms of setup and cleanup phase (test data management)? Should I set necessary objects in a test in pre-req section to make the test independent of others? I followed this approach but I couldn't find a way to send requests synchronously.

On the other hand, I made a different approach which is basically using objects created in previous tests. But this makes the tests unmaintainable and very difficult to keep track of.

Are there any other approaches on test data management? Or should I do things differently?

2 Answers 2

1

Postman offers two main approaches how to manage (test) data, either variables or data files.

It's important to take into account how you want to run your collections. If you use the test runner or newman only, you can go with test files, since they are pretty straighforward in this sense. If you want to run your collection request by request from time to time (perhaps when digging into some kind of problem a bit more using exploratory testing), then those data files are not gonna be much useful to you, you'd need variables in one of those scopes.

Should I set necessary objects in a test in pre-req section to make the test independent of others? I followed this approach but I couldn't find a way to send requests synchronously.

Maybe I didn't understand this part, but if you set some objects in pre-request scripts, they are set up before the request is sent (https://learning.postman.com/docs/postman/scripts/pre-request-scripts/), so you can use whatever you've set up in the request and in test scripts. Otherwise, it wouldn't make sense to call it pre-request scripts.

You perhaps meant that you have additional requests in pre-request/test scripts. That's probably not the best practice, since it will make the tests hard to maintain.

On the other hand, I made a different approach which is basically using objects created in previous tests. But this makes the tests unmaintainable and very difficult to keep track of.

I'd say it depends on how many requests you have in your collection. Postman also offers to structure your collection into subfolders, which can make it a bit more organised:

enter image description here

So, I can imagine you can follow the approach of setting up necessary objects in the first request in a subfolder and cleaning up these objects in the last request in the same subfolder or any time you don't need such an object anymore. This can work well if you don't have tens or hundreds of requests in one place, then it would be unmaintainable as you mentioned and it's probably better to go with data files.

A general rule at the end. As long as you set up your (test) data in one place, you'll not have that much trouble maintaining it. On the other hand, setting up objects in various pre-request scripts and using them in various other requests is almost certainly an approach that will bring you a lot of trouble later on. However, there might be some exceptions like when you know there is a variable you need for only one request, perhaps you want to choose a random number like a birthday month:

pm.variables.set("birthday_month", _.random(1, 12));

then I don't see a reason why this can't be set just before the request that consumes such a variable. These local variables will not be preserved between sessions and/or requests, so then won't clutter anything else and you don't need to bother with cleaning up.

-1

pm.sendRequest is an asynchronous operation and you need to make the second request in the callback of the first one. This is the standard way to serially call two callback-based async operations in JavaScript.

pm.sendRequest(firstRequest, function (err, firstResponse)  {
  // add your extra logic here
  pm.sendRequest(secondRequest, function (err, secondResponse)  {
    // add your extra logic here
  });
});
2
  • I am doing that at the moment but I believe the code will get messy this way. Are you sure there are no other ways? Also what do you think about the strategy? Is it best practice for api tests to make them independent of each other?
    – user38046
    Aug 22, 2019 at 11:55
  • Sure, IMO they need to be independent of each other. Didn't find any other way yet (except maybe running them in docker locally with preconfigured database which has always the same state when starting and then it's deleted after test run). Aug 22, 2019 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.