在邮递员中请求重用 [英] Request reuse in Postman
问题描述
我们的团队希望自动化REST API测试.现在,我们收集了一些邮递员的请求,并手动使它们跳了起来.
Our team wants to automate our REST API testing. Right now, we have a collection of Postman requests and make them jump through hoops manually.
我们可以为每个测试方案创建一个集合/文件夹,但这将意味着大量的重复.我们的API仍在大量开发中,我真的不想在更改后在20个地方修复相同的问题.
We could create a collection/folder for each testing scenario, but that would mean a ton of duplication. Our API is still under heavy development and I really don't want to fix the same thing at twenty places after it changes.
我希望每个端点在一个集合中只请求一次,并且希望某种独立的逻辑可以按任意顺序执行它们.我知道Postman 不以任何干净的方式支持请求重用,因此,我至少正在寻找一种方法来做到这一点.
I would like to have each endpoint request only once in a collection and some kind of independent logic that can execute them in an arbitrary order. I know Postman doesn't support request reuse in any clean way, so I am looking for at least a hacky way how to do it.
推荐答案
创建具有以下结构的文件以加载到Postman Collection Runner中:
Create a file to load into the Postman Collection Runner, with the following structure:
[{
"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在每次请求后检查列表,然后决定下一步执行什么.可以做到这一点.G.在整个集合的测试块"中:
Put all your test sequences in that file, then make Postman check the list after each request and decide what to execute next. This can be done e. g. in a "tests block" of the whole collection:
// 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);
}
}
如果您的请求需要在不同的运行期间使用不同的数据,则可以在输入文件中定义数据并将其用作
If your requests need to use different data during different runs, you can define the data in the input file and use them as variables in the request.
这篇关于在邮递员中请求重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!