【问题标题】:Generic function to test response schema in Postman在 Postman 中测试响应模式的通用函数
【发布时间】:2019-01-25 02:47:06
【问题描述】:

最近几天,创建测试脚本来验证我们正在开发的应用程序的 300 多个端点,我头疼不已。最后我想出了一个非常实用的解决方案,归结为:一个通用的 JSON 验证函数,并将预期的结果复制并粘贴到一个对象中进行测试。此脚本在 JSON 内部最多执行 3 个级别的每个字段验证。

pm.globals.set("validationHelper", function validationHelper(example) {
    for (var field in example) {

        if (typeof example[field] === "object") {

            pm.test(`Field '${field}' is part of the response`, function () { 
                pm.expect(pm.response.text()).to.include(field);
            });

            for (var nested in example[field]) {

                if (!Array.isArray(example[field][nested])) {

                    pm.test(`Nested field '${nested}' is part of the response`, function () {
                        pm.expect(pm.response.text()).to.include(nested);
                    });    

                } else {

                    pm.test(`Nested field '${nested}' is part of the response`, function () {
                        pm.expect(pm.response.text()).to.include(nested);
                    });

                    for (var index in example[field][nested]) {

                        if (typeof example[field][nested][index] === "object") {

                            if (!Array.isArray(example[field][nested][index])) {

                                for (var child in example[field][nested][index]) {

                                    pm.test(`Child field '${child}' is part of the response`, function () {
                                        pm.expect(pm.response.text()).to.include(child);
                                    });
                                }
                            }
                        }
                    }
                }
            }
        } else {

            pm.test(`Field '${field}' is part of the response`, function () { 
                pm.expect(pm.response.text()).to.include(field);
            });

        }
    }
    return true
} + ';');
  1. 使用 Postman,在集合中创建一个用于运行测试的预请求脚本
  2. 在您要测试的请求中,粘贴以下代码:

// Save the example response used to validate // the body using a validation function example = { "detail": "Successfully logged out." } // This function loads the global helper function // and starts using the example schema eval(pm.globals.get("validationHelper")); validationHelper(example);

  1. 您保存的示例对象以及您期望的响应。
  2. 尝试发送请求并获得全部绿色

由于性能原因,Postman 文档不建议循环测试,但取决于您想节省多少时间,这可能是一个很好的解决方案。 :-)

【问题讨论】:

  • 这很好,感谢分享,但感觉更像是一篇简短的博客文章,而不是关于您需要帮助的问题,说实话。
  • 对不起,我应该标记为知识共享而不是问题。我的错!

标签: javascript automated-tests postman


【解决方案1】:

经过一些研究,我找到了针对同样问题的更优雅的解决方案。 ;-)

pm.globals.set("validationHelper", function validationHelper(example, keys = {}) {
  for (var k in example) {

    if (typeof example[k] == "object" && example[k] !== null) {
      if (k.constructor === String) {
        if (!k.match(/^-{0,1}\d+$/)) {
          existProperty = false
          for (var key in keys) {
            (key === k) && (existProperty = true)
          }
          if (!existProperty) {
            keys[k] = true
            pm.test(`Child field '${k}' is part of the response`, function () {
                pm.expect(pm.response.text()).to.include(k);
            });
          }
        }
      }
      validationHelper(example[k], keys);

    } else {
      if (k.constructor === String) {
        if (!k.match(/^-{0,1}\d+$/)) {
          existProperty = false
          for (var key in keys) {
            (key === k) && (existProperty = true)
          }
          if (!existProperty) {
            keys[k] = true
            pm.test(`Child field '${k}' is part of the response`, function () {
                pm.expect(pm.response.text()).to.include(k);
            });
          }
        }
      }
    }
  }
  return true
} + ';');

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2021-02-28
    • 2017-06-05
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多