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

问题描述 投票:5回答:4

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

例如对于一个类:

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

  }

  @Test
  public void test2_shouldNotBeRun() {

  } 
}

我只想运行方法“test1_shouldBeRun”。

java unit-testing junit playframework
4个回答
4
投票

我不认为JUnit本身是否支持它。但是,您有一些替代方案:

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

1
投票

我回来后做了一个post about this

使用JUnit 4和Ant可以实现这一点。诀窍是将方法名称[s]作为属性传递给命令行,如果传递了该属性,则使用不同的任务。

鉴于此测试类:

import org.junit.Test;

public class FooTests {

  @Test
  public void firstTest() {
    System.out.println("test 1");
  }

  @Test
  public void secondTest() {
    System.out.println("test 2");
  }

  @Test
  public void thirdTest() {
    System.out.println("test 3");
  }

}

创建此ant文件:

<project default="test">

    <!-- Tell Ant where to find JUnit -->
    <path id="classpath.test">
        <pathelement location="junit-4.11.jar" />
        <pathelement location="hamcrest-core-1.3.jar" />
        <pathelement location="." /> <!-- or where ever your test class file is -->
    </path>
    <target name="test" description="Runs the tests">
        <!-- Set a new Ant property "use-methods" to true if the "test-methods" Ant property - which we'll pass in from the command line - exists and is not blank.-->
        <condition property="use-methods" else="false">
            <and>
                <isset property="test-methods"/>
                <not>
                    <equals arg1="${test-methods}" arg2=""/>
                </not>
            </and>
        </condition>

        <!-- Sanity check -->
        <echo message="use-methods = ${use-methods}"/>
        <echo message="test-methods = ${test-methods}"/>

        <!-- Run the tests -->
        <junit>
            <classpath refid="classpath.test" />
            <formatter type="brief" usefile="false" />
            <!-- The "if" tells JUnit to only run the test task if the use-methods property is true.-->
            <test if="${use-methods}" name="FooTests" methods="${test-methods}"/>
            <!-- The "unless" tells JUnit to not run the test task if the use-methods property is true.-->
            <test unless="${use-methods}" name="FooTests"/>
        </junit>
    </target>
</project>

例子

运行所有测试。

$ ant
Buildfile: build.xml

test:
     [echo] use-methods = false
     [echo] test-methods = ${test-methods}
    [junit] Testsuite: FooTests
    [junit] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] test 3
    [junit] test 1
    [junit] test 2
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds

通过在命令行上指定“test-methods”Ant属性,仅运行第二个测试。

$ ant -Dtest-methods=secondTest
Buildfile: build.xml

test:
     [echo] use-methods = true
     [echo] test-methods = secondTest
    [junit] Testsuite: FooTests
    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] test 2
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds

仅运行第二次和第三次测试。

$ ant -Dtest-methods=secondTest,thirdTest
Buildfile: build.xml

test:
     [echo] use-methods = true
     [echo] test-methods = secondTest,thirdTest
    [junit] Testsuite: FooTests
    [junit] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] test 3
    [junit] test 2
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds

0
投票

有一个Jute plugin,发布在maven central,插件允许执行JUnit测试方法作为外部分离的Java进程,你可以定义将包含在测试过程中并从测试过程中排除的方法,如果只定义一个测试方法,那么只有测试方法将被执行


0
投票

您可以使用surefire插件[1]:

mvn -Dtest=TestClass#test1_shouldbeRun test

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

© www.soinside.com 2019 - 2024. All rights reserved.