java JUnit 扩展基类并在该类中运行测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2138007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:38:21  来源:igfitidea点击:

JUnit extend base class and have tests in that class being run

javaunit-testingjunitjunit3

提问by John Oxley

I am using JUnit 3 and have a situation where often I have to test that an object is created correctly. My idea was to write a class MyTestBaseas shown below and then extend from that for the situation specific unit tests.

我正在使用 JUnit 3,并且经常需要测试对象是否正确创建。我的想法是编写一个MyTestBase如下所示的类,然后针对特定情况的单元测试对其进行扩展。

However in the example I've given, MyTestsdoes not run the tests in MyTestBase.

但是在我给出的例子中,MyTests没有在MyTestBase.

public class MyTestBase extends TestCase {
   protected String foo;
   public void testFooNotNull() {
     assertNotNull(foo);
   }
   public void testFooValue() {
     assertEquals("bar", foo);
   }
}


public class MyTests extends MyTestBase {
  public void setUp() {
    this.foo = "bar";
  }
  public void testSomethingElse() {
    assertTrue(true);
  }
}

What am I doing wrong?

我究竟做错了什么?

UpdateApologies. Stupid error. The tests in my base class weren't named correctly.

更新道歉。愚蠢的错误。我的基类中的测试命名不正确。

采纳答案by Petar Minchev

You have said "MyTests does not run the tests in MyTestBase.". I tried it and all tests were called including the ones in MyTestBase.

您已经说过“MyTests 不会在 MyTestBase 中运行测试。”。我试过了,所有测试都被调用,包括 MyTestBase 中的测试。

回答by Jon Skeet

Well, you could make MyTestBaseabstract, so that it didn't try to run tests in the base class. A better solution would be to have setUpin the base class and make it call abstract methods (e.g. getFoo()) to initialize the variables it will require later on.

好吧,你可以MyTestBase抽象,这样它就不会尝试在基类中运行测试。更好的解决方案是setUp在基类中使用并使其调用抽象方法(例如getFoo())来初始化稍后需要的变量。

In fact, if you have those abstract methods you may find you don't even need a set-up phase in the first place - you could call the abstract methods where you need the value, instead of using an instance variable. Obviously it will depend on the exact situation, but in many cases this could be a lot cleaner.

事实上,如果你有那些抽象方法,你可能会发现你甚至不需要设置阶段——你可以在需要值的地方调用抽象方法,而不是使用实例变量。显然,这取决于具体情况,但在许多情况下,这可能会更清晰。

回答by Bozho

What you are trying to do is not the most appropriate way to achieve your goal:

您尝试做的不是实现目标的最合适方式:

If you want to have common functionality that makes some checks

如果您想拥有进行一些检查的通用功能

  • define it in a utility class, in staticmethods
  • define it in the superclass and call it from each test method
  • 在实用程序类中,在static方法中定义它
  • 在超类中定义它并从每个测试方法调用它

回答by bertolami

I don't know what exactly you want to do, but usually it is not a very good idea to too much common parts in test, because when the common part fails you will have a large number of tests that fail even tough you probably have just one small bug in your software.

我不知道你到底想做什么,但通常在测试中太多公共部分不是一个很好的主意,因为当公共部分失败时,你会有大量的测试失败,即使你可能有只是您软件中的一个小错误。

I suggest you to use a Factory or a Builder to create the complex object and then test the Factory (or Builder) for creating the object correctly.

我建议您使用 Factory 或 Builder 来创建复杂对象,然后测试 Factory(或 Builder)以正确创建对象。