Postman Easy Getting Started Tutorial

Tool download: https://www.postman.com/downloads/?utm_source=postman-home

> New, create a new request, collection or environment here; you can also create more advanced documents, Mock Server and Monitor, and API.

> Import, this is used to import collections or environments. There are options like import from file, folder, link or paste raw text.

> Runner, you can execute automated tests through Collection Runner.

> History - A history of all requests so it's easy to keep track of what you've done.

> Collections - Organize your test suite by creating collections. Each collection may have subfolders and multiple requests. Requests or folders can also be copied.

> Request tab - This will display the headers of the request being processed. By default, "Untitled Request" will be displayed for requests without a title.

> HTTP Request - Clicking it will display a drop-down list of different requests such as GET, POST, COPY, DELETE, etc. In testing, the most commonly used requests are GET and POST.

> Request URL - Also known as an endpoint, shows the URL of the API. .

> Save - If changes are made to the request, save must be clicked so the new changes are not lost or overwritten.

> Params - here will write please

Find the required parameters, such as Key - Value.

> Authorization - In order to access the api, proper authorization is required. It can be in the form of Username, Password, Token, etc.

> Headers - request header information

> Body - request body information, usually used in POST

> Pre-request Script - Execute scripts before requests, use pre-request scripts that set the environment to ensure that tests are run in the correct environment

> Tests - These scripts are executed during the request. It is important to have a test as it sets checkpoints to verify that the response status is ok, the data retrieved is as expected, and other tests

> Settings - The latest version has settings, which are generally not used

How to handle GET requests

Get requests are used to get information from a specified URL without making any changes to the endpoint. Here we use the following URL as a demo:

https://jsonplaceholder.typicode.com/users

In Postman's workspace:

1. Select the HTTP request method as GET

2. Enter the link in the URL field

3. Click the "Send" button

4. You will see the 200 status code returned below

5. There should be 10 user results in the body, indicating that your test has run successfully.

**NOTE:** In some cases, a Get request may fail due to an invalid URL or authentication required.

How to handle POST requests

Post requests are different from Get requests because there are data operations where the user adds data to the endpoint. Using the same data from the previous GET request, now add our own user. Step 1) Create a new request

Step 2) In a new request:

1. Select the HTTP request method as GET

2. Enter the link in the URL area: https://jsonplaceholder.typicode.com/users

3. Switch to the Body option

Step 3) Body option 1, select raw option 2, select JSON

Step 4) Copy the first section of the json content returned by the previous GET request, change the id to 11, change the name and uesrname and email

[
    {
        "id": 11,
        "name": "Krishna Rungta",
        "username": "Bret",
        "email": "[email protected]",
        "address": {
            "street": "Kulas Light",
            "suite": "Apt. 556",
            "city": "Gwenborough",
            "zipcode": "92998-3874",
            "geo": {
                "lat": "-37.3159",
                "lng": "81.1496"
            }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
            "name": "Romaguera-Crona",
            "catchPhrase": "Multi-layered client-server neural-net",
            "bs": "harness real-time e-markets"
        }
    }
]

Note: It is important to check the JSON format used in the Body to ensure the data is correct. Detection tools such as: jsonformatter.curiousconcept.com/

Step 5 )发送请求 1、完成上述的信息输入,点击Send按钮 2、Status:应该是201,显示为创建成功 3、在Body里返回数据

如何将请求参数化

数据参数化是Postman最有用的特征之一。你可以将使用到的变量进行参数化,而不是使用不同的数据创建相同的请求,这样会事半功倍,简洁明了。 这些数据可以来自数据文件环境变量。参数化有助于避免重复相同的测试,可用于自动化迭代测试。

参数通过使用双花括号创建:{ {sample}}

接下来创建一个参数化get请求:

Step 1) 创建一个参数化get请求:

1、将HTTP请求设置为GET

2、输入URL: jsonplaceholder.typicode.com/users;将链接的域…{ {url}}。请求url现在应该是{ {url}}/users。

3、点击Send按钮。 应该没有响应,因为我们没有设置参数的源,如下图:

Step 2) 使用环境设置所需的参数:

1、点击眼睛图标

2、单击Edit将该变量设置为可在所有集合中使用的全局环境。

Step 3) 变量--variable 1、将名称设置为url,该url为https://jsonplaceholder.typicode.com 2、点击保存按钮

Step 4 ) 回到你的Get请求页面,然后单击发送Send按钮,Get请求应该就会返回结果了,如下图:

注意:请确保所有的参数都有准确的源数据,不管是环境变量还是数据文件,以避免出错。

如何创建Postman Tests

