zoukankan      html  css  js  c++  java
  • postman 官方 test 脚本样例

    Test examples

    样例来源:

    https://learning.getpostman.com/docs/postman/scripts/test_examples/

    Test scripts are run after a request is sent and a response has been received from the server.

    Let’s look at some examples of Postman tests. Most of these are available as snippets inside Postman. You can have as many tests as you want for a request.

    Setting an environment variable

    pm.environment.set("variable_key", "variable_value");

    Setting a nested object as an environment variable

    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));

    Getting an environment variable

    pm.environment.get("variable_key");

    Getting an environment variable (whose value is a stringified object)

    // These statements should be wrapped in a try-catch block if the data is coming from an unknown source.
    
    var array = JSON.parse(pm.environment.get("array"));
    var obj = JSON.parse(pm.environment.get("obj"));

    Clear an environment variable

    pm.environment.unset("variable_key");

    Set a global variable

    pm.globals.set("variable_key", "variable_value");

    Get a global variable

    pm.globals.get("variable_key");

    Clear a global variable

    pm.globals.unset("variable_key");

    Get a variable

    This function searches for the variable across globals and the active environment.

    pm.variables.get("variable_key");

    Check if response body contains a string

    pm.test("Body matches string", function () {
        pm.expect(pm.response.text()).to.include("string_you_want_to_search");
    });

    Check if response body is equal to a string

    pm.test("Body is correct", function () {
        pm.response.to.have.body("response_body_string");
    });

    Check for a JSON value

    pm.test("Your test name", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.value).to.eql(100);
    });

    Content-Type is present

    pm.test("Content-Type is present", function () {
        pm.response.to.have.header("Content-Type");
    });

    Response time is less than 200ms

    pm.test("Response time is less than 200ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(200);
    });

    Status code is 200

    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });

    Code name contains a string

    pm.test("Status code name has string", function () {
        pm.response.to.have.status("Created");
    });

    Successful POST request status code

    pm.test("Successful POST request", function () {
        pm.expect(pm.response.code).to.be.oneOf([201,202]);
    });

    Use TinyValidator 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;
    });

    Decode base64 encoded data

    var intermediate,
    	base64Content, // assume this has a base64 encoded value
    	rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
    
    intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
    pm.test('Contents are valid', function() {
      pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
    });

    Send an asynchronous request

    This function is available as both a pre-request and test script.

    pm.sendRequest("https://postman-echo.com/get", function (err, response) {
        console.log(response.json());
    });

    Convert XML body to a JSON object

    var jsonObject = xml2Json(responseBody);
  • 相关阅读:
    Java中编写代码出现异常,如何抛出异常,如何捕获异常
    用Java制作斗地主
    Java—Map接口中的常用方法
    Java—增强for循环与for循环的区别/泛型通配符/LinkedList集合
    Java—包装类/System类/Math类/Arrays类/大数据运算/Collection接口/Iterator迭代器
    Java—时间的原点 计算时间所使用的 Date类/DateFormat类/Calendar类
    Java—匿名对象/内部类/访问修饰符/代码块
    Java—构造方法及this/super/final/static关键字
    Java—接口
    URL Protocol打开应用程序并传递程序启动参数(Windows、Mac)
  • 原文地址:https://www.cnblogs.com/jinanxiaolaohu/p/10137025.html
Copyright © 2011-2022 走看看