Postman中无法使用变量实现测试分支的问题排查
It sounds like you're running into a common issue with Postman's Collection Runner where the execution flow isn't matching your intended sequence. Let's break down the likely causes and walk through fixes to get your workflow running as expected.
First, Check Collection Runner's Execution Order Setting
The most straightforward culprit here is how Postman is configured to sort requests during runs:
- Open the Collection Runner and select your target collection.
- Look at the top of the runner interface for the Order dropdown. If it's set to
Folder, Postman will execute all requests in one folder first (e.g., all POST requests) before moving to the next folder (all GET requests)—which explains why you're seeing all POSTs run consecutively instead of interleaved withresultsPositive. - Switch the order to
Manual, then drag and drop your requests into the exact sequence you want:postRich→resultsPositive→postAllProperties→resultsPositive→postMinimum→resultsPositive
- Click Save Order if you want this sequence to persist for future runs.
Fix setNextRequest Logic Conflicts
You mentioned having a collection-level post-request script to track the last run request—this might be overriding the sequential flow you want. Let's refine the script logic to explicitly control the execution chain:
- Update each POST request's post-script:
ForpostRich,postAllProperties, andpostMinimum, add this code to their post-request scripts:// Save the current request name to track the sequence pm.environment.set("lastRequest", pm.info.requestName); // Force the next request to be resultsPositive postman.setNextRequest("resultsPositive"); - Control the next step from
resultsPositive:
Add this code toresultsPositive's post-request script to determine what runs next based on the last POST request:const lastRanRequest = pm.environment.get("lastRequest"); switch(lastRanRequest) { case "postRich": postman.setNextRequest("postAllProperties"); break; case "postAllProperties": postman.setNextRequest("postMinimum"); break; case "postMinimum": postman.setNextRequest(null); // Stop execution after this run break; default: postman.setNextRequest(null); } - Clean up collection-level scripts:
Remove anypostman.setNextRequest()calls from the collection-level post-script—these can interfere with the request-specific logic we just added.
Verify Request/Folder Statuses
- Double-check that all requests in your sequence are enabled (not grayed out). Disabled requests will be skipped entirely.
- Review the test script in your
positivePostsfolder—make sure it doesn't contain anysetNextRequest()calls that might be overriding your intended flow.
Test the Sequence
Run the collection again in the Collection Runner, and keep the Postman Console open (bottom toolbar) to monitor the setNextRequest logs. This will help you spot if any script is incorrectly directing the flow.
内容的提问来源于stack exchange,提问作者Scott

