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

查看:120
本文介绍了课前的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 非静态函数?

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

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.
}

对于TestNG相当于JUnit 4的 @Before @After ,你可以使用 @BeforeMethod @AfterMethod 分别。

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

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

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