接口测试 Postman 接口测试

chen · 2018年08月08日 · 1618 次阅读

一:工具介绍

  • Postman 界面分为两个部分,左边是工具栏,右边是请求编辑器

  • URL

一个 URL 就是一个接口,接口大致会分为一下几个部分
请求协议:
http --- 普通的 http 请求
https --- 加密的 http 请求,传输数据更加安全
请求 IP:就是指提供接口的系统所部署的服务器地址
请求端口:默认是 80
接口路径:指系统提供的接口在什么位置
接口参数:参数在接口路径后,用 “?” 来表示路径地址完了,剩下的都是参数了,用 “&” 来区分参数个数

  • Params(json 参数)

  • Headers

HTTP Headers 是 HTTP 请求和相应的核心,它承载了关于客户端浏览器,请求页面,服务器等相关的信息。

  • 断言

下一个请求使用{{token}}使用

执行接口

运行

执行结果报告

  • 查看执行请求信息 > View-show Postman Console 开启 Console 进行监听

Console 返回请求的详细信息

函数说明

  • 1.设置变量

    postman.setEnvironmentVariable("key", "value");
    例子: postman.setEnvironmentVariable("url", "http://www.baidu.com");
    使用环境变量的格式:{{url}}

  • 1.1 清除变量
    > postman.clearEnvironmentVariable("variable_key");
    > 例子:postman.clearEnvironmentVariable("url");

  • 2.设置一个全局变量
    > postman.setGlobalVariable("key", "value");
    > 例子:postman.setGlobalVariable("username", "123@qq.com");
    > 使用全局变量格式:{{variableName}}

  • 2.1 清除一个全局变量
    > postman.clearGlobalVariable("key", "value");
    > 例子:postman.clearGlobalVariable("username", "123@qq.com");

  • 3.检查响应体包含一个字符串
    > tests["Body matches string"] = responseBody.has("string_you_want_to_search");
    > 例子:响应体包含以下字段 "path": "field is read-only",
    > tests["Body matches string"] = responseBody.has("field is read-only");
    > tests["Body matches string"] = responseBody.has("path");

  • 4.转换 XML 身体 JSON 对象
    > var jsonObject = xml2Json(responseBody);

  • 5.检查响应体等于一个字符串
    > tests["Body is correct"] = responseBody === "response_body_string";
    > 例子:响应体包含以下字段 "path": "field is read-only",
    > tests["Body is correct"] = responseBody === "response_body_string";

  • 6.检查一个 JSON 值
    > var data = JSON.parse(responseBody);
    > tests["Your test name"] = data.value === 100;

  • 7.Content-Type 的存在(不区分大小写检查)
    > tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() > > method returns the header value, if it exists.

  • 8.Content-Type 的存在(区分大小写)
    > tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

  • 9.响应时间小于 200ms 的
    > tests["Response time is less than 200ms"] = responseTime < 200;

  • 10.状态代码是 200
    > tests["Status code is 200"] = responseCode.code === 200;
    > 例子:状态码是 404
    > tests["Status code is 404"] = responseCode.code === 404;

  • 11.代号包含一个字符串
    > tests["Status code name has string"] = responseCode.name.has("Created");
    >例子:Status:201 CREATED
    > tests["Status code is 201"] = responseCode.code === 201;
    > tests["Status code name has string"] = responseCode.name.has("Created");

  • 12.成功的 POST 请求的状态代码
    > tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册