簡體   English   中英

JUnit 4測試套件問題

[英]JUnit 4 test suite problems

我在測試套件中運行的某些JUnit 4測試存在問題。

如果我單獨運行測試,則它們可以正常工作,但是當以套件形式運行時,大多數測試方法(90%的測試方法)會因錯誤而失敗。 我注意到的是,總是第一個測試可以正常工作,但其余的都失敗了。 另一件事是方法的一些測試未按正確的順序執行(反射無法按預期方式工作,或者它不能按預期的方式進行,因為方法的檢索不一定按創建的順序進行)。 如果使用相同名稱的方法進行了多個測試,通常會發生這種情況。 我嘗試調試一些測試,似乎從一行到下一行,某些屬性的值變為null

有誰知道是什么問題,或者行為是否“正常”?

提前致謝。

PS:OK,測試不依賴於對方,他們沒有這樣做,他們都有@BeforeClass@Before@After@AfterClass所以之間的測試一切都清理。 這些測試適用於數據庫,但是在@BeforeClass每個測試之前都會清除數據庫,因此這不是問題。

簡化示例:

測試套件:

import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
importy testclasses...;

@RunWith(Suite.class)
@Suite.SuiteClasses({ Test1.class, Test2.class })
public class TestSuiteX {
 @BeforeClass
 public static void setupSuite() { System.out.println("Tests started"); }   
 @AfterClass
 public static void setupSuite() { System.out.println("Tests started"); }   
}

測試:這些測試正在運行於Glassfish上的服務器應用程序上進行功能測試。

現在,測試擴展了一個具有@BeforeClass-方法的基類,該方法清除數據庫和登錄名,而@AfterClass僅進行注銷。 這不是問題的根源,因為在介紹此類之前,發生了相同的事情。

該類具有一些其他測試未使用的公共靜態屬性,並實現了2種控制方法。

其余的類,在本例中,這兩個類擴展了基類,並且不繼承繼承的controll方法。

測試類示例:

    imports....

    public class Test1 extends AbstractTestClass {  
    protected static Log log = LogFactory.getLog( Test1.class.getName() );

    @Test
    public void test1_A() throws CustomException1, CustomException2 {

        System.out.println("text");

        creates some entities with the server api.
        deletes a couple of entities with the server api.

        //tests if the extities exists in the database
        Assert.assertNull( serverapi.isEntity(..) );

    }

}

第二個:

public class Test1 extends AbstractTestClass {

    protected static Log log = LogFactory.getLog( Test1.class.getName() );

    private static String keyEntity;
    private static EntityDO entity;

    @Test
    public void test1_B() throws CustomException1, CustomException2 {

        System.out.println("text");

        creates some entities with the server api, adds one entities key to the static attribute and one entity DO to the static attribute for the use in the next method.
        deletes a couple of entities with the server api.

        //tests if the extities exists in the database
        Assert.assertNull( serverapi.isEntity(..) );

    }

    @Test
    public void test2_B() throws CustomException1, CustomException2 {

        System.out.println("text");

        deletes the 2 entities, the one retrieved by the key and the one associated with the static DO attribute

        //tests if the deelted entities exists in the database
        Assert.assertNull( serverapi.isEntity(..) );

    }

這是一個基本示例,實際測試更為復雜,但是我嘗試使用簡化測試,但仍然無法正常工作。 謝謝。

您描述的情況聽起來像是一個副作用。 您提到測試可以很好地隔離工作,但取決於操作順序:這通常是一個嚴重的症狀。

設置整套測試用例的部分挑戰是確保每個測試都從干凈狀態開始,執行其測試,然后自行清理,將所有內容恢復為干凈狀態的問題。

請記住,在某些情況下標准清理例程(例如, @Before@After )還不夠。 我前一段時間遇到的一個問題是在一組數據庫測試中:作為測試的一部分,我正在向數據庫中添加記錄,並且需要專門刪除剛添加的記錄。

因此,有時您需要添加特定的清理代碼以恢復到原始狀態。

看來您是在假設執行方法的順序是固定的前提下構建測試套件的。 這是錯誤的-JUnit不保證測試方法的執行順序,因此您不應依賴它。

這是設計使然-單元測試應該完全相互獨立。 為了保證這一點,JUnit創建了一個獨特的測試類新實例來執行每個測試方法。 因此,在一種方法中設置的任何屬性都將在下一種方法中丟失。

如果您具有通用的測試設置/拆卸代碼,則應將其放入單獨的方法中,並用@Before / @After注釋。 它們在每種測試方法之前和之后執行。

更新:您寫了

在@BeforeClass中的每個測試之前清除數據庫

如果這不是拼寫錯誤,則可能是問題的根源。 該數據庫應在清除@Before方法- @BeforeClass只運行一次為每個類。

在使用@BeforeClass @Before進行設置時,以及在每次單獨測試之前使用@Before進行設置時,請務必謹慎。 並注意實例變量。

如果您可以發布發生問題的簡化示例,我們也許可以提供更具體的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM