2012-03-11 57 views
2

您是否可以運行單個測試方法而不是整個班級?理想情況下從命令行。僅對單一方法運行測試

例如對於一個類:

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

    } 

    @Test 
    public void test2_shouldNotBeRun() { 

    } 
} 

我只想運行「test1_shouldBeRun」方法。

回答

2

我不認爲如果這是由JUnit本機支持。但是,你有一些選擇:

  1. 把需要測試在不同的類,所以你也許可以使用@Ignore註釋有助於你分開運行
  2. 如果您使用的是一些IDE,它可能支持這種方法。例如在Eclipse中,展開包瀏覽器中的測試類,選擇要運行的方法,然後單擊運行方式 - > JUnit測試
  3. Here's a quite extensive tutorial關於如何創建一個自定義TestSuit和一個Ant任務,可以爲你做這個。
  4. UPDATEIn this thread的傢伙說,「老的junit.textui.TestRunner實用程序提供一種方式來運行的命令行了一個方法,通過runSingleMethod(),但它不支持JUnit4註解測試
+0

謝謝,即使eclipse選項似乎運行的所有類的測試它仍然幫助我。 – forste 2012-03-11 13:22:31

+0

然而,應用程序日誌不會被記錄到Eclipse控制檯,你知道如何指揮它呢? – forste 2012-03-11 13:23:35

+0

顯然日誌級別以某種方式設置爲INFO,因此我遇到類似http://stackoverflow.com/questions/3501355/log4j-output-not-displayed-in-eclipse-console – forste 2012-03-11 13:34:49

0

我稍微做了一個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,發表於行家中部,插件允許執行JUnit測試方法作爲外部分離Java進程並且可以定義將被包括在內,從試驗過程中排除和方法如果僅定義一個測試方法包括那麼只有測試方法將被執行

相關問題