junit 3中的类拆解?

Cin*_*Joe 9 java junit junit4

我们使用JUnit 3编写了很多集成测试,尽管我们现在使用4.4运行它们.其中一些需要一个tearDown方法,该方法在类中的所有测试完成后运行(以释放一些公共资源).

我看到这可以在junit 4中使用@AfterClass(org.junit)完成.但是,将其混合到扩展TestCase(junit.framework.*)的现有junit 3测试中似乎不起作用.[BTW有迁移工具吗?问题264680表明一年前没有一个.]

我已经看到过使用junit.extensions.TestSetup来提到这种事情.我对此的简要测试似乎不起作用.任何例子?

Cin*_*Joe 18

找到了我自己的问题的答案:)我在发布问题之前曾经尝试过这个问题,但这对我没有用.但我现在意识到这是因为我们的测试框架调用junit的方式与默认方式不同,所以它没有调用以下解决方案中所需的"套件"方法.在Eclipse中,如果我使用捆绑的junit runner直接执行一个Test/TestCase,它运行正常.

在junit 3中每个类进行一次设置/拆卸的方法是使用TestSuite.这是junit.org上的一个例子:

有没有办法让setUp()只运行一次?

public static Test suite() {
    return new TestSetup(new TestSuite(YourTestClass.class)) {

        protected void setUp() throws Exception {
            System.out.println(" Global setUp ");
        }
        protected void tearDown() throws Exception {
            System.out.println(" Global tearDown ");
        }
    };
}
Run Code Online (Sandbox Code Playgroud)