猿问

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

以前,我使用 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. 使其工作的唯一方法是更改我的套件 - 删除@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没有运行(我的报告不再创建)。这是什么原因,我该如何解决?


江户川乱折腾
浏览 188回答 1
1回答

慕妹3146593

最后我放弃了使用独占AllTests.class,现在使用Suite.class调用AllTests.class. 例子:主要的Result result = JUnitCore.runClasses(intercessor.class);代祷者@RunWith(Suite.class)@SuiteClasses({mainTester.class})public class intercessor {&nbsp; &nbsp; //do Before & After}主测试器@RunWith(AllTests.class)public class mainTester {&nbsp; &nbsp; public static TestSuite suite() {&nbsp; &nbsp; &nbsp; &nbsp; TestSuite suite = new TestSuite();&nbsp; &nbsp; &nbsp; &nbsp; for(Class<?> klass : CsvParser.readCSV()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; suite.addTest(new junit.framework.JUnit4TestAdapter(klass));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return suite;&nbsp; &nbsp; }}希望它在未来对某人有所帮助
随时随地看视频慕课网APP

相关分类

Java
我要回答