java junit 3 中的类拆解?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1646586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:24:48  来源:igfitidea点击:

Class teardown in junit 3?

javajunitjunit4

提问by Cincinnati Joe

We have a lot of integration tests written using JUnit 3, though we're now running them with 4.4. A few of these have a need for a tearDown method that runs after all the tests in the class are complete (to free some common resources).

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

I see that this can be done in junit 4 with @AfterClass (org.junit). However, mixing this into existing junit 3 tests that extend TestCase (junit.framework.*) doesn't seem to work. [BTW is there a migration tool yet? Question 264680indicated there wasn't one a year ago.]

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

I've seen mention of using junit.extensions.TestSetup for this kind of thing. My brief testing of this didn't seem work. Any examples?

我已经看到提到使用 junit.extensions.TestSetup 来处理这种事情。我对此的简短测试似乎不起作用。有什么例子吗?

回答by Cincinnati Joe

Found the answer to my own question :) I had tried this briefly before posting my question, but it wasn't working for me. But I realize now that it's because our test framework is invoking junit differently than the default, so it wasn't calling the "suite" method that is needed in the following solution. In Eclipse, if I use the bundled junit runner to directly execute one Test/TestCase, it runs fine.

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

A way to do this setup/teardown once per class in junit 3 is to use TestSuite. Here's an example on junit.org:

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

Is there a way to make setUp() run only once?

有没有办法让 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 ");
        }
    };
}

回答by Dónal

In JUnit 4, your test class doesn't need to extend TestCase. Instead you must ensure that the name of your test class matches *Testand each test method is annotated with @Test. Have you tried making these changes, then adding the @AfterClassannotation to the relevant method?

在 JUnit 4 中,您的测试类不需要扩展TestCase. 相反,您必须确保测试类的名称匹配,*Test并且每个测试方法都使用@Test. 您是否尝试过进行这些更改,然后将@AfterClass注释添加到相关方法中?

There are annotations to replace most/all functionality you may currently be using from TestCase.

有注释可以替换您当前可能正在使用的大部分/所有功能TestCase