【问题标题】:POSTman - Test with custom function throws an assertion errorPOSTman - 使用自定义函数进行测试会引发断言错误
【发布时间】:2019-11-07 05:31:16
【问题描述】:

我正在使用 Postman 分析 JSON 文件进行一些测试。我意识到大多数时候我最终都会对一堆属性进行相同的测试,因此我决定创建一个自定义函数并将其作为参数添加到测试中。

一旦我这样做了,只要断言失败,它就会抛出一个断言错误,而不是测试本身失败。测试不会出现在“测试结果”窗口中。

它在断言成功时起作用。测试通过并显示在“测试结果”窗口中。

它可以在没有自定义函数的情况下工作,但在这种情况下,每个测试都会有自己的函数,使用相同的代码行,这就是我想要避免的。

function myFunction(propertyName,testData) {
    {   
            return pm.expect(testData).to.have.property(propertyName);
    }
}

var data = JSON.parse(responseBody);

var testData = data.entries;
/*TESTS USING CUSTOM FUNCTION*/
for (var i = 0; i < 5; i++) { 
    try{
    //This test PASSES and shows in the Test Results window.
        pm.test("test1", myFunction('summonerId',testData[i])); 
    }catch(error){
        console.log(error);
    }
    try{
    //This test PRODUCES AN ERROR and DOESN'T shows in the Test Results window.
        pm.test("test2", myFunction('somethingElse',testData[i])); 

    }catch(error){
        console.log(error);
    }
 }
/*TESTS USING ONE FUNCTION PER TEST*/
for (var i = 0; i < 5; i++) { 

    try{
    //This test PASSES and shows in the Test Results window.
        pm.test("test1", function(){
           pm.expect(testData).to.have.property('summonerId'); 
        }); 

    }catch(error){
        console.log(error);
    }
    try{
    //This test FAILS and shows in the Test Results window, without errors
        pm.test("test2", function(){
            pm.expect(testData).to.have.property('somethingElse'); 
        });
    }catch(error){
        console.log(error);
    }
 }

因此,当使用自定义函数时,如果断言成功,一切都很好,并且测试显示为 PASSED,但是如果断言失败,它会抛出错误而不是测试失败,并且测试不会显示为'失败'。

断言错误:

message: "expected { Object (summonerId, summonerName, ...) } to have property 'somethingElse'"
name: "AssertionError"
showDiff: false
stack: "AssertionError: expected { Object (summonerId, summonerName, ...) } to have property 'somethingElse'
    at myFunction (eval at exec (evalmachine.<anonymous>:13040:2548), <anonymous>:9:52)
    at Object.eval (eval at exec (evalmachine.<anonymous>:13040:2548), <anonymous>:24:30)
    at Uniscope.exec (evalmachine.<anonymous>:13040:2583)
    at module.exports (evalmachine.<anonymous>:69:538)
    at Object.<anonymous> (evalmachine.<anonymous>:72:1871)
    at evalmachine.<anonymous>:16:26
    at Array.forEach (<anonymous>)
    at Object.emit (evalmachine.<anonymous>:15:54)
    at evalmachine.<anonymous>:52:24
    at evalmachine.<anonymous>:5:21"

总测试显示 15 测试。 10 次通过 5 次失败。

缺少 5 个测试(在使用自定义函数时应该失败的测试)。

{
    "tier": "CHALLENGER",
    "leagueId": "65ebcd4f-368c-30f6-a635-976beb0e1a8c",
    "queue": "RANKED_SOLO_5x5",
    "name": "Varus's Outriders",
    "entries": [
        {
            "summonerId": "bPZhuLrFtF8ntu6a3JfnYNFX-NfG0LFX7aelPDd2Xegav-MM",
            "summonerName": "dang qiu qian",
            "leaguePoints": 739,
            "rank": "I",
            "wins": 162,
            "losses": 130,
            "veteran": true,
            "inactive": false,
            "freshBlood": false,
            "hotStreak": false
        },
        {
            "summonerId": "gk0lUIs1j9pMAWxa723hurPSDXAGbaXFJRdfek9WfQvWURA",
            "summonerName": "DV1 Matislaww",
            "leaguePoints": 795,
            "rank": "I",
            "wins": 757,
            "losses": 680,
            "veteran": false,
            "inactive": false,
            "freshBlood": false,
            "hotStreak": false
        }
]}

【问题讨论】:

  • @Div 刚刚添加,完全忘记了。
  • 这有助于理解我在这里遇到的问题,link。知道如何让它抛出 AssertionFailure 而不是 AssertionError?

标签: javascript testing postman assert


【解决方案1】:

感谢@prateeksarda 在这篇文章Postman AssertionError when testing using a defined function 中的帮助。

确实,调用函数的方式有所不同。 如果不是显式调用函数,而是在回调中调用它,结果将是正确的。

for (var i = 0; i < 5; i++) { 

        pm.test("test1", function(){
            myFunction('summonerId',testData[i])
        }); //This test PASSES and shows in the Test Results window.

        pm.test("test2", function(){
            myFunction('somethingElse',testData[i])
        }); //This test FAILS and shows in the Test Results window. :)

 }

这就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2020-06-25
    • 2015-09-02
    相关资源
    最近更新 更多