You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman中无法为第二个测试脚本单独设置变量值,求排查

问题

我在单个请求中编写了两个测试,希望第二个测试使用不同的变量值,尝试用pm.variables.set("confirm_emal", "vodani6277@ulforex.com")设置变量,但无法实现第二个脚本单独使用目标变量值。

测试代码如下:

pm.test("Confirmed email", () =>{
// 解析响应JSON并测试属性
const responseJson = pm.response.json();
pm.expect(responseJson.email).to.eql('gekisa9678@exoacre.com');
pm.expect(responseJson.auth_key).to.be.eql('557UmPdC2pGV_tA67bSjHGPbtbRz90Hk');
pm.expect(responseJson.first_name).to.eql('Vitalijj');
pm.expect(responseJson.last_name).to.eql('Agarev');
pm.expect(responseJson.birthday_date).to.eql(978307200);
pm.expect(responseJson.register_type).to.eql('email');
pm.expect(responseJson.email_confirmed).to.eql("true");
});
pm.test("unconfirmed email", () =>{
    pm.variables.set("confirm_emal", "vodani6277@ulforex.com");
  
    const responseJson = pm.response.json();
pm.expect(responseJson.email).to.eql('vodani6277@ulforex.com');
pm.expect(responseJson.register_type).to.eql('email');
pm.expect(responseJson.email_confirmed).to.eql(false);
});

运行后出现断言错误:AssertionError: expected 'vodani6277@ulforex.com' to deeply equal 'gekisa9678@exoacre.com'


问题原因与解决方案

核心错误

  1. 测试脚本执行时机错误:Postman的测试脚本是在请求收到响应后才运行的,你在第二个测试里设置变量时,请求已经发送完成,服务器返回的是第一个测试对应的响应数据,所以第二个测试的预期值和实际响应不匹配。
  2. 变量名拼写错误:你设置的变量是confirm_emal(缺少字母i,正确应为confirm_email),即使变量设置时机正确,请求也无法正确引用这个变量。

正确实现方式

要让两个测试使用不同变量值,必须在请求发送前就设置好对应变量,以下是两种可行方案:

方案1:拆分两个独立请求

直接复制原请求,分别给每个请求设置对应的confirm_email变量值,每个请求只保留对应场景的测试脚本。这种方式简单直接,适合快速测试不同场景。

方案2:用预请求脚本+集合迭代(批量测试)

如果想在单个请求中完成两个场景测试,可借助预请求脚本和集合运行器:

  1. 在Pre-request Script标签页中添加以下代码:
// 定义两个测试场景的参数
const testCases = [
    { 
        email: "gekisa9678@exoacre.com", 
        isConfirmed: "true",
        authKey: "557UmPdC2pGV_tA67bSjHGPbtbRz90Hk",
        firstName: "Vitalijj",
        lastName: "Agarev",
        birthday: 978307200
    },
    { 
        email: "vodani6277@ulforex.com", 
        isConfirmed: false
    }
];

// 获取当前迭代索引(集合运行器会自动递增),默认取第一个场景
const currentCase = testCases[pm.info.iterationIndex || 0];

// 设置当前请求要用的变量
pm.variables.set("confirm_email", currentCase.email);
pm.variables.set("current_test_case", JSON.stringify(currentCase));
  1. 修改测试脚本,统一使用变量:
const currentCase = JSON.parse(pm.variables.get("current_test_case"));
const responseJson = pm.response.json();

pm.test(`${currentCase.isConfirmed ? "已验证" : "未验证"}邮箱:${currentCase.email}`, () => {
    pm.expect(responseJson.email).to.eql(currentCase.email);
    pm.expect(responseJson.register_type).to.eql('email');
    pm.expect(responseJson.email_confirmed).to.eql(currentCase.isConfirmed);
    
    // 仅对已验证邮箱测试额外字段
    if(currentCase.isConfirmed === "true"){
        pm.expect(responseJson.auth_key).to.be.eql(currentCase.authKey);
        pm.expect(responseJson.first_name).to.eql(currentCase.firstName);
        pm.expect(responseJson.last_name).to.eql(currentCase.lastName);
        pm.expect(responseJson.birthday_date).to.eql(currentCase.birthday);
    }
});
  1. 打开Postman集合运行器,选择该请求,设置迭代次数为2,运行即可依次测试两个场景。

内容的提问来源于stack exchange,提问作者Виталий Агарев

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 13:21:15