postman下载:官方下载地址

接口用例:用例编号、模块、测试标题、优先级、前置条件、URL、请求方法、请求参数、预期结果

宠物商店:实践接口平台

一、创建测试集

  宠物商店----宠物

二、创建请求

1、查询宠物

  GET请求,https://petstore.swagger.io/v2/pet/findByStatus

  填入必填项params,status:available,下面我全局变量改成乐sold

  增加断言

 1 // 断言状态
 2 pm.test("响应状态码为 200", function () {
 3     pm.response.to.have.status(200);
 4 });
 5 
 6 // 断言业务
 7 pm.test("响应体中包含宠物的 id 信息", function () {
 8     pm.expect(pm.response.text()).to.include("id");
 9 });
10 pm.test("响应体中包含宠物的 状态", function () {
11     pm.expect(pm.response.text()).to.include("sold");
12 });
Tests

2、新增宠物

  POST请求,https://petstore.swagger.io/v2/pet/

  增加Body---raw---json

 1 {
 2     "id": 9223372016900012345,
 3     "category": {
 4         "id": 0,
 5         "name": "cat"
 6     },
 7     "name": "miao",
 8     "photoUrls": [
 9         "string"
10     ],
11     "tags": [
12         {
13             "id": 5,
14             "name": "cute"
15         }
16     ],
17     "status": "available"
18 }
json

  增加断言

 1 pm.test("响应状态码 200", function () {
 2     pm.response.to.have.status(200);
 3 });
 4 
 5 // 全量字符匹配
 6 // https://www.sojson.com/yasuo.html
 7 // 压缩并转移义
 8 pm.test("响应体与请求参数完全一致", function () {
 9     pm.response.to.have.body("{\"id\":9223372016900012345,\"category\":{\"id\":0,\"name\":\"cat\"},\"name\":\"miao\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":5,\"name\":\"cute\"}],\"status\":\"available\"}");
10 });
Tests

3、更新宠物信息

  PUT请求,https://petstore.swagger.io/v2/pet/

  增加Body---raw---json

 1 {
 2     "id": 9223372016900012345,
 3     "category": {
 4         "id": 0,
 5         "name": "cat"
 6     },
 7     "name": "喵喵",
 8     "photoUrls": [
 9         "string"
10     ],
11     "tags": [
12         {
13             "id": 5,
14             "name": "cute"
15         }
16     ],
17     "status": "available"
18 }
json

  增加断言

 1 {
 2     "id": 9223372016900012345,
 3     "category": {
 4         "id": 0,
 5         "name": "cat"
 6     },
 7     "name": "喵喵",
 8     "photoUrls": [
 9         "string"
10     ],
11     "tags": [
12         {
13             "id": 5,
14             "name": "cute"
15         }
16     ],
17     "status": "available"
18 }
Tests

4、删除宠物

  DELETE请求,https://petstore.swagger.io/v2/pet/自己新增的宠物id

  增加断言

 1 // 需要已经有这个宠物id,才能删除
 2 
 3 pm.test("响应状态码 200", function () {
 4     pm.response.to.have.status(200);
 5 });
 6 
 7 pm.test("业务错误码为 200", function () {
 8     var jsonData = pm.response.json();
 9     pm.expect(jsonData.code).to.eql(200);
10 });
11 
12 pm.test("message 为删除宠物的id信息", function () {
13     var jsonData = pm.response.json();
14     pm.expect(jsonData.message).to.eql("9223372016900012345");
15 });
Tests

三、运行测试集

查看结果,点击进入后可以查看每个的请求结果

