JUnit 4 基本功能

发布于 2024-11-14 11:32:49 字数 328 浏览 2 评论 0 原文

JUnit 4 创建测试用例以使用通用功能的正确方法是什么:例如,几个单元测试类通用的设置?我所做的是创建一个测试用例并将通用功能放入 @Before 方法中,然后任何需要此功能的测试用例都会扩展基类。然而,这似乎需要在每个子类中执行: super.setUp()

有更好的办法吗?

编辑

实际上我提出的解决方案不起作用。有时 JUnit 会调用基类两次。如果碰巧首先在基类上运行测试,则一次,当它到达子类时再次运行测试(至少我认为这是正在发生的事情)。因此,“继承”通用测试用例功能的更好方法会很棒。

What is the proper way with JUnit 4 to create test cases to use common functionality: e.g. setup that is common to several unit test classes? What I did was create a test case and put the common functionality in the @Before method, then any test case that needs this would extend the base class. However, this seems to require doing: super.setUp() in every subclass.

Is there a better way?

EDIT

Actually my proposed solution is not working. Sometimes JUnit will call the base class TWICE. Once if it happens to run the test on the base class first, and again when it reaches a child class (at least I think this is whats happening). So a better way of "inheriting" common test case functionality would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无力看清 2024-11-21 11:32:49

如果您使用 super.setup() “nofollow">@Before注释

在编写测试时,经常会发现需要多个测试
在它们之前创建的类似对象
可以运行。注释公共空白
带有 @Before 的方法会导致该方法
在测试方法之前运行。这
超类的 @Before 方法将
在当前的之前运行
类。

我建议这样:

  @Before
  public void initForAll() {}

在超级/主类

和任何

  @Before
  public void initTest() {...}

测试用例中。

编辑:

在编辑中回答您的问题。

  1. 您可以使用 @BeforeClass ,每个 TestClass 都会调用一次。
  2. 但我认为你正在寻找类似惰性/静态初始化的东西。

我这样做:

private boolean initialized = false;

@BeforeClass
public static void init()
{
  if(initialized)
  {
    return;
  }

  //Initialize everything

  initialized = true;
}

You don't need to invoker super.setup() if you use the @Before annotation:

When writing tests, it is common to find that several tests need
similar objects created before they
can run. Annotating a public void
method with @Before causes that method
to be run before the Test method. The
@Before methods of superclasses will
be run before those of the current
class.

I'd suggest something like this:

  @Before
  public void initForAll() {}

In the super/Main class

and any

  @Before
  public void initTest() {...}

In your Testcases.

EDIT:

To answer your questions in the edit.

  1. You could use @BeforeClass which will be invoked once per TestClass.
  2. But I Think you are looking for something like a lazy /static Initialisation.

I do this like this:

private boolean initialized = false;

@BeforeClass
public static void init()
{
  if(initialized)
  {
    return;
  }

  //Initialize everything

  initialized = true;
}
月棠 2024-11-21 11:32:49

这是一个非常好的方法。如果子类上有 setUp(),那么是的,您必须调用 super.setUp(),但实际上,这有多难?

thats a pretty good way to do it. If you have a setUp() on the subclass, then yes, you have to invoke super.setUp(), but really, how hard is that?

酸甜透明夹心 2024-11-21 11:32:49

您是否尝试过其他方法。
不要创建具有公共功能的基类,而是创建一个具有公共功能的静态方法的 util 类。然后只需在每个测试用例的@Before注释方法中调用util方法即可。

Have you tried other approach.
Instead of creating a base class with a common functionality, make an util class with static methods which will the common functionality. Then just call util methods in @Before annotated method of each test case.

梨涡少年 2024-11-21 11:32:49

如果您的基类有一个方法 public void setUp(),您的子类也有一个方法 public void setUp(),那么子类的 setUp 方法覆盖基类的同名和签名的方法,因此只有子类的方法将被调用(并且它可能在应该调用基类的方法时被调用;对此不确定)。

解决方案是调用您的基类设置方法,例如baseSetup()。然后它们都会被自动调用。 Joachim 的答案确实解决了命名问题,但我想更清楚地了解方法重写的效果。

If your base class has a method public void setUp(), and so does your sub-class, then the sub-class's setUp method overrides the base class's method of the same name and signature, so only the sub-class's method will be called (and it may be called when the base class's method should have been called; not sure about this).

The solution is to call your base class setup method something else, e.g. baseSetup(). Then they will both be called automatically. Joachim's answer does address the naming, but I wanted to be clearer about the effects of method override.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文