为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

江户川乱折腾 2021-07-13 13:01:41
以前,我使用 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;

    }


}

希望它在未来对某人有所帮助


查看完整回答
反对 回复 2021-07-29
  • 1 回答
  • 0 关注
  • 187 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信