# Determining if a test was run from a Collection or Send

**URL:** <https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [November 13, 2023, 3:43pm UTC](https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572 "2023-11-13T15:43:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattirwin](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@mattirwin](https://community.postman.com/u/mattirwin)\
**Post date:** [November 13, 2023, 3:43pm UTC](https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572/1 "2023-11-13T15:43:53Z")

</div>

Hello,

I have some setup javascript that I am running in each folders Pre-request script. This allows me to run a collection or just a single request and get the same output since the initialization is done every time.

However, this initialization process is not the fastest. For this reason I am looking for a way to only run the setup once when I am coming from a collection. But run it every time a single request is run manually.

I have tried to create a dummy request and set a variable. This way when the collection runs it will call this request, which sets the var ‘isCollection’, which will then stop further initializations from running in the other tests with a if statement. The problem is that the next time I run a single test the variable will still be true so the initialization wont run. I could add a request at the end that sets “isCollection” back to false but then if a user stops execution mid run of the collection then the variable wont be set back to false.

I feel like “detect if the run is coming from a folder or collection vs a single test” should be a out of the box feature but I can’t seem to find any way to accomplish this.

Thanks in advance for responses.

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [November 13, 2023, 7:19pm UTC](https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572/2 "2023-11-13T19:19:19Z")

</div>

This doesn’t address your question, but may address your use case =\> I have seen separate folders used for Setup, Tests, and Teardown. So when you run the collection, setup and teardown happen as you configured the steps. And when you run a single request, you can still manually execute set up or tests.

---

<div class="post-metadata">

**Author:** ![mattirwin](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@mattirwin](https://community.postman.com/u/mattirwin)\
**Post date:** [November 13, 2023, 8:31pm UTC](https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572/3 "2023-11-13T20:31:58Z")

</div>

@jetison we had something like this originally. I think this works well. My big reason for moving to prerequest scripts is that we didn’t like that a user had to rerun the setup folder every time they wanted to run a request. As in, I run request, get a error that it didn’t work properly, go back and run the setup folder, go back to run my test. Understand that that structure does resolve this though!

---

<div class="post-metadata">

**Author:** ![mattirwin](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@mattirwin](https://community.postman.com/u/mattirwin)\
**Post date:** [November 13, 2023, 8:50pm UTC](https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572/4 "2023-11-13T20:50:33Z")

</div>

I finally found something. I think this might be closer to what I am looking for [https://stackoverflow.com/questions/72363743/how-to-know-if-a-request-is-running-from-collection-in-test-script](https://stackoverflow.com/questions/72363743/how-to-know-if-a-request-is-running-from-collection-in-test-script). This will tell me the number of tests setup to run.

---

<div class="post-metadata">

**Author:** ![mattirwin](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@mattirwin](https://community.postman.com/u/mattirwin)\
**Post date:** [November 13, 2023, 9:06pm UTC](https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572/5 "2023-11-13T21:06:18Z")

</div>

I think this is actually what I want “postman.\_\_execution.cursor.position” if the cursor position is 0 I am on the first request the runner is executing.

---

<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:** [December 13, 2023, 9:07pm UTC](https://community.postman.com/t/determining-if-a-test-was-run-from-a-collection-or-send/54572/6 "2023-12-13T21:07:03Z")

</div>

Closed on December 13, 2023, 9:07pm UTC

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