chaijs接入
1 2 3 4
| //在线接入 import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; //离线 直接整个文件复制保存下来即可
|
语法
json类
1 2 3 4 5 6 7 8 9 10 11 12 13
| expect(res.json()).to.have.property('key1', 'key2')
expect(myObject).to.have.property('key1', 'value1'); expect(myObject).to.have.property('key2').that.equals('value2'); expect(myObject).to.deep.include();
expect().to.have.all.keys('a', 'b');
expect().to.be.an('object').that.has.all.keys('a', 'b');
|
我怎么接入匹配
我的目标是,除了检查成功失败,还要能够在报告上体现。报告的status
字段可选值passed、broken
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| export function checkExpectation(expectationFunc) { try { expectationFunc(); return {'pass': 'passed'}; } catch (error) { return {'fail': error.message}; } } //然后报告中设置报告结构 res.report.steps = [ {"name": "返回状态200或者201", "status": checkExpectation(() => expect([200,201]).to.be.an("array").that.includes(res.res.status)).pass || 'broken', "parameters": [{'name': '实际结果', 'value': checkExpectation(() => expect([200,201]).to.be.an("array").that.includes(res.res.status)).fail || res.res.status}] }, { "name": "返回包含objectId", "status": checkExpectation(() => expect(res.res.json()).to.have.property("objectId")).pass || 'broken', "parameters": [{'name': '实际结果', 'value': checkExpectation(() => expect(res.res.json()).to.have.property("objectId")).fail || res.res.json().objectId}] } ] //效果图忘记了,后面再补充
|