我希望在任何测试开始运行之前为我的整个测试套件设置数据。我知道maven一个接一个地运行测试而不是套件,所以我不能使用@SuiteClasses。此外,我不想通过dbunit-maven-plugin创建数据集,必须通过REST创建数据集。有没有办法我可以运行特定的类作为maven预集成测试和集成后测试的一部分来设置和清理?
例如
public class TestInit
{
public void setUp()
{
//Data setup
}
public void tearDown()
{
//Data clean up
}
}
在测试套件启动之前运行安装程序,在结束之后运行tearDown。或者我可以运行2个单独的类,如TestInitSetup和TestInitTearDown?
答案 0 :(得分:4)
Here是基于规则的解决方案。它可能很有用。
语法如下:
public class SimpleWayToUseDataSetTest {
@Rule
public DataSetRule rule = new DataSetRule(); // <-- this is used to access to the testVectors from inside the tests
public static class MyDataSet extends SimpleTestVectors {
@Override
protected Object[][] generateTestVectors() {
return new Object[][] {
{true, "alpha", new CustomProductionClass()}, // <-- this is a testVector
{true, "bravo", new CustomProductionClass()},
{false, "alpha", new CustomProductionClass()},
{false, "bravo", new CustomProductionClass() }
};
}
}
@Test
@DataSet(testData = MyDataSet.class) // <-- annotate the test with the dataset
public void testFirst() throws InvalidDataSetException { // <-- any access to testData may result in Exception
boolean myTextFixture = rule.getBoolean(0); // <-- this is how you access an element of the testVector. Indexing starts with 0
String myAssertMessage = rule.getString(1); // <-- there are a couple of typed parameter getters
CustomProductionClass myCustomObject = (CustomProductionClass) rule.getParameter(2); // <-- for other classes you need to cast
Assert.assertTrue(myAssertMessage, true);
}
}
答案 1 :(得分:1)
如果你在JUnit中找不到解决方案,TestNG支持@BeforeSuite和@AfterSuite,它们似乎可以做你想要的。