以前,我使用 Suite.class 指定要為具有以下結構方案的特定套件運行的測試: 套件@RunWith(Suite.class)@SuiteClasses({TC0.class, TC1.class...})public class mainTester { @BeforeClass public static void setUp() {//create xml report - create file, start xml structure} @AfterClass public static void tearDown(){//close xml report - close xml structure, close writer}測試@RunWith(Parameterized.class)public class TC0 extends testXMLReporter{ //Tests with params}最后testXMLReporter持有規則@Rulepublic TestRule watchman = new TestWatcher(){ //@Override apply, succeeded, failed to write custom xml structure for xml report}但是現在我需要創建套件來動態讀取文件中的數據,我從 Suite.class 切換到AllTests.class. 使其工作的唯一方法是更改我的套件 - 刪除@SuiteClasses - 將@RunWith值更改為AllTests.class - 并添加suite()方法public static TestSuite suite() { TestSuite suite = new TestSuite(); for(Class<?> klass : CsvParser.readCSV()) { suite.addTest(new junit.framework.JUnit4TestAdapter(klass)); } return suite;}在讀取文件和運行測試方面運行良好,但現在突然間我的@Before/AfterClass 注釋以及@Rule沒有運行(我的報告不再創建)。這是什么原因,我該如何解決?
1 回答

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
最后我放棄了使用獨占AllTests.class,現在使用Suite.class調用AllTests.class. 例子:
主要的
Result result = JUnitCore.runClasses(intercessor.class);
代禱者
@RunWith(Suite.class)
@SuiteClasses({mainTester.class})
public class intercessor {
//do Before & After
}
主測試器
@RunWith(AllTests.class)
public class mainTester {
public static TestSuite suite() {
TestSuite suite = new TestSuite();
for(Class<?> klass : CsvParser.readCSV()) {
suite.addTest(new junit.framework.JUnit4TestAdapter(klass));
}
return suite;
}
}
希望它在未來對某人有所幫助
添加回答
舉報
0/150
提交
取消