有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在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));
    }
}

但我不知道怎么做这样的事。有什么想法吗?谢谢


共 (1) 个答案

  1. # 1 楼答案

    你不能按照你列出的方式来做,因为测试类需要一个无参数构造函数

    根据测试内容,您可以选择两种方式:

    选项1。使用具有以下参数的子类创建抽象测试类:

    让抽象测试类包含所有测试,然后让子类提供变量信息。抽象类可以在构造函数中获取参数,子类no arg constructor使用适当的参数调用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参数化测试使每个选项组合:

    我假设帐户和视图模式是枚举?如果是这样,您可以轻松地使用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(){
    
        }
    

    }