2015-10-17 67 views
-1

我想要在Selenium WebDriver腳本中的Assert條件失敗時繼續執行直到結束。在我的代碼中,當Assert條件失敗時,它將停止執行其餘的代碼。當Selenium WebDriver中的Assert條件失敗時無法繼續

這是我寫的代碼。需要幫忙。

try 
{ 
    Assert.assertEquals(false, status); 
} 
catch(Exception Ex) 
{ 
    System.out.println("Disabled user can register for workshop : case Failed "); 
} 

回答

0

使用exceptions to control the flow of logic通過代碼被認爲是不好的做法(你捕捉異常,寫一個錯誤信息,並繼續處理)。

而不是使用assertEquals的,你可以直接檢查條件:

if (status == false) { 
    System.out.println("Disabled user can register for workshop : case Failed "); 
} 

另外,如果你仍然想在測試失敗時打印的附加信息(並停止在測試進一步檢查)時,Asserts通常提供了可選的最後一個參數的重載傳達這樣的信息時,該測試失敗:

Assert.assertEquals(false, status, 
"Disabled user can register for workshop : case Failed "); 
0

的JUnit有一個名爲ErrorCollector該CA規則n用於在執行結束時收集關於測試和報告的漸進統計信息。

不結合斷言和ErrorCollector,你會如果你的測試從聲明

未能在測試結束任何故障將被收集並在JUnit輸出集中上市失去ErrorCollector信息。

package stackoverflow.proof.junit; 

import org.hamcrest.core.Is; 
import org.hamcrest.core.IsEqual; 
import org.hamcrest.core.IsNull; 
import org.junit.Assert; 
import org.junit.Rule; 
import org.junit.Test; 
import org.junit.rules.ErrorCollector; 

/** 
* Test case for demonstrating some Junit Rule behaviors. 
* <p/> 
* Requires at least JUnit 4.7 
* 
* @see "http://stackoverflow.com/questions/33183789/not-able-to-continue-when-an-assert-condition-fails-in-selenium-webdriver/33183882#33183882" 
* @author http://stackoverflow.com/users/5407189/jeremiah 
* @since Oct 17, 2015 
* 
*/ 
public class JUnitRulesDemoTest { 
    /** 
    * Collector which can be used to track problems through an event series and still allow a test to run to 
    * completion. 
    */ 
    @Rule 
    public ErrorCollector collector = new ErrorCollector(); 

    /** 
    * Test illustrating ErrorCollector behavior. 
    * 
    */ 
    @Test 
    public void testErrorCollector() { 
     collector.checkThat("These things don't match", new Object(), IsEqual.equalTo(new Object())); 
     collector.checkThat(new Object(), IsNull.notNullValue()); 
     try { 
      throw new Exception("Demonstration Exception in collector"); 
     } catch (Exception ex) { 
      collector.addError(ex); 
     } 
     collector.checkThat("Status does not match!", true, Is.is(true)); 
     System.out.println("Done."); 
    } 

    /** 
    * Test illustrating ErrorCollector behavior when an Assert Fails the Test. 
    * 
    */ 
    @Test 
    public void testErrorCollectorWithAssert() { 
     collector.checkThat("These things don't match", new Object(), IsEqual.equalTo(new Object())); 
     collector.checkThat(new Object(), IsNull.notNullValue()); 
     try { 
      throw new Exception("Demonstration Exception in collector"); 
     } catch (Exception ex) { 
      collector.addError(ex); 
     } 
     //Test stops here. You won't see "done", nor will you see any of the previous ErrorCollector status' 
     Assert.assertTrue(false); 

     collector.checkThat("Status does not match!", true, Is.is(true)); 
     System.out.println("Done."); 
    } 

} 

JUnit Result

規則必須:

  • 被註解爲@rule
  • 聲明在您的測試公共

建議的解決方案:

在你列出你上面應該刪除try/catch塊,並使用匹配器,而不是org.junit.Assert.assertEquals的情況下:

@Rule 
public ErrorCollector collector = new ErrorCollector(); 

@Test 
public void testSomething() { 
    boolean status = doStatusEval(); 
    collector.checkThat("Disabled user can register for workshop", false, Is.is(status)); 
//Continue adding things to the collector. 
} 

祝你好運!

+0

在Junit截圖中,我應該指出兩個junit測試是在同一個會話中運行的。 * testErrorCollectorWithAssert *的證明在控制檯中被調用,您只能在兩個測試中看到一個「完成」語句。其中只有一個完成。 – Jeremiah

相關問題