簡體   English   中英

僅對單個方法運行 junit 測試

[英]run a junit test for a single method only

你可以只為一個方法而不是整個類運行測試嗎? 理想情況下從命令行。

例如對於一個類:

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

  }

  @Test
  public void test2_shouldNotBeRun() {

  } 
}

我只想運行方法“test1_shouldBeRun”。

我不認為這是否由 JUnit 本身支持。 但是,您有一些選擇:

  1. 將所需的測試放在不同的類中,以便您可以單獨運行它們
  2. 也許使用@Ignore注釋可以幫助您。
  3. 如果您使用的是某些 IDE,它可能支持這種方法。 例如在 Eclipse 中,在Package Explorer 中展開測試類,選擇要運行的方法,然后單擊Run As -> JUnit test
  4. 這是關於如何創建自定義TestSuit和 Ant 任務的非常廣泛的教程,可以為您完成此任務。
  5. 更新在這個線程中,人們說“舊的 junit.textui.TestRunner 實用程序提供了一種在命令行上運行單個方法的方法,通過 runSingleMethod(),但它不支持 JUnit4 注釋測試類。

當您使用 surefire 插件 [1] 時是可能的:

mvn -Dtest=TestClass#test1_shouldbeRun test

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

不久前我發了一篇關於這個帖子

這可以使用 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");
  }

}

創建這個螞蟻文件:

<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

Jute 插件,發布在 maven central 中,該插件允許將 JUnit 測試方法作為外部分離的 Java 進程執行,您可以定義將包含和排除在測試進程中的方法,如果只定義一個測試方法作為包含則只有測試方法將被執行

不知道這是否適用於您的機器/庫版本,但是在我不想測試的方法上使用private modifier對我有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM