Using variables inside Postman and Collection Runner

 

Variables are among the most powerful features in Postman. Using variables in your Postman requests, eliminates the need to duplicate requests, which can save a lot of time!

A very common scenario while testing APIs is that the API infrastructure might be present on your local machine, a staging setup, and a production setup. Using variables you can call all these APIs without having to replace values manually. You can also use variables to test for different users (for example, they might need different access tokens), different input values, and a lot more.

With Jetpacks(http://blog.getpostman.com/2014/01/16/ridiculously-easy-api-testing-introducing-jetpacks-for-postman/), you get even more power to play with variables. You can extract data from responses and set variable values automatically. You can chain requests one after the other with minimal effort. Variable values are also available inside tests to help you make them more generic.

Data variables let you test multiple instances of a request in one go. Imagine that you want to test an API which let's users post comments on a post. Using data variables you can test for hundreds of variations of a request with different IDs, tokens or content bodies.

Let's look at how you can use variables in your workflow inside Postman.

Create an environment

For this example, let's assume we want to create two environments, production and dev.

Click on "No environment" in the header.


1-dropdown
2. Select "Manage environments" and then on the "Add" button in the modal that comes up.

2-manage-environments
3. Fill in the values as shown in the screenshot below

3-add-environment

Press submit.
To create the dev environment, we can just duplicate this environment so that we don't have to type in variable keys all over again.

4-duplicated-environment

Click the duplicate environment icon, and then click on the environment name to edit. Fill in the values, as shown in the screenshot below.

4-2-duplicate-environment-values

Variables can be used almost everywhere inside Postman. They are available inside:

URL
URL params
Header values
form-data/url-encoded values
Raw body content
Helper fields
To use a variable you need to enclose the variable name with double curly braces – {{my_variable_name}}.

With our environments created, let's try out a sample request. Set the base URL field for the API to {{url}}/post.

6-url-request

Now select an environment from the environment selection dropdown.

5-dropdown-filled

Once an environment is selected, Postman will replace all instances of a variable with it's corresponding value. If no environment is selected, then Postman will try to find a matching global variable.

On hitting send here with the "Production" environment, we see that the request is made to "httpbin.org".

7-sent-request

Change the environment to "dev", and hit send. The request will be made to localhost:5000. It's as easy as that!

Now, let's modify this request by adding a POST body and setting a variable inside the body.

8-post-request

On hitting send, we see that the API received the response body with the variable replaced by it's value.

9-post-request-response

Variables with test scripts

You will need the Jetpacks upgrade for writing test scripts. If you still haven't purchased it, I strongly recommend that you do!

Inside Postman test scripts, you can set environment and global variables using the postman.setEnvironmentVariable and postman.setGlobalVariable functions. You can also access these values using the special environment and globals dictionaries.

Check out this blog entry about how you can extract values from response bodies and assign them to variables. Subsequent calls are made using these values. This is an extremely powerful feature and I am sure you will wonder how you went by without it!

Data variables

Data variables are used inside the Collection Runner. The Collection Runner can run a collection or a folder multiple times. For every iteration, it will pick up new values from a JSON or a CSV file and replace variables in the request with these values.

Let's save our request above in a collection and use data variables to change the response body.

10-data-variables-request

As shown in the screenshot above, I have added a couple of parameters to the JSON body. Values for these will be filled by Postman from a data file.

11-data-variables-test

I have also added a couple of tests which checks whether the correct value was received in the response. You can access the data values for a specific iteration using the special data variable. You can log values to the Chrome DevTools console inside test scripts. Make sure to enable Chrome DevTools first.

Postman lets you import CSV or JSON files as data sources.

The CSV file needs to have the variable names in it's first row. Subsequent rows should be separated by line breaks while the values should be separated by commas.
Download a sample CSV file(http://getpostman.com/samples/test_data_file.csv) to find out how it looks like.

profile_url, username
http://www.google.com, a85
http://www.google.com, google

 

The JSON file is an array of objects, with each object having key/value pairs. Postman supports only simple numerical and string values inside the JSON file.
Download a sample JSON file(http://getpostman.com/samples/test_data_file.json) to find out how it looks like.

[
{
"profile_url": "http://www.google.com",
"username": "a85",
"password": "blah"
},
{
"profile_url": "http://www.getpostman.com",
"username": "larry",
"password": "nocolors"
}
]

 

12-data-variables-start-test-run

Click on "Choose Files" and import the file that has the data with which the API is to be tested. For this example, we'll just run 2 iterations. I am running this example in the dev environment. Note that environment variables, global variables and data variables can be used together.

After clicking start, the requests are run one after the other. The Collection Runner window tells us that all our tests passed!

13-data-variables-test-run-results

Using variables is essential to maximising your Postman workflow. I hope this tutorial helped you gain a better understanding of this feature. If you have any questions, do let me know in the comments.

 

http://blog.getpostman.com/2014/02/20/using-variables-inside-postman-and-collection-runner/

 

时间: 2024-08-01 09:28:20

Using variables inside Postman and Collection Runner的相关文章

Enabling Chrome Developer Tools inside Postman

Chrome's Developer Tools are an indispensable part of the modern web development workflow. However, accessing them inside the Postman packaged app takes a few steps. To enable them: Type chrome://flags inside your Chrome URL windowSearch for "packed&

浅析PHP原理之变量(Variables inside PHP)_php技巧

或许你知道,或许你不知道,PHP是一个弱类型,动态的脚本语言.所谓弱类型,就是说PHP并不严格验证变量类型(严格来讲,PHP是一个中强类型语言,这部分内容会在以后的文章中叙述),在申明一个变量的时候,并不需要显示指明它保存的数据的类型: 复制代码 代码如下: <?php  $var = 1; //int  $var = "laruence"; //string  $var = 1.0002; //float  $var = array(); // array  $var = ne

asp.net key considerations(一)从前用惯了asp的朋友看看这个吧,大家常问的如Request等问题解答得很清楚

asp.net|request|解答|问题 Migrating to ASP .NET: Key Considerations Jim KieleyMicrosoft CorporationJuly 2001Summary: This article explores some basic considerations for moving existing ASP applications to the ASP .NET environment as quickly and efficient

JAVASCRIPT的一些知识点梳理

春节闲点,可以安心的梳理一下以前不是很清楚的东东.. 看的是以下几个URL: http://web.jobbole.com/82520/ http://blog.csdn.net/luoweifu/article/details/41466537 http://javascriptissexy.com/understand-javascript-closures-with-ease/ http://javascriptissexy.com/javascript-variable-scope-an

TensorFlow教程之API DOC 6.3.13. STATE OPS

本文档为TensorFlow参考文档,本转载已得到TensorFlow中文社区授权. Variables Note: Functions taking Tensor arguments can also take anything accepted by tf.convert_to_tensor. Contents Variables Variables class tf.Variable Variable helper functions tf.all_variables() tf.train

优化PHP代码的建议

1. If a method can be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化,就对它做静态声明.速率可提升至4倍. 2. echo is faster than print. echo 比 print 快. 3. Use echo's multiple parameters instead of string concatenation. 使用echo的多重参数(译注:指用逗号

PHP代码优化的集合

  1.如果一个方法能被静态,那就声明他为静态的,速度可提高1/4; 2.echo的效率高于print,因为echo没有返回值,print返回一个整型; 3.在循环之前设置循环的最大次数,而非在在循环中; 4.销毁变量去释放内存,特别是大的数组; 5.避免使用像__get, __set, __autoload等魔术方法; 6.requiere_once()比较耗资源; 7.在includes和requires中使用绝对路径 8.如果你需要得到脚本执行时的时间,$_SERVER['REQUSET_

同步与Java内存模型(一)序言

原文:http://gee.cs.oswego.edu/dl/cpj/jmm.html 作者:Doug Lea 译者:萧欢  校对:丁一,方腾飞 先来看如下这个简单的Java类,该类中并没有使用任何的同步. 查看源代码 打印帮助 01 final class SetCheck { 02 private int  a = 0; 03 private long b = 0; 04   05 void set() { 06 a =  1; 07 b = -1; 08 } 09   10 boolean

C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新)

原文:C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新) 历史版本 C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有17年的历史,语言本身具有丰富的特性,微软对其更新支持也十分支持.微软将C#提交给标准组织ECMA,C# 5.0目前是ECMA发布的最新规范,C# 6.0还是草案阶段,C# 7.1是微软当前提供的最新规范. 这里仅仅列个提纲,由于C# 5.0是具有ECMA标准规范的版本,所以选择C# 5.0作为主要版本学习,并专题学习C# 6.0,7.0版本新特性. C