JUnit TestCase对象实例化 [英] JUnit TestCase object instantiation

查看:230
本文介绍了JUnit TestCase对象实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否使用新的(或不同的) TestCase 对象实例来运行JUnit测试用例中的每个测试方法?或者一个实例被重用于所有测试?

Is a new (or different) instance of TestCase object is used to run each test method in a JUnit test case? Or one instance is reused for all the tests?

public class MyTest extends TestCase {
  public void testSomething() { ... }
  public void testSomethingElse() { ... }
}

运行此测试时,是否创建了多少个 MyTest 类的实例?

While running this test, how many instances of MyTest class is created?

如果可能,请提供链接到我可以验证行为的文档或源代码。

If possible, provide a link to a document or source code where I can verify the behaviour.

推荐答案

我在JUnit文档中找不到明确的答案关于你的问题,但正如anjanb所写,其意图是每个测试都独立于其他测试,因此可以为每个要运行的测试创建一个新的TestCase实例。

I couldn't find a clear answer in the JUnit docs about your question, but the intent, as anjanb wrote, is that each test is independent of the others, so a new TestCase instance could be created for each test to be run.

如果您希望在测试类中的所有测试用例中共享昂贵的测试设置( fixtures ),则可以使用 @BeforeClass 注释一个静态方法来实现这个结果: http://junit.sourceforge.net/javadoc_40/org/junit/BeforeClass.html 。但请注意,仍然可以为每个测试创建一个新实例,但这不会影响@BeforeTest方法初始化的静态数据。

If you have expensive test setup ("fixtures") that you want to be shared across all test cases in a test class, you can use the @BeforeClass annotation on a static method to achieve this result: http://junit.sourceforge.net/javadoc_40/org/junit/BeforeClass.html. Note however, that a new instance may still be created for each test, but that won't affect the static data your @BeforeTest method has initialized.

这篇关于JUnit TestCase对象实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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