JUnit从Suite切换到AllTests导致注释无法运行

时间:2018-08-17 08:33:59

标签: java junit4

以前,我使用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;
}

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

1 个答案:

答案 0 :(得分:0)

最后,我放弃了仅使用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;
    }

}

希望它对以后的人有帮助