我们的团队希望自动化REST API测试。现在,我们收集了一些邮递员请求,并让他们手动跳过圈。
我们可以为每个测试场景创建一个集合/文件夹,但这意味着大量的重复。我们的API仍在大力开发中,我真的不想在它改变之后在二十个地方修复同样的东西。
我想让每个端点只在集合中请求一次,并且某种独立逻辑可以以任意顺序执行它们。我知道邮差doesn't support request reuse任何干净的方式,所以我正在寻找至少一个hacky方式如何做到这一点。
创建要加载到Postman集合运行器的文件,具有以下结构:
[{
"testSequence": ["First request name", "Second request name", "..." ],
"anyOtherData": "Whatever the request needs",
"evenMoreData": "Whatever the request needs",
"...": "..."
},{
"testSequence": ["Login", "Check newsfeed", "Send a picture", "Logout" ],
"username": "Example",
"password": "correcthorsebatterystaple",
},{
"...": "keep the structure for any other test scenario or request sequence"
}]
将所有测试序列放在该文件中,然后使Postman在每个请求后检查列表并确定接下来要执行的操作。这可以做到e。 G。在整个集合的“测试块”中:
// Use the mechanism only if there is a test scenario file
// This IF prevents the block from firing when running single requests in Postman
if (pm.iterationData.get("testSequence")) {
// Is there another request in the scenario?
var sequence = pm.globals.get("testSequence");
if ((sequence instanceof Array) && (sequence.length > 0)) {
// If so, set it as the next one
var nextRequest = sequence.shift();
pm.globals.set("testSequence", sequence);
postman.setNextRequest(nextRequest);
} else {
// Otherwise, this was the last one. Finish the execution.
postman.setNextRequest(null);
}
}
如果您的请求需要在不同的运行期间使用不同的数据,您可以在输入文件中定义数据并在请求中将它们用作variables。