Junit 课前(非静态) [英] Junit before class ( non static )

查看:22
本文介绍了Junit 课前(非静态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何最佳实践可以让 Junit 在测试文件中执行一次函数,并且它也不应该是静态的.

Are there any best practices to get Junit execute a function once in a test file , and it should also not be static.

喜欢在非静态函数上使用 @BeforeClass 吗?

like @BeforeClass on non static function?

这是一个丑陋的解决方案:

Here is an ugly solution :

@Before void init(){
    if (init.get() == false){
        init.set(true);
        // do once block
    }
}

这是我不想做的事情,我正在寻找一个集成的 junit 解决方案.

well this is something i dont want to do , and i am looking for an integrated junit solution.

推荐答案

如果你不想设置一次性初始化的静态初始化器并且对使用 JUnit 不讲究,可以看看 TestNG.TestNG 支持非静态、一次性初始化,具有多种配置选项,全部使用注解.

If you don't want to set up static initializers for one time initialization and are not particular about using JUnit, take a look at TestNG. TestNG supports non-static, one-time initialization with a variety of configuration options, all using annotations.

在 TestNG 中,这相当于:

In TestNG, this would be equivalent to:

@org.testng.annotations.BeforeClass
public void setUpOnce() {
   // One time initialization.
}

对于拆解,

@org.testng.annotations.AfterClass
public void tearDownOnce() {
   // One time tear down.
}

对于与 JUnit 4 的 @Before@After 等效的 TestNG,您可以使用 @BeforeMethod@AfterMethod分别.

For the TestNG equivalent of JUnit 4's @Before and @After, you can use @BeforeMethod and @AfterMethod respectively.

这篇关于Junit 课前(非静态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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