2016-02-11 77 views
0

我有兩個類用於測試迴歸測試。在某些情況下,我們在類和我們通常使用斷言的方法中有多個測試方法。我想知道是否有任何方法可用,只能使用@Rule測試方法才能使用該類中的最後一個方法。這裏是我的代碼:如何僅定義最後的測試方法斷言將涉及

@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
public class JustOneClass extends ParentClass { 

    @Rule 
    public class GeneralRule articleHotspotRule = new class GeneralRule (this); 

    @Test 
    aMethod(){ 
     Assert.assertTrue() 
    } 
    @Test 
    bMethod(){ 
     Assert.assertTrue() 
    } 
    @Test 
    cMethod(){ 
     Assert.assertTrue() 
    } 
    @Test 
    dMethod(){ 
     if this assert is failed Assert.assertTrue() 
    } 
} 

我們有一個在這種情況下,我想這baseTest.after()只會如果dMedthod斷言失敗使用擴展TestWatcher

public class GeneralRule extends TestWatcher { 

    private ParentClass baseTest; 

    public GeneralRule (final GeneralRule generalRule) { 
     this.baseTest = generalRule; 
    } 

    @Override 
    protected void failed(final Throwable e, final Description description) { 
     baseTest.after(); 
    } 

} 

另一個類。

+0

你想創建一個規則,只有當TestClass的最後一個測試方法失敗時才執行TestClass的方法嗎?是嗎? – acdcjunior

+0

它可能有助於解釋你正在嘗試解決的問題,而這看起來很奇怪 – tddmonkey

+0

我只想在一個條件下執行一個方法base.after(),即如果assert方法失敗(標記爲up在de dMethod())中。 – user3360123

回答

1

與其使用規則來嘗試和檢查失敗,如何檢查失敗條件,然後以編程方式失敗測試?當然,不像規則那樣優雅或可重用,但可以滿足您的要求。

@Test 
public void dMethod() { 
    ... 
    if(actual == false) {    // check for failure scenario 
     after();      // call the after method 
     Assert.fail("hello failure"); // programatically fail the test 
    } 
}