[英]How to reuse postmant tests and script?
Currently, I have code saved in two requests in order to hydrate my environment vars with tests and scripts.目前,我将代码保存在两个请求中,以便用测试和脚本来补充我的环境变量。
functions to reuse tests to resuse重用测试的功能以重用
In the resquest called: 'Scripts to pre requests' —> I have added functions to reuse in other pre requests scripts.在名为:“预请求的脚本”的请求中 -> 我添加了可在其他预请求脚本中重用的函数。
In the request called:'Load common test' —> I have added tests to reuse in other requests tests.在名为:'Load common test' 的请求中 -> 我添加了测试以在其他请求测试中重用。
What I have not been able to solve is to hydrate my environment vars without making the POST request that I am sending (totally unnecessary request to oauth)我无法解决的是在不发出我发送的 POST 请求的情况下对我的环境变量进行水合(对 oauth 完全不必要的请求)
Another solution would be to directly hydrate the environment var from the console:另一种解决方案是从控制台直接对环境变量进行水化:
But it is not comfortable to write the code there and I have not found another way to load code to reuse it.但是在那里编写代码并不舒服,而且我还没有找到另一种加载代码以重用它的方法。
What is the right way to solve it?解决问题的正确方法是什么?
EDIT: add snippets of the code in plain text:编辑:以纯文本形式添加代码片段:
// functions no reuse
var generate_phone = () => {
return '0' + _.random(91000000, 99999999);
}
pm.environment.set("generate_phone", generate_phone.toString());
var generate_document = () => {
return _.random(1000000, 10000000);
}
pm.environment.set("generate_document", generate_document.toString());
// test to reuse
var admin_profile_test = () => {
pm.test("check admin id", function(){
pm.expect(data.id).to.equal(parseInt(pm.environment.get("admin_id")));
})
pm.test("check admin name", function(){
pm.expect(data._embedded.profile.name).to.equal(pm.environment.get("admin_name"));
})
pm.test("check admin lastName", function(){
pm.expect(data._embedded.profile.lastName).to.equal(pm.environment.get("admin_last_name"));
})
pm.test("check admin document", function(){
pm.expect(data._embedded.profile.document).to.equal(pm.environment.get("admin_document"));
})
pm.test("check admin email", function(){
pm.expect(data._embedded.profile.email).to.equal(pm.environment.get("admin_email"));
})
pm.test("check admin phone", function(){
pm.expect(data._embedded.profile.phone).to.equal(pm.environment.get("admin_phone"));
})
}
pm.environment.set("admin_profile_test", admin_profile_test.toString());
I keep the images to show where I currently locate this code.我保留图像以显示我当前找到此代码的位置。
Postman scripts, variables etc can be defined in collection level also: Postman 脚本、变量等也可以在集合级别定义:
click the inverted hamburger after clicking the collection folder and then select edit collection :单击收藏文件夹后单击倒置的汉堡包,然后选择编辑收藏:
Here you can define your tests and pre-requisite scripts :您可以在这里定义您的测试和先决条件脚本:
This will run before each request and test这将在每个请求和测试之前运行
so you can right any logic to skip the pre-requisite when you don't want example pm.request.name != "somethem" { then }因此,当您不想要示例 pm.request.name != "somethem" { then } 时,您可以纠正任何逻辑以跳过先决条件
Also you can modify everything in prerequsite script so if you don't want to do post you can change the method from prerequsite as您也可以修改 prerequsite 脚本中的所有内容,因此如果您不想发布,可以将 prerequsite 中的方法更改为
pm.reqeust.method="GET"
This will change method to get before sending the request这将更改在发送请求之前获取的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.