Java 在JUnit4中构建测试套件
现在,我有一个简单的测试套件:Java 在JUnit4中构建测试套件,java,unit-testing,junit,test-suite,Java,Unit Testing,Junit,Test Suite,现在,我有一个简单的测试套件: @RunWith(Suite.class) @Suite.SuiteClasses({ CommentingTest.class, SubscriptionsTest.class, EditingTest.class, BasicOperationsTest.class }) public class WebTestSuite { } 但现在我想将参数传递给这些测试类,告诉它们是否使用管理员帐户进行测试,是否在视图模式A或B下进行
@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。使用具有以下参数的子类创建抽象测试类: 让抽象测试类包含所有测试,然后让子类提供变量信息。抽象类可以接受构造函数中的参数,子类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参数化测试使每个选项组合:
我假设帐户和视图模式是枚举?如果是这样,您可以轻松地使用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(){
}
@RunWith(参数化的.class)
公共类评论测试{
@参数
公共静态集合createData(){
列表数据=新的ArrayList();
对于(帐户:Accounts.values()){
对于(ViewMode视图:ViewMode.values()){
put(新对象[]{account,view});
}
}
返回数据;
}
私人帐户;
私有视图模式;
公共评论测试(帐户a,查看模式v){
这个账户=a;
this.viewMode=v;
}
//使用给定的帐户和视图将测试放在此处
@试验
公共图书馆{
}
@试验
公共空白栏(){
}
}