簡體   English   中英

在Junit 4中構建測試套件

[英]Constructing test suites in Junit 4

現在,我有一個簡單的測試套件:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    CommentingTest.class,
    SubscriptionsTest.class,
    EditingTest.class,
    BasicOperationsTest.class
})
public class WebTestSuite { }

但是,現在我想將參數傳遞給這些測試類,以告訴他們是否使用管理員帳戶進行測試,是否以視圖模式A或B進行測試,等等。我希望可以這樣做:

@RunWith(Suite.class)
public class WebTestSuite {
    public WebTestSuite() {
        this.addTest(new CommentingTest(Accounts.ADMIN, ViewMode.A));
        this.addTest(new CommentingTest(Accounts.ADMIN, ViewMode.B));
        this.addTest(new CommentingTest(Accounts.GUEST, ViewMode.B));
        this.addTest(new SubscriptionsTest(Accounts.ADMIN, ViewMode.A));
        this.addTest(new SubscriptionsTest(Accounts.ADMIN, ViewMode.B));
        this.addTest(new SubscriptionsTest(Accounts.GUEST, ViewMode.B));
        this.addTest(new EditingTest(Accounts.ADMIN, ViewMode.A));
        this.addTest(new EditingTest(Accounts.ADMIN, ViewMode.B));
        this.addTest(new EditingTest(Accounts.GUEST, ViewMode.B));
        this.addTest(new BasicOperationsTest(Accounts.ADMIN, ViewMode.A));
        this.addTest(new BasicOperationsTest(Accounts.ADMIN, ViewMode.B));
        this.addTest(new BasicOperationsTest(Accounts.GUEST, ViewMode.B));
    }
}

但是我不知道該怎么做。 有任何想法嗎? 謝謝!

您無法按照列出的方式進行操作,因為測試類需要具有無參數的構造函數。

您可以根據測試方式選擇2種方法:

選項1.使用具有以下參數的子類制作抽象測試類:

使所有測試都成為抽象測試類,然后讓子類提供變量信息。 抽象類可以在構造函數中使用參數,子類no-arg構造函數使用適當的參數調用super(...)

public abstract class AbstractCommentingTest{

    private Account account;
    private ViewMode mode;

    public AbstractCommentingTest(Account a, ViewMode v){
       this.account=a;
       this.viewMode = v;
    }


    //Put your tests here using the given account and view
    @Test
    public void foo(){

    }

    @Test
    public void bar(){

    }


}

然后你的具體課程

public class AdminViewACommentingTest extends AbstractCommentingTest{
      //no-arg constructor for JUnit
      public AdminViewACommentingTest(){
          super(Accounts.ADMIN, Viewmode.A);
      }
}

這可行,但是如果有很多選擇的話會很快失控

選項2:使用Junit參數化測試可具有所有選項組合:

我假設Accounts和ViewMode是枚舉? 如果是這樣,您可以輕松地使用values()方法創建所有可能的組合,作為參數化測試集的一部分。

@RunWith(Parameterized.class)
public class CommentingTest{

     @Parameters
     public static Collection<Object[]> createData(){
                List<Object[]> data = new ArrayList<Object[]>();

                 for(Accounts account : Accounts.values()){
                    for(ViewMode view : ViewMode.values()){
                         data.put(new Object[]{account, view});
                    }
                  }
                 return data;
    }


    private Account account;
    private ViewMode mode;

    public CommentingTest(Account a, ViewMode v){
       this.account=a;
       this.viewMode = v;
    }

    //Put your tests here using the given account and view
    @Test
    public void foo(){

    }

    @Test
    public void bar(){

    }

}

暫無
暫無

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

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