Postman Tests在请求中添加JavaScript代码来协助验证结果,如:成功或失败状态、预期结果的比较等等。 通常从pm.test开始。它可以与断言相比较,验证其他工具中可用的命令。 接下来创建一个包含Tests的请求:

Step 1) 创建一个Get请求 :

1、切换到Tests选项,右边是代码片段选项。

2、从右边的代码片段选项里面选中 “Status code: Code is 200”

3、JS代码就自动出现在窗口中

Step 2) 点击发送请求按钮。测试结果就显示出来了,如下图:

Step 3) 回到Tests选项卡,让我们添加另一个测试。这次我们将比较预期结果和实际结果。 在右边的SNIPPETS区域选择"Response body:JSON value check"选项,我们将检查Leanne Graham是否拥有userid 1。

Step 4) 1、将代码中的“Your Test Name”替换为“Check if user with id1 is Leanne Graham”,以便测试名称确切描述我们想测试的内容。 2、使用jsonData[0].name代替jsonData.value; 获取路径,在获取结果之前检查Body。因为Leanne Graham是userid 1,所以jsonData在第一个结果中,这个结果应该从0开始。如果你想获得第二个结果,那么对后续结果使用jsonData[1] 即可。 3、在eql中,输入“Leanne Graham”

pm.test("Check if user with id1 is Leanne Graham", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].name).to.eql("Leanne Graham");
});

Step 5) 点击发送请求,可以看到你的请求之后测试结果中有两项显示测试通过。

注意: 有不同种类的测试可以在Postman中创建。尝试探索这个工具,看看哪些测试适合你实际测试。

如何创建测试集合

集合在组织测试套件中扮演着重要的角色。它可以被导入和导出,使得在团队之间共享集合变得很容易。在本教程中,我们将学习如何创建和执行集合。

Step 1) 单击页面的New按钮,选择Collection(集合).如下图:

Step 2 ) 选择Postman test collection测试集合,点击右键选择添加请求,添加后应该包含了一个请求,如下图:

Step 3) 重复上述的步骤,继续创建请求,这样,测试集合就应该有多个请求了,如下图:

如何使用Collection Runner 运行集合

有两种方式来运行一个集合,即Collection Runner和Newman。

Collection Runner:

Step 1) 单击页面右边的Runner按钮,如下图:

Step 2)Collection Runner页面应该出现如下所示。以下是对各个字段的描述

如何使用Newman运行集合

运行集合的另一种方式是通过Newman。Newman和Collection Runner之间的主要区别如下:

1、Newman是Postman的替代品,所以需要单独安装Newman;

2、Newman使用命令行,而Collection Runner使用UI界面;

3、Newman可以用于持续集成。

安装Newman并运行Collection,步骤如下:

Step 1) 下载并安装NodeJs(注意配置环境变量): https://nodejs.org/zh-cn/download/

Step 2) 打开命令行窗口并输入下面命令:

npm install -g newman

安装后如下图:

Step 3 ) Newman安装好之后,回到Postman的workspace。在Collections框中,单击三个点 ... 会出现新的选择选项,可看到Export选项,如下图:

Step 4 )

选择导出集合,默认使用推荐的集合版本,比如此处是v2.1,然后单击导出:

Step 5 ) 选择你想要保存的地址之后点击保存,这里建议专门新建一个文件夹来存放你的Postman tests。(注意:json文件名不能存在空格)

Step 6 ) 另外还需要导出我们的环境(enviroment)。最好将环境放在与Step5 导出的集合相同的文件夹中。(注意:json文件名不能存在空格)

Step 7 ) 导出Environment 到集合文件夹后,现在回到命令行,将目录更改为保存集合和环境的位置。使用下面的命令运行你的测试集合:

newman run Space_Post_Request_Collection.postman_collection.json -e My_Workspace.postman_globals.json

关于Newman的一些基础指导如下:

1、只运行集合(如果没有环境或测试数据文件依赖关系,则可以使用此选项。)

newman run <collection name> 

2、运行集合和环境(参数-e 是environment)

newman run <collection name> -e <environment name> 

3、使用所需的编号运行集合的迭代。

newman run <collection name> -n <no.of iterations>

4、运行数据文件

newman run <collection name> --data <file name>  -n <no.of iterations> -e <environment name> 

5、设置延迟时间。(这一点很重要,因为如果由于请求在后台服务器上,完成前一个请求时没有延迟时间直接启动下一个请求,测试可能会失败。)

newman run <collection name> -d <delay time>

Guess you like

Origin blog.csdn.net/sinat_33101665/article/details/128935293