如何指定JUnit测试依赖项?

Ego*_*gen 53 java junit unit-testing

我们的工具包有超过15000个JUnit测试,如果其他测试失败,许多测试都会失败.例如,如果方法X.foo()使用Y.bar()和YTest.testBar()失败的功能,那么XTest.testFoo()也将失败.显然,由于X.foo()特有的问题,XTest.testFoo()也会失败.

虽然这很好但我仍然希望运行两个测试,如果可以使用指向YTest.testBar()的XTest.testFoo()来注释测试依赖项,那将是很好的.这样,人们可以立即看到X.foo()使用的功能也失败了,什么不能.

JUnit或其他地方是否有这样的注释?就像是:

public XTest {

  @Test
  @DependsOn(method=org.example.tests.YTest#testBar)
  public void testFoo() {
     // Assert.something();
  }

}
Run Code Online (Sandbox Code Playgroud)

Esk*_*ola 24

JExampleTestNG有类似的东西.

我不知道它有多有用,但是如果你尝试一下,请回来告诉我们它是否有用.

  • 是的,这似乎是一个很好的开始:http://chem-bla-ics.blogspot.com/2010/08/specifying-unit-test-dependencies-with.html (3认同)
  • 当 ClassA.methodA() 调用 ClassB.methodB() 并且两者都需要大量时间时,它很有用。在这种情况下,您可以指定 testMethodA() 依赖于 testMethodB(),即如果 testMethodB() 失败,testMethodA() 也会失败**而不执行**。 (2认同)

Jos*_*nis 17

JUnit对此有所贡献:https://github.com/junit-team/junit.contrib/tree/master/assumes

  • 好好!我希望很快就能合并:) (3认同)

小智 5

org.junit.FixMethodOrder

@FixMethodOrder(MethodSorters.NAME_ASCENDING)这是你的单元测试类的顶部.

您可以将方法命名为public void step1_methodName等