简体   繁体   English

junit 3中的类拆解?

[英]Class teardown in junit 3?

We have a lot of integration tests written using JUnit 3 , though we're now running them with 4.4 . 我们使用JUnit 3编写了很多集成测试,尽管我们现在使用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). 其中一些需要一个tearDown方法,该方法在类中的所有测试完成后运行(以释放一些公共资源)。

I see that this can be done in junit 4 with @AfterClass (org.junit). 我看到这可以在junit 4中使用@AfterClass(org.junit)完成。 However, mixing this into existing junit 3 tests that extend TestCase (junit.framework.*) doesn't seem to work. 但是,将其混合到扩展TestCase(junit.framework。*)的现有junit 3测试中似乎不起作用。 [BTW is there a migration tool yet? [BTW有迁移工具吗? Question 264680 indicated there wasn't one a year ago.] 问题264680表明一年前没有一个。]

I've seen mention of using junit.extensions.TestSetup for this kind of thing. 我已经看到过使用junit.extensions.TestSetup来提到这种事情。 My brief testing of this didn't seem work. 我对此的简要测试似乎不起作用。 Any examples? 任何例子?

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. 但我现在意识到这是因为我们的测试框架调用junit的方式与默认方式不同,所以它没有调用以下解决方案中所需的“套件”方法。 In Eclipse, if I use the bundled junit runner to directly execute one Test/TestCase, it runs fine. 在Eclipse中,如果我使用捆绑的junit runner直接执行一个Test / TestCase,它运行正常。

A way to do this setup/teardown once per class in junit 3 is to use TestSuite. 在junit 3中每个类进行一次设置/拆卸的方法是使用TestSuite。 Here's an example on junit.org: 这是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 ");
        }
    };
}

In JUnit 4, your test class doesn't need to extend TestCase . 在JUnit 4中,您的测试类不需要扩展TestCase Instead you must ensure that the name of your test class matches *Test and each test method is annotated with @Test . 相反,您必须确保测试类的名称与*Test匹配,并且每个测试方法都使用@Test注释。 Have you tried making these changes, then adding the @AfterClass annotation to the relevant method? 您是否尝试过进行这些更改,然后将@AfterClass注释添加到相关方法中?

There are annotations to replace most/all functionality you may currently be using from TestCase . 有一些注释可以替换您当前在TestCase使用的大多数/所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM