【问题标题】:Constructing test suites in Junit 4在 Junit 4 中构建测试套件
【发布时间】:2013-10-09 19:16:31
【问题描述】:

现在,我有这个简单的测试套件:

@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));
    }
}

但我不知道如何做这样的事情。有任何想法吗?谢谢!

【问题讨论】:

    标签: java unit-testing junit test-suite


    【解决方案1】:

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

    根据测试的情况,您可以选择 2 个选项:

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

    使用所有测试创建抽象测试类,然后让子类提供变量信息。抽象类可以在构造函数中接受参数,子类无参数构造函数调用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(){
    
        }
    

    }

    【讨论】:

      猜你喜欢
      • 2010-10-02
      • 2011-03-02
      • 2020-10-23
      • 2013-11-04
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多