2013-09-30 54 views
-1

即使TestNG中的一個或多個斷言失敗,我也不得不繼續測試。 爲了在我的項目中實現軟斷言,我在下面提到了鏈接。軟斷言是如何工作的

http://beust.com/weblog/2012/07/29/reinventing-assertions/

http://seleniumexamples.com/blog/guide/using-soft-assertions-in-testng/

http://www.seleniumtests.com/2008/09/soft-assertion-is-check-which-doesnt.html

,但我不理解的代碼執行的流程,類似於函數調用,FLOW。

請幫助我瞭解soft asserions的工作流程。

代碼:

import org.testng.asserts.Assertion; 
    import org.testng.asserts.IAssert; 

    //Implementation Of Soft Assertion 
    public class SoftAssertions extends Assertion{ 
    @Override public void executeAssert(IAssert a){ 
    try{ a.doAssert(); } 
    catch(AssertionError ex){ 
    System.out.println(a.getMessage()); } } } 

    //Calling Soft Assertion 
SoftAssertions sa = new SoftAssertions(); 
sa.assertTrue(actualTitle.equals(expectedTitle), 
"Login Success, But Uname and Pwd are wrong"); 

注:繼續執行,即使上述斷言失敗

感謝 馬赫什

+0

你能告訴我們你的代碼嗎?你嘗試了什麼?你期望什麼?反而發生了什麼? – Seanny123

+0

那麼, 代碼如下。 import org.testng.asserts.Assertion; import org.testng.asserts.IAssert; //實現軟斷言 公共類SoftAssertions延伸斷言{\t @覆蓋 公共無效executeAssert(IAssert一){ 嘗試{ a.doAssert(); catch(AssertionError ex){ System.out.println(a.getMessage()); }}} //調用Soft Assertion SoftAssertions softAssertions = new SoftAssertions(); softAssertions.assertTrue(actualTitle.equals(expectedTitle),「Login Successful Eventhough Username and Password Is Wrong」); 執行繼續,即使上述斷言失敗 –

+0

請將此添加到您的問題。閱讀和理解格式正確的代碼要容易得多,並且使未來的讀者更容易理解這個問題。 – Seanny123

回答

0

軟斷言在本地狀態下儲存失敗(也許他們記錄到stderr作爲工作他們遇到)。測試完成後,需要檢查是否存在任何存儲的故障,如果遇到任何故障,則在該時間點通過整個測試。

我相信TestNG的維護人員想到的是在測試結束時調用myAssertion.assertAll(),該測試將運行Assert.fail(),並且如果任何先前的軟斷言檢查失敗,則使測試失敗。

您可以通過添加一個@Before方法來初始化本地軟斷言對象,在測試使用它,並添加@After方法來運行你的軟斷言對象的assertAll()方法這一點你自己。

請注意,這種方法使得您的測試不是線程安全的,因此每個測試必須在測試類的新實例中運行。如果您的測試需要線程安全,則在測試方法本身內部創建軟斷言對象,並在方法末尾運行assertAll()檢查更可取。 TestNG的一個很酷的特性就是它能夠運行多線程測試,所以在實現這些軟斷言時要注意。

+0

有關處理軟斷言的抽象類的示例,請參閱:http://stackoverflow.com/a/25068860/2167531 –