【发布时间】:2014-01-20 09:43:47
【问题描述】:
我正在尝试设置一些参数化测试套件,不幸的是到目前为止没有任何运气。 我有两组参数,我想以所有可能的组合运行多个测试用例(它们在不同的类中)。我试图用 JUnit4 来做,但我无法正确设置它。这将是我的基本想法:
-
TestSuite1.class设置一组参数,然后启动TestSuite2.class。 -
TestSuite2.class设置第二组参数,然后开始使用这两个参数的实际测试。
同时似乎不可能同时在RunWith 注释中设置Suite.class 和Parameterized.class(根据谷歌,Parameterized 扩展Suite,我通常得到“无法运行方法找到”消息,如果我使用。)
我的代码基本上是这样的:
TestSuite1.class:
@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 中设置了另一个变量。
TestCase1.class:
public class TestCase1 {
@Test
public void test1(){
System.out.println("first: "+Params.first+" second: "+Params.second);
Assert.assertTrue(true);
}
}
我对所有想法持开放态度——例如使用 TestNG。我也尝试过(虽然今天是我第一次看到它),但我注意到套件与 JUnit 中的套件有些不同。我不想在测试之前设置 XML 文件,我想以编程方式解决所有设置。
我正在尝试通过任何框架实现的目标吗?
更新:使用 TestNG 我有以下代码:
开始类:
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();
}
}
Params.class:
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和SecondTest,用第二组参数等等。
我尝试设置 setPreserveOrder(true),并尝试了所有 setParallel 选项。然而,在这种方式下,结果是随机顺序的。
(这将是一些硒测试。我知道测试不应该相互依赖,但它仍然是我想要的方式)
【问题讨论】:
标签: java junit testng test-suite