Postman接口测试_基本功能

1、   安装与更新

一、安装的方式

方式1:chrome插件版本:chrome--->设置--->扩展程序;java

方式2:native版本(具备更好的扩展性,推荐使用):https://www.getpostman.com/node

二、Chrome app和native app的区别

一、Cookies(登陆):native版本能够直接操做cookie,而chrome版本须要安装扩展;python

二、Built-in proxy(代理):native版本自带proxy,能够用来抓包;chrome

三、Menu bar(菜单栏):chrome没有带菜单栏,而native自带菜单栏;npm

四、Restricted headers(受限headers):有一些headers在chrome app上是受限的,好比Origin and User-Agent;json

五、Don‘t follow redirects option(不去跟随重定向):native版本才有这个选项;api

六、Postman console:native版本自带;浏览器

三、native app更新

步骤:File--->Settings--->Update--->Minor fixes服务器

2、   发送一个api请求

文档:https://www.v2ex.com/p/7v9TEc53cookie

api地址:https://www.v2ex.com/api/topics/hot.json

3、   模拟HTTP Requests

一、请求Request

URL

Method:根据方法的不一样,body编辑器会变化;

Headers

body:form-data:①、网页表单用来传输数据的默认格式,能够模拟填写表单,而且提交表单;②、能够上传一个文件做为key的value提交(如上传文件),但该文件不会做为历史保存,只能在每次须要发送请求的时候,从新添加;

     x-www-urlencodedl:①、urlencode中的key-value会写入URL,from-data模式的key-value不明显写入URL,而是直接提交;②、这个编码格式不能上传文件;

     raw:能够包含任何东西,全部填写的text都会随着请求发送json/字符串;

     binary:也不能保存历史,每次选择文件,提交;

选择参数方式后,postman自动的帮咱们设置Content-Type;

Pre-request Script:请求发送前,可进行一些脚本设置,如,设置或清除参数、变量;

Tests:定义发送Request以后,须要用脚本检测的内容,也就是Test case的内容;

二、响应Response

Body:Pretty:①、格式化了JSON和XML,方便查看,点击里面的URL,postman会建立一个request;②、json、HTML、XML、Text等;

Raw:text;

preview:格式化了JSON和XML,方便查看;

 4、   权限Authorization

Basic Auth:填写用户名和密码,点击Update Requests;

Digest Auth:要比Basic Auth复杂的多,使用当前填写的值生成authorization header,因此在生成header以前要确保设置的正确性,若是当前的header已经存在,postman会移除以前的header;

OAuth 1.0a:让你签署支持OAuth1.0基于身份验证的请求,OAuth不用获取access token,你须要去API提供者获取的,OAuth1.0能够在header或者查询参数中设置value;

OAuth2.0:支持得到OAuth2.0 token并添加到request中;

5、   断言Writing Test

设置全局变量;

设置环境变量;

拿到并处理请求的响应;

定义测试检查点和断言;

一、Clear a global variable:

①、清除一个全局变量;

②、postman.clearGlobalVariable(“variable.key”);

二、Clear an environment variable:

①、清除一个环境变量;

②、postman.clearGlobalEnvironmentVariable(“variable.key”);

三、Response body:Contains string:

①、response包含内容;

②、tests[“Body matches string”]=responseBody.has(“string_you_want_to_search”)

四、Response body:Convert XML body to a JSON Object:

①、将xml格式的response转换成json格式;

②、var jsonObject = xml2Json(responseBody);

五、Response body:Is equal to a string:

①、response等于预期内容;

②、tests[*Body is corret*] = resposeBody === *response_body_string*;

六、Response body:JSON value check:

①、json解析key的值进行校验;

②、tests[*Args key contains argument passed as url parament*] = ‘test’ in responseJSON.args;

七、Response headers:Content-Type header check:

①、检查response的header信息是否有被测字段;

②、tests[*Content-Type is present*] = postman.getResponseHeader(“Content-Type”);

八、Response time is less than 200ms:

①、响应时间判断;

②、tests[*Response time is less than 200ms*] = responseTime < 200;

九、Set an global variable:

①、设置全局变量;

②、postman.setGlobalVariable(“variable_key”,”variable_value”);

十、Set an environment variable:

①、设置环境变量;

②、postman.setEnvironmentVariable(“variable_key”,”variable_value”);

十一、Status code:Code is 200:

①、判断状态码;

②、tests[“Status code is 200”] = responseCode.code !=400;

十二、Status code:Code name has string:

①、检查code name是否包含内容;

②、tests[“Status code name has string”] = responseCode.name.has(“Created”);

1三、Status code:Successful POST request:

①、成功的post请求;

②、tests[“Successful POST request”] = 201 ||responseCode.code ===202;

1四、Use Tiny Validator for JSON data:

var schema = {

  "items": {

    "type": "boolean"

  }

};

var data1 = [true, false];

var data2 = [true, 123];

pm.test('Schema is valid', function() {

  pm.expect(tv4.validate(data1, schema)).to.be.true;

  pm.expect(tv4.validate(data2, schema)).to.be.true;

});

6、   测试集合Collections

1、数据驱动的接口自动化测试

2、命令行中运行

一、命令行中运行:能够在无UI界面的服务器上运行;能够在ci持续集成系统上运行;

二、运行准备:导出collection;安装nodejs和npm(国内cnpm);安装newman;

三、生成测试报告:CLI reporter;JSON reporter;HTML reporter;JUnit reporter;

 

7、   变量

 一、环境变量environment:好比能够将测试domain设置成环境变量;

二、全局变量Global variable:实现接口请求的参数依赖于其余接口的返回,好比能够将错误信息设置成全局变量;

三、Local本地变量:通常能够在sandbox中定义;

四、Data:测试数据中导入的变量,也就是所谓的参数化;

注意:当全局变量和环境变量冲突时,环境变量覆盖全局变量;

8、   在test suite中运行test case

一、Environment :环境变量;

二、Iterations:重复运行次数;

三、Delay:间隔时间,用例与用例间的间隔时间;

四、Data:外部数据加载,即用例的参数化,能够与Iterations结合起来用,实现参数化,也就是数据驱动;

9、   生成Request代码

1、导出python脚本进行数据驱动的接口测试

一、导出python的requests脚本

二、使用nuittest进行接口自动化测试

2、导出java代码进行数据驱动的接口测试

一、导出成java的OkHttp代码;

二、使用Junit进行接口自动化测试;

三、使用fastJSON解析json字符串;

10、   Jekins+postman+newnam

一、Postman导出用例集合和导出环境变量;

二、newman运行:①、无环境变量:newman run D:\API.postman_collection.json;

             ②、有环境变量:newman run D:\API.postman_collection.json –environment D:\postnam_environment.json;

三、执行服务器上的Collection:newman –u https://www.getpostman.com/collections/cb208e7e64056f5294e5;

四、jenkins:①、构建;②、构建后发邮件;

11、   Interceptor录制

一、先在浏览器安装Interceptor,并打开;

二、postman同时打开Interceptor;

三、刷新网页,进行录制请求;

相关文章
相关标签/搜索