Verify API access with Bearer token authentication
Preconditions (2)
✅ Expected Result: The API responds with status 200 OK and returns the expected data in JSON format
Jump into concepts and practice - no test required
pm.test('Status code is 200', function () { pm.response.to.have.status(200); }); pm.test('Response has expected data field', function () { const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('data'); });
The first test checks that the response status code is 200, confirming successful authentication and data retrieval.
The second test parses the JSON response and asserts that it contains a property named 'data', which is expected from the API.
Using pm.test and pm.expect ensures clear, readable assertions in Postman scripts.
Now add data-driven testing with 3 different Bearer tokens: one valid, one expired, and one invalid
pm.request.headers.add({key: 'Authorization', value: `Bearer ${pm.environment.get('token')}`});