Introduction
Variables help keep your tests flexible and easy to update. They stop you from repeating the same fixed values everywhere.
Variables help keep your tests flexible and easy to update. They stop you from repeating the same fixed values everywhere.
pm.variables.set('variableName', 'value'); let value = pm.variables.get('variableName');
pm.variables.set('baseUrl', 'https://api.example.com'); let url = pm.variables.get('baseUrl');
pm.variables.set('userId', '12345'); let id = pm.variables.get('userId');
This test sets an API key as a variable and then checks if it was set correctly. This avoids hardcoding the key directly in the test.
pm.variables.set('apiKey', 'abc123'); // Use the variable in a test let key = pm.variables.get('apiKey'); pm.test('API key is set correctly', function () { pm.expect(key).to.eql('abc123'); });
Variables make your tests easier to maintain and update.
Changing a variable value updates all places where it is used automatically.
Hardcoding values can cause errors and extra work when things change.
Variables help avoid repeating fixed values in tests.
They make tests easier to update and reuse.
Using variables reduces mistakes and saves time.
baseUrl set to https://api.test.com, what will be the full request URL if the request uses {{baseUrl}}/items?token but the test fails because the token is not replaced. What is the most likely cause?token was not defined in the environment or collection. -> Option BPass the test and this pattern moves unknown