【问题标题】:True / false written on console , trying to write tests in postman. If false, fail the test真/假写在控制台上,试图用邮递员写测试。如果为假,则测试失败
【发布时间】:2020-06-14 06:11:18
【问题描述】:

我包含在控制台中打印为真/假的功能,我需要在邮递员中编写一个测试,以便如果发现错误,则测试失败并可以显示在测试选项卡上。在下面的 const testfalse= 在控制台中给出值 true 或 false。但是如果发现是假的,如果基金是假的,我需要进行测试才能失败。我尝试了 if 条件,它没有记录任何内容

    // Getting the category name and name key
var categname= pm.environment.get("categoryName");
console.log(categname)
var categnamekey= pm.environment.get("categorynameKey")
console.log(categnamekey)

var arr=JSON.parse(responseBody); 
function contains(arr, key, val) {
    for (var i = 0; i < arr.length; i++) {
        if(arr[i][key] === val) 
        return true;
    }
    return false;
}

// To verify if value present or not which prints true or false

const testfalse=console.log(contains(arr, "name", categname)); 
console.log(contains(arr,"nameKey",categnamekey));

if(testfalse=='false'){
    tests["fail"]
}else {
    tests["success"]
}

【问题讨论】:

    标签: javascript postman-testcase


    【解决方案1】:

    您可以使用pm.expect(someValue).to.equal(expectedValue) 方法来比较该值是否符合您的预期。如果不一样,结果中会显示失败的测试。

    您的testFalse 也应该只是检查,您可以随时使用console.log,它不会影响测试...它只会在测试运行时给您一个日志供您查看。

    const testfalse = contains(arr, "name", categname);
    console.log('Category found: ' + testfalse); 
    
     //rest of code here
    
    //Test will either pass or fail after this line and reflect on test tab
    pm.expect(testfalse).to.equal(true); //maybe rename testPass
    

    https://learning.postman.com/docs/postman/scripts/test-scripts/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2022-08-16
      相关资源
      最近更新 更多