zoukankan      html  css  js  c++  java
  • postman使用--断言

    前戏

    在我们测接口的时候,没有断言的接口都是耍流氓,因为做自动化的时候,不加断言我们不知道这个接口是执行成功了还是执行失败了,所以断言是做接口自动化必须的

    断言

    断言就是我们预期值和接口返回值是否一样,就和我们写功能测试用例时的预期结果一个意思

    pre-request Script(预置脚本)可以用来修改一些默认参数,在请求发送之前执行
    test Scripr(测试脚本):当接受到响应之后,在执行脚本

    还是我们上节讲的A接口,我们来断言返回值中是不是有zouzou666,如果有,则认为接口执行成功,没有则失败

     在test里设置断言

    pm.test("包含字符串",function(){
    pm.expect(pm.response.text()).to.include("zouzou666");
    });

    如果执行成功则在Test Results里显示PASS,那我们把断言改为失败试试

    我们发现,断言失败会显示成FALL的,当然我们可以在Test下面添加多个断言

    postman脚本执行顺序

    也就是先执行最上层的文件夹里的pre-request Script,在执行它下面文件夹里的pre-request Script,在执行请求里的pre-request Script。在发送请求,在执行最上层的文件夹里的Tests,在执行它下面文件夹里的Tests,最后执行请求里的Tests

    常用的断言

    判断返回结果是不是在1s之内
    pm.test("返回结果在1s之内",function(){
    pm.expect(pm.response.responseTime).to.be.below(1000);
    });


    判断返回结果是不是在300ms左右
    pm.test("判断返回结果在300ms左右",function(){
    pm.expect(pm.response.responseTime).to.be.closeTo(300,200);
    });在300ms的上下200ms,也就是100--500ms之间

    断言返回的状态码是不是200
    tests["返回状态码正确"]=responseCode.code===200;
    
    pm.test("返回正确",function(){
        // pm.response.to.have.status(200);    
        pm.response.to.be.ok;
    })
    pm.test("包含字符串",function(){
        pm.expect(pm.response.text()).to.include("@#sd1135");
    });
    
    pm.test("包含字符串",function(){
        pm.expect(pm.response.text()).to.include("38dd572dd9c14c73b7637893c0592aa7");
    });
    
    pm.test("包含字符串",function(){
        pm.expect(pm.response.text()).to.include("10");
    });
    设置环境变量
    pm.environment.set("variable_key", "variable_value");
    将嵌套对象设置为环境变量
    var array = [1, 2, 3, 4];
    pm.environment.set("array", JSON.stringify(array, null, 2));
    
    var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
    pm.environment.set("obj", JSON.stringify(obj));
    获取环境变量
    pm.environment.get("variable_key");
    获取环境变量(其值是字符串化对象)
    var array = JSON.parse(pm.environment.get("array"));
    var obj = JSON.parse(pm.environment.get("obj"));
    清除环境变量
    pm.environment.unset("variable_key");
    设置全局变量
    pm.globals.get("variable_key");
    清除全局变量
    pm.globals.unset("variable_key");
    检查响应主体是否包含字符串
    pm.test("Body matches string", function () {
        pm.expect(pm.response.text()).to.include("string_you_want_to_search");
    });
    检查响应主体是否等于字符串
    pm.test("Body is correct", function () {
        pm.response.to.have.body("response_body_string");
    });
    检查JSON值
    pm.test("Your test name", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.value).to.eql(100);
    });
    内容类型存在
    pm.test("Content-Type is present", function () {
        pm.response.to.have.header("Content-Type");
    });
    响应时间小于200毫秒
    pm.test("Response time is less than 200ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(200);
    });
    状态代码是200
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });
    代码名称包含一个字符串
    pm.test("Status code name has string", function () {
        pm.response.to.have.status("Created");
    });
    成功的POST请求状态代码
    pm.test("Successful POST request", function () {
        pm.expect(pm.response.code).to.be.oneOf([201,202]);
    });
  • 相关阅读:
    关于基于.net的WEB程序开发所需要的一些技术归纳
    技术的学习方法探索之一
    生活,就是让程序为人们服务!
    js滑动提示效果
    radio判断是否为空
    JS清除网页历史记录,屏蔽后退按钮
    多表查询存储过程
    IP地址转化为数字,charindex ,SUBSTRING
    c# byte转化为string
    获得IP地址中文
  • 原文地址:https://www.cnblogs.com/zouzou-busy/p/11031118.html
Copyright © 2011-2022 走看看