# Is there a way to define test dependency?

**URL:** <https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766>\
**Category:** Help Hub\
**Tags:** pre-request, dependency\
**Created:** [September 1, 2022, 3:49pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766 "2022-09-01T15:49:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![alekdavis](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alekdavis/32/12996_2.png) [@alekdavis](https://community.postman.com/u/alekdavis)\
**Post date:** [September 1, 2022, 3:49pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/1 "2022-09-01T15:49:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [September 1, 2022, 10:16pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/2 "2022-09-01T22:16:39Z")

</div>

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()](https://learning.postman.com/docs/running-collections/building-workflows/) may also be helpful in this case!

Hope I was able to help 🙂

---

<div class="post-metadata">

**Author:** ![alekdavis](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alekdavis/32/12996_2.png) [@alekdavis](https://community.postman.com/u/alekdavis)\
**Post date:** [September 2, 2022, 12:06am UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/3 "2022-09-02T00:06:04Z")

</div>

Thanks @kevinc-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.

---

<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:** [September 2, 2022, 4:03pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/4 "2022-09-02T16:03:38Z")

</div>

@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.

> **[Best Practices | Cypress Documentation](https://docs.cypress.io/guides/references/best-practices)**
>
> Real World Practices The Cypress team maintains the Real World App (RWA), a full stack example application that demonstrates best practices and scalable

---

<div class="post-metadata">

**Author:** ![alekdavis](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alekdavis/32/12996_2.png) [@alekdavis](https://community.postman.com/u/alekdavis)\
**Post date:** [September 2, 2022, 8:01pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/5 "2022-09-02T20:01:36Z")

</div>

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

```auto
pm.setPrerequisite("Test A");

```

---

<div class="post-metadata">

**Author:** ![e1000net](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/e1000net/32/13151_2.png) [@e1000net](https://community.postman.com/u/e1000net)\
**Post date:** [September 7, 2022, 8:15am UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/6 "2022-09-07T08:15:12Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![alekdavis](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alekdavis/32/12996_2.png) [@alekdavis](https://community.postman.com/u/alekdavis)\
**Post date:** [September 7, 2022, 11:30pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/7 "2022-09-07T23:30:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![e1000net](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/e1000net/32/13151_2.png) [@e1000net](https://community.postman.com/u/e1000net)\
**Post date:** [September 8, 2022, 1:23pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/8 "2022-09-08T13:23:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cruxto](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@cruxto](https://community.postman.com/u/cruxto)\
**Post date:** [September 11, 2022, 8:47am UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/9 "2022-09-11T08:47:15Z")

</div>

Hi @alekdavis,

I completed a [postman course](https://testautomationu.applitools.com/exploring-service-apis-through-test-automation/resources.html) 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](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts) 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](https://testautomationu.applitools.com/exploring-service-apis-through-test-automation/chapter4.3.html) so could be more difficult if you are new to Postman.

---

<div class="post-metadata">

**Author:** ![alekdavis](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alekdavis/32/12996_2.png) [@alekdavis](https://community.postman.com/u/alekdavis)\
**Post date:** [September 11, 2022, 9:21pm UTC](https://community.postman.com/t/is-there-a-way-to-define-test-dependency/38766/10 "2022-09-11T21:21:01Z")

</div>

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.
