断言响应体不为空需区分空字符串、空JSON结构及业务有效内容:用trim()排除空白,JSON.parse()验证格式,再检查对象键数或数组长度;字段判空须用可选链避免报错;响应头和Cookie需先exist再trim判空;旧tests[]写法易致脚本错误而非FAIL。

断言 response body 不为空(字符串判空)
最常见的情况是:接口返回了 200 状态码,但 responseBody 是空字符串 ""、"{}"、"[]" 或纯空白("\n\t "),这往往意味着业务逻辑没走通或数据被过滤掉了。不能只看状态码就认为成功。
直接用 pm.expect(pm.response.text()).to.not.be.empty 是错的——Chai 默认把 "{}" 和 "[]" 当作“非空”,但它其实是有效 JSON 的空结构,未必符合你的业务预期。
- 若只要求响应体不是空字符串(含空白字符):
pm.test("Response body is not empty string", () => { pm.expect(pm.response.text().trim()).to.not.equal(""); }); - 若要求响应体能被解析为 JSON 且不是空对象/空数组:
pm.test("Response body is valid non-empty JSON", () => { const body = pm.response.text().trim(); pm.expect(body).to.not.equal(""); pm.expect(() => JSON.parse(body)).to.not.throw(); const json = JSON.parse(body); pm.expect(json).to.not.be.empty; // 注意:这里 .not.be.empty 对 {} / [] 仍会失败,需进一步判断 }); - 更稳妥的写法(推荐):
pm.test("Response body has meaningful content", () => { const text = pm.response.text().trim(); pm.expect(text).to.not.equal(""); try { const json = JSON.parse(text); pm.expect(json).to.exist; // 排除 null/undefined pm.expect(Object.keys(json).length > 0 || Array.isArray(json) && json.length > 0).to.be.true; } catch (e) { // 非 JSON 响应,至少保证有可见字符 pm.expect(/[\S]/.test(text)).to.be.true; } });
断言某个 JSON 字段值不为空(非 null / undefined / "")
比如登录接口返回 {"token": "abc...", "user_id": 123},你得确保 token 字段既存在,又不是空字符串。直接 pm.expect(json.token).to.not.equal("") 会因字段不存在而报 Cannot read property 'token' of undefined 错误。
- 安全取值 + 判空(必须加
?.或 try/catch):pm.test("token exists and is non-empty", () => { const json = pm.response.json(); pm.expect(json?.token).to.exist; pm.expect(json?.token).to.be.a("string"); pm.expect(json?.token).to.not.equal(""); }); - 如果字段可能为
0、false等 falsy 值但业务上合法,就不能用.to.not.be.empty,得明确排除null和undefined:pm.expect(json?.token).to.not.be.oneOf([null, undefined]); - 环境变量或前置脚本中设置的字段,也要同样防护——
pm.environment.get("token")返回undefined时,.length会报错,务必先?.或|| ""
断言响应头或 Cookie 不为空
有些接口靠响应头(如 X-Request-ID)或 Set-Cookie(如 sessionid)传递关键信息,但容易忽略它们可能缺失或为空值。
Postman 12.27.7 Windows x64 官方安装包,适用于 API 设计、调试、测试与协作。该系列包含 Enterprise 云端性能测试静态 IP 出口能力,以及接口规范标签管理改进,适合受限网络测试与规范资源组织场景。
- 响应头判空:
pm.test("X-Request-ID header is present and non-empty", () => { const id = pm.response.headers.get("X-Request-ID"); pm.expect(id).to.exist; pm.expect(id.trim()).to.not.equal(""); }); - Cookie 判空(注意:Postman 的
pm.cookies.get()在 cookie 不存在时返回undefined,不是空字符串):pm.test("sessionid cookie is set", () => { const sid = pm.cookies.get("sessionid"); pm.expect(sid).to.exist; pm.expect(typeof sid).to.equal("string"); }); - 不要用
postman.getResponseCookie("name")(旧语法),它在新版本里已弃用,且对不存在的 cookie 返回null而非undefined,行为不一致
为什么 tests["xxx"] = xxx 写法容易漏掉空值问题
老式断言写法 tests["Check token"] = jsonData.token !== "" 看似简洁,但有硬伤:一旦 jsonData 是 undefined 或 jsonData.token 不存在,整个表达式直接抛 TypeError,Postman 不会把它记为 FAIL,而是标为 “Script error”,在 Test Results 里看不到具体哪条挂了,排查成本陡增。
新版 pm.test() + pm.expect() 的优势就在这里:所有校验都包裹在 Chai 断言链里,类型错误、属性缺失、JSON 解析失败都会转成清晰的 FAIL 条目,并附带错误栈。哪怕你写 pm.expect(jsonData.token).to.not.equal(""),它也会告诉你 “expected undefined to not equal ''”,而不是静默崩掉。
真正容易被忽略的点是:空格、换行、BOM 字符、JSON 中的 null 字段——它们都算“有内容”,但业务上往往不可接受。别依赖肉眼扫响应体,每处判空都要明确你定义的“空”到底指什么。

















