2015-01-03 79 views
5

發生失敗時,是否有辦法繼續測試套件? 例如:如何在CasperJS中斷言失敗時繼續測試用例?

casper.test.begin("",3,function suite(){ 
    casper.start(url).then(function(){ 
    test.assert(...); 
    test.assert(...); //If this assert fail, the script stop and the third assert isn't tested 
    test.assert(...); 
    }).run(function(){ 
    test.done(); 
    }); 
}); 

我希望所有斷言進行測試,即使一些失敗。可能嗎?

+0

進行單元測試這種行爲是你通常會想什麼。您可以使用「驗證」方法,該方法檢查但不停止測試執行(如下面的答案中所述)。像你一樣描述這種情況通常會描述不良的測試設計。在一次測試中測試過多。解決方案將被分解成單獨的測試。 – buxter

回答

6

請參閱casperjs google group post。我們可以圍繞與casper.then(..

斷言此後續代碼的工作就像我想要的(但這種方式是不是也許是最好的?)

casper.test.begin("",3,function suite(){ 
    casper.start(url).then(function(){ 
    casper.then(function(){ 
     test.assert(...); //if fail, this suite test continue 
    }); 
    casper.then(function(){ 
     test.assert(...); //so if assert(1) fail, this assert is executed 
    }); 
    casper.then(function(){ 
     test.assert(...); 
    }); 
    }).run(function(){ 
     test.done(); 
    }); 
}); 
+0

對於一些測試來說,這比Darren的建議更糟糕,但是當您在CasperJS上爲您的網站測試構建框架時,這可能是一種更乾淨的方法。 –

4

這通常是你在單元測試時想要的:如果它無論如何都會失敗,那就快點做吧。即在每個測試功能的第一個問題上失敗。而且,後來的測試通常假設通過了較早的測試,如果頁面標題錯誤並且說404,則沒有意義測試頁面上正確數量的圖像。

我猜你想要這個,這樣你可以得到測試結果的更多信息,要做到這一點是使用一個單一的斷言和自定義錯誤消息的一種方法:但是

var title = this.getTitle(); 
var linkText = this.getHTML('a#testLink'); 
this.assert(title == "MyPage" && linkText == "continue", 
    "title=" + title + ";a#testLink = " + linkText); 

那會變得凌亂。如果你想使用assert家庭功能的全部力量,而不是讓他們扔掉,而是繼續的the source code一項研究表明,這可能工作:

test.assert(false, null, {doThrow:false}); 
test.assertEquals(1 == 2, null, {doThrow:false}); 
test.assertEquals(2 == 2); 

如果你想這是所有斷言的默認行爲,嗯,黑客代碼可能是最好的選擇! (將doThrow的默認值true更改爲false。)

+0

我剛剛找到另一個解決方案=)我寫這個 – user2137454

+0

是的,那是另一種方式,很好找。 –