我正在尝试设置一些参数化测试套件,不幸的是到目前为止还没有任何运气。
我有两组参数,我想使用所有可能的组合运行多个测试用例(它们位于不同的类中)。我尝试使用 JUnit4 来完成此操作,但无法正确设置。这将是我的基本想法:
-
TestSuite1.class
设置一组参数,然后启动TestSuite2.class
.
-
TestSuite2.class
设置第二组参数,然后开始使用这两个参数的实际测试。
同时似乎不可能同时设置两者Suite.class
and Parameterized.class
in the RunWith
同时注释(根据google,Parameterized
延伸Suite
,如果我使用,我通常会收到“找不到可运行方法”消息。)
我的代码基本上是这样的:
TestSuite1.类:
@RunWith(Parameterized.class)
@Parameterized.SuiteClasses({TestSuite2.class})
//I have tried with @RunWith(Suite.class) and
//@Suite.SuiteClasses({TestSuite2.class}) annotations also - all combinations
public class TestSuite1{
public TestSuite1(int number) {
Params.first = number;
}
@Parameters
public static Collection<Object[]> parameters(){
Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
return Arrays.asList(data);
}
}
TestSuite2.class
看起来一样TestSuite1.class
,除了我添加了TestCase1.class
到套房而不是TestSuite2
,并且它设置了另一个变量Params
.
测试用例1.类:
public class TestCase1 {
@Test
public void test1(){
System.out.println("first: "+Params.first+" second: "+Params.second);
Assert.assertTrue(true);
}
}
我对所有想法持开放态度 - 即使是 TestNG。我也尝试过(虽然今天是我第一次看到它),但我注意到这些套件与 JUnit 中的有点不同。我不想在测试之前设置 XML 文件,我想以编程方式解决所有设置。
我想要实现的目标可以通过任何框架实现吗?
更新:使用 TestNG 我有以下代码:
开始.class:
public class Start {
public static void main(String[] args){
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { FirstTest.class, SecondTest.class });
testng.addListener(tla);
testng.run();
}
}
参数类:
public class Params {
@DataProvider(name = "param")
public static Object[][] createData() {
Object[][] data = new Object[][] { { 1 }, { 2}, { 3}, { 4} };
return data;
}
}
FirstTest.class:
public class FirstTest {
@Test(dataProvider = "param", dataProviderClass = Params.class)
public static void printIt(int number){
System.out.println("FirstTest: "+number);
}
}
SecondTest.class
是相同的FirstTest.class
。如果我运行这个,它就会运行FirstTest
4次,然后运行SecondTest
4次。我想跑步FirstTest
一次,并且SecondTest
一次也使用第一组参数。然后我想跑FirstTest
and SecondTest
一次,使用第二组参数,等等。
我尝试设置 setPreserveOrder(true),并尝试了所有 setParallel 选项。然而,通过这种方式,结果是随机顺序的。
(这将是一些硒测试。我知道测试不应该相互依赖,但这仍然是我想要的方式)