JUnit 从 Suite 切换到 AllTests 导致注释不是 运行

JUnit switch from Suite to AllTests causes annotations to not be run

以前我使用 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 持有规则

@Rule
public TestRule watchman = new TestWatcher(){
    //@Override apply, succeeded, failed to write custom xml structure for xml report
}

但是从现在开始我需要创建套件动态地从我从 Suite.class 切换到 AllTests.class 的文件中读取数据。让它工作的唯一方法是更改​​我的 Suite - 删除 @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;
}

在读取文件和 运行ning 测试方面运行得很好,但现在突然间我的 @Before/AfterClass 注释以及 @Rule 不是 运行ning(不再创建我的报告)。这是什么原因,我该如何解决?

最后我放弃了独占AllTests.class,现在用Suite.class调用AllTests.class。示例:

主要

Result result = JUnitCore.runClasses(intercessor.class);

代祷者

@RunWith(Suite.class)
@SuiteClasses({mainTester.class})
public class intercessor {
    //do Before & After
}

mainTester

@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;
    }

}

希望对以后的人有所帮助