0
0
Postmantesting~10 mins

Variable syntax ({{variable}}) in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies that a Postman request correctly uses the variable syntax {{variable}} to substitute environment variables in the URL and headers. It checks that the request sends with the expected values and receives a successful response.

Test Code - Postman Test Script
Postman
pm.test("Variable substitution works", function () {
    // Access environment variable
    const baseUrl = pm.environment.get("baseUrl");
    const apiKey = pm.environment.get("apiKey");

    // Construct expected URL
    const expectedUrl = `${baseUrl}/users`;

    // Check request URL uses variable substitution
    pm.expect(pm.request.url.toString()).to.eql(expectedUrl);

    // Check header uses variable substitution
    const authHeader = pm.request.headers.get("Authorization");
    pm.expect(authHeader).to.eql(`Bearer ${apiKey}`);

    // Check response status
    pm.response.to.have.status(200);
});
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsPostman environment variables 'baseUrl' and 'apiKey' are set-PASS
2Postman sends request with URL containing {{baseUrl}} and header containing {{apiKey}}Request URL is 'https://api.example.com/users', Authorization header is 'Bearer 12345abcde'-PASS
3Test script retrieves environment variables 'baseUrl' and 'apiKey'Variables retrieved: baseUrl='https://api.example.com', apiKey='12345abcde'-PASS
4Test script checks that request URL matches expected URLRequest URL is 'https://api.example.com/users', expected URL is 'https://api.example.com/users'pm.expect(pm.request.url.toString()).to.eql(expectedUrl)PASS
5Test script checks that Authorization header matches expected valueAuthorization header is 'Bearer 12345abcde', expected is 'Bearer 12345abcde'pm.expect(authHeader).to.eql(`Bearer ${apiKey}`)PASS
6Test script checks that response status is 200Response status is 200 OKpm.response.to.have.status(200)PASS
Failure Scenario
Failing Condition: Environment variable 'baseUrl' or 'apiKey' is missing or not substituted correctly in the request
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the URL in the Postman request?
AThat the {{baseUrl}} variable is correctly replaced with its value
BThat the URL contains the literal string '{{baseUrl}}'
CThat the URL is empty
DThat the URL uses query parameters
Key Result
Always verify that environment variables are correctly defined and active in Postman before running tests that depend on {{variable}} substitution.