Is there a way to define test dependency?

Is there a way to specify test dependency when you execute a single test? For example, say, you have tests A and B. Test A is sets up a variable that test B requires, so test B cannot be called as a stand-alone test, but would always need to be called after test A. Is there a way to set up such dependency? Like when executing test B, always run test A first?

Hi @alekdavis!

For these tests, are they individual requests or “tests” in a single request under the one request’s Tests tab?

If they are all part of a single request, you can structure your code such that Test B will only run if Test A successfully completes. This could be as simple as checking the variable that B expects from A.

For multiple requests, the same approach can be taken but you would need to structure your code a little differently and definitely rely on variables. .setNextRequest() may also be helpful in this case!

Hope I was able to help :slight_smile:

Thanks @kevin-postman. Here is my use case:

Test A creates a user account (with some dynamic properties), test B updates the user account created in A. If I just run test B, the property by which I identify user in B will not be there. But if I try to run B, and the environment detects that B is dependent on A, it first launches A and then B, then all is cool. If I have test C that deletes user created in A, then trying to run C would also execute A (but not B, since C is only dependent on A). That’s like compiling projects in Visual Studio (if project B depends on project A and you compile project B, it will first compile project A and then project B).

If I understand you correctly, I don’t think your suggestion accomplishes the same. It implies that you cannot run test B unless you run it after test A (as a group, so I need to explicitly select tests A and B and execute them as a collection).

From what I see there seems to be no way to do this. I’ll think of maybe splitting tests into folders so a group from the folder runs as a logical collection with required dependencies. It would be nice if this were available out of the box, though.

@alekdavis

You can put each end to end test in a folder and the requests in that folder should run in order.

The collection being the test suite, and the folder being the test case.

It would be nice if this could handle this concept a little better out of the box.
Test Suite → Test Case → Test Scripts

With some control over whether to sort assert or hard assert and stop running the tests in that folder without having to write code.

This is where I struggle sometimes with automation.
I try to follow the singular approach to tests, that each test should not rely on another test to run.
With the goal to be able to run those tests in parallel. But this does need a strategy on how to layer your tests, and what should be a pre (or post) test element.

Not related to Postman, but I read this the other day that seems relevant to this topic.

1 Like

I get that part, but it would be so much easier if you could do something like this:

pm.setPrerequisite("Test A");
1 Like

Would be even better (I dream of it) to have an overall script that could launch different tests and get the control back after each test and can decide (if then else) what next test to launch.
This would ease the scripting of true business scenarios. As opposed to collections and prerequest and test scripts, that are overly complex to achieve this, IMHO. Or I missed something :wink:

I’m not positive, but can’t you do this via Newman?

1 Like

Possibly, but it looks so complex with this command line stuff, as opposed to nice Postman GUI…
But I did not explore Newman I have to admit

Hi @alekdavis,

I completed a postman course recently on the automation university site.

In their collection where there was a dependency such as a booking needed to exist for the test. In the pre-request script, it made a request to create a new booking using the built-in sendRequest function.

This way the test contains its dependency which, removes the need for separate test steps in a folder. Saying that depending on how complicated your dependencies are it could get pretty complicated very quickly. This way of setting up your tests is a little more advanced so could be more difficult if you are new to Postman.

This is probably the best option now, but it would require a lot more code (since unlike another test, you would need to implement call to make REST API calls.

1 Like