0
0
Postmantesting~20 mins

Local variables in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Local Variables Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Postman test script?
Consider the following Postman test script that uses local variables. What will be the value of the variable result after execution?
Postman
pm.test("Check local variable scope", function () {
    let result = 0;
    if (true) {
        let result = 5;
    }
    pm.expect(result).to.eql(0);
});
ATest passes because <code>result</code> is 5
BTest fails because <code>result</code> is 5
CTest fails with ReferenceError because <code>result</code> is undefined
DTest passes because <code>result</code> is 0
Attempts:
2 left
💡 Hint
Remember that let declares block-scoped variables.
assertion
intermediate
1:30remaining
Which assertion correctly tests a local variable in Postman?
You have a local variable statusCode set to pm.response.code. Which assertion correctly verifies it equals 200?
Apm.expect(statusCode).to.equal(200);
Bpm.expect(statusCode).to.be(200);
Cpm.expect(statusCode == 200).to.be.true;
Dpm.expect(pm.response.code).to.eql('200');
Attempts:
2 left
💡 Hint
Use the correct Chai assertion syntax for equality.
🔧 Debug
advanced
2:30remaining
Why does this Postman script fail to update the local variable?
Given this script, why does the count variable not increment as expected?
let count = 0;
pm.test('Increment count', () => {
  let count = count + 1;
  pm.expect(count).to.eql(1);
});
AThe assertion is incorrect; it should check for 0 instead of 1
BThe variable <code>count</code> is immutable and cannot be incremented
CThe inner <code>let count</code> shadows the outer variable, causing a ReferenceError on right side
DThe test passes because <code>count</code> is incremented correctly
Attempts:
2 left
💡 Hint
Consider variable shadowing and initialization order.
🧠 Conceptual
advanced
1:30remaining
What is the scope of local variables declared with let inside Postman test scripts?
Choose the best description of the scope of a variable declared with let inside a Postman test script function.
AThe variable is scoped only within the block or function where it is declared
BThe variable is globally accessible throughout all requests and tests
CThe variable is accessible only within the Postman environment variables
DThe variable is accessible across all collections and environments automatically
Attempts:
2 left
💡 Hint
Think about JavaScript block scoping rules.
framework
expert
3:00remaining
How to correctly use local variables to share data between multiple Postman tests in the same request?
You want to share a local variable token between multiple pm.test blocks in the same Postman request script. Which approach works best?
ADeclare <code>let token</code> inside each <code>pm.test</code> block separately
BDeclare <code>let token = pm.response.json().token;</code> once outside all <code>pm.test</code> blocks, then use <code>token</code> inside each test
CUse <code>pm.variables.set('token', ...)</code> to set a global variable accessible in all tests
DDeclare <code>var token</code> inside the first <code>pm.test</code> block and access it in others
Attempts:
2 left
💡 Hint
Local variables declared outside test blocks can be shared inside them.