仅针对单个方法运行junit测试

for*_*ste 5 java junit unit-testing playframework

你可以只为一个方法运行测试,而不是整个类吗?理想情况下来自命令行.

例如,对于一个班级:

public class TestClass extends Unittest {
  @Test
  public void test1_shouldbeRun() {

  }

  @Test
  public void test2_shouldNotBeRun() {

  } 
}
Run Code Online (Sandbox Code Playgroud)

我只想运行方法"test1_shouldBeRun".

fab*_*abb 7

当您使用surefire插件[1]时,这是可能的:

mvn -Dtest=TestClass#test1_shouldbeRun test
Run Code Online (Sandbox Code Playgroud)

[1] http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html


rle*_*ndi 5

我不认为这是否由 JUnit 本身支持。但是,您有一些选择:

  1. 将所需的测试放在不同的类中,以便您可以单独运行它们
  2. 也许使用@Ignore注释对你有帮助。
  3. 如果您使用的是某些 IDE,它可能支持这种方法。例如在 Eclipse 中,在Package Explorer 中展开测试类,选择要运行的方法,然后单击Run As -> JUnit test
  4. 这是关于如何创建自定义TestSuit任务和 Ant 任务的非常广泛的教程,可以为您完成此任务。
  5. 更新 在这个线程中,人们说“旧的 junit.textui.TestRunner 实用程序提供了一种在命令行上运行单个方法的方法,通过 runSingleMethod(),但它不支持 JUnit4 注释测试类。