所有junit测试后清理 [英] Cleanup after all junit tests

查看:250
本文介绍了所有junit测试后清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我必须在所有测试之前进行一些存储库设置。这是使用一些棘手的静态规则完成的。然而,我不知道在所有测试之后如何进行清理。我不想保留一些神奇的静态数字,指的是所有测试方法的数量,我应该一直保持这些数字。

In my project I have to do some repository setup before all tests. This is done using some tricky static rules. However I've got no clue how to do clean up after all the tests. I don't want to keep some magic static number referring the number of all test methods, which I should maintain all the time.

最值得赞赏的方法是添加一些在所有测试后调用的监听器。在JUnit4中是否有任何接口?

The most appreciated way is to add some listener which would be invoked after all the tests. Is there any interface for it already in JUnit4?

编辑:这与@BeforeClass和@AfterClass无关,因为我必须知道最后一次是否调用了使用@AfterClass注释的方法。

edit: this has nothing to do with @BeforeClass and @AfterClass, cause I have to know if method annotated with @AfterClass is invoked for the last time.

推荐答案

我正在使用JUnit 4.9。这会有帮助吗?:

I'm using JUnit 4.9. Will this help?:

import junit.framework.TestCase;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({First.class,Second.class,Third.class})
public class RunTestSuite extends TestCase {
    @BeforeClass
    public static void doYourOneTimeSetup() {
        ...
    }

    @AfterClass
    public static void doYourOneTimeTeardown() {
        ...
    }    
}

编辑:我非常积极(除非我误解了你的问题)我的解决方案正是你要找的。即所有测试运行后的一种拆卸方法。不需要监听器,JUnit有这个功能。谢谢。

I am quite positive (unless I misunderstand you question) that my solution is what you are looking for. i.e. one teardown method after all your tests have ran. No listener required, JUnit has this facility. Thanks.

这篇关于所有junit测试后清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