附:常用的断言

  • 验证响应状态码
  • 验证响应体中是否包含某个字符串
  • 验证 JSON 中的某个值是否等于预期的值
  • 验证响应体是否与某个字符串完全相同
  • 验证响应头信息中的 Content-Type 是否存在
  • 验证响应时间是否小于某个值
 1 // Status Code:Code is 200
 2 // 验证响应状态码
 3 pm.test("响应状态码为 200", function () {
 4     pm.response.to.have.status(200);
 5 });
 6 
 7 // Response Body:contains string 
 8 // 验证响应体中是否包含某个字符串
 9 pm.test("响应体中包含预期的字符串", function () {
10     pm.expect(pm.response.text()).to.include("doggie");
11 });
12 
13 // Response Body:JSON value check
14 // 验证 JSON 中的某个值是否等于预期的值
15 pm.test("宠物名称为 doggie", function () {
16     var jsonData = pm.response.json();
17     pm.expect(jsonData[0].name).to.eql("doggie");
18 });
19 
20 // Response Body:Is equal to a string
21 // 验证响应体是否与某个字符串完全相同
22 pm.test("响应体正确", function () {
23     pm.response.to.have.body("response_body_string");
24 });
25 
26 // Response Body:Content-Type header check
27 // 验证响应头信息中的 Content-Type 是否存在
28 pm.test("Content-Type is present", function () {
29     pm.response.to.have.header("Content-Type");
30 });
31 
32 // Response time is less than 200ms
33 // 验证响应时间是否小于某个值
34 pm.test("Response time is less than 200ms", function () {
35     pm.expect(pm.response.responseTime).to.be.below(200);
36 });
常用的断言

四、变量

Postman 中变量的种类与作用域

  • Data:在测试集中上传的数据
  • Environment:环境范围
  • Collection:集合范围
  • Global:全局范围
  • Local:在脚本中设置的变量

定义变量

  • 全局变量:Environments -> Globals
  • 测试集变量:测试集页面 -> Variables
  • 环境变量:Environments -> +

在 Pre-request Script 和 Tests 脚本中使用封装好的语句获取或者设置对应变量

 1 // 获取全局变量
 2 var status = pm.globals.get("status");
 3 // 输入到控制台
 4 console.log(status)
 5 
 6 // 获取测试集变量
 7 var petId = pm.collectionVariables.get("petId");
 8 // 获取环境变量
 9 var url = pm.environment.get("baseURL");
10 
11 // 设置全局变量
12 pm.globals.set("status", "sold");
13 // 设置测试集变量
14 pm.collectionVariables.set("petId", 0);
15 // 设置环境变量
16 pm.environment.set("baseURL", "");
变量的使用

 1)全局变量,Environments -> Globals

  • 请求 URL, Params 参数或 Body 表格或JSON/XML 文本中通过 {{变量名}} 使用

附:可使用 console 调试脚本

重置全局变量:pm.globals.set("status", "sold");

2)测试集变量,测试集页面 -> Variables

  

  修改 新增宠物 的 json 和 Tests,运行成功

 1 {
 2     "id": {{petId}},
 3     "category": {
 4         "id": 0,
 5         "name": "cat"
 6     },
 7     "name": "miao",
 8     "photoUrls": [
 9         "string"
10     ],
11     "tags": [
12         {
13             "id": 5,
14             "name": "cute"
15         }
16     ],
17     "status": "available"
18 }
修改后的json
 1 pm.test("响应状态码 200", function () {
 2     pm.response.to.have.status(200);
 3 });
 4 
 5 var petId = pm.collectionVariables.get("petId");
 6 
 7 // 全量字符匹配
 8 // https://www.sojson.com/yasuo.html
 9 // 压缩并转移义
10 pm.test("响应体与请求参数完全一致", function () {
11     pm.response.to.have.body("{\"id\":"+ petId + ",\"category\":{\"id\":0,\"name\":\"cat\"},\"name\":\"miao\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":5,\"name\":\"cute\"}],\"status\":\"available\"}");
12 });
修改后的Tests

  更新宠物信息、删除宠物,这两个接口的 json 和 Tests,也是一样的

3)环境变量,Environments -> +

  所有请求地址相应改一下{{baseURL}},如请求地址拼接:{{baseURL}}/pet/findByStatus?status={{status}}

变量优先级:尽量不使用重名变量

  优先级从高至低为:Data -> Enviroment -> Collection -> Global -> Local

 

笔记2022-12-10

posted on 2022-12-10 15:15  三天乐趣  阅读(104)  评论(0编辑  收藏  举报