2013-07-24 28 views
0
捕問題

請找代碼,如果在TestNG的報告應該是一個失敗狀態,然後拋出的任何斷言錯誤斷言錯誤(TestNG的)用java

Test(dataProvider = "User_login") 
public void StatusForm_Verification(String uname, String pwd) 
    throws InterruptedException { 
    try { 
NavigateToLogin(); 
Dashboard RD = LoginAs_user(uname, pwd); 
Thread.sleep(2000); 

    if (Integer.parseInt(ReviewedStatuscount) >= 1) { 

     Assert.assertEquals("true", 
       revui.Btn_SaveReview.getAttribute("disabled")); 

     Assert.assertEquals("true", 
       revui.Btn_submitReview.getAttribute("disabled")); 

     Assert.assertEquals("true", 
       revui.Btn_Needmoreinfo.getAttribute("disabled")); 

     status = TestLinkAPIResults.PASSED;  

    } else { 
     throw new SkipException(
       "Test Skipping - Reviewed count is Zero"); 
    } 
    }catch((AssertionError ex) { 
     testlink_result = TestLinkAPIResults.TEST_FAILED; 
     msg = ex.getMessage(); 
} 
} 

同時執行上面的方法 - 這是正確的

但是,如果我在上述方法中使用Try catch塊(捕獲AssertionError),TestNG報告始終爲PASS狀態,爲什麼?怎麼樣?

回答

5

我對TestLink並不熟悉,但在正常情況下,TestNG可以知道測試失敗的唯一方法就是捕獲拋出的異常。當你在這裏捕獲異常時,你會「吞下」它,阻止它進入TestNG框架。

爲了解決這個問題,只需重新拋出異常做你TestLink的處理之後,像這樣:

} catch (AssertionError ex) { 
    testlink_result = TestLinkAPIResults.TEST_FAILED; 
    msg = ex.getMessage(); 

    // Rethrow the AssertionError to signal to TestNG that the test failed. 
    throw ex; 
}