2017-08-29 47 views
0

我嘗試使用maven或Junit從命令行運行單個測試方法。我的項目使用Maven構建在Junit Framework中。使用Maven和Jnunit從命令行運行單一方法

我並行執行並行運行方法。當我嘗試從命令行運行它時,它仍然並行運行所有testmenthds。

mvn -Dtest=<Classname>#<testmethodname> test 

有沒有人會幫助,如何達到從命令行運行單個測試方法或特定的測試方法。

版本: 的JUnit - 4.9 Maven的編譯器插件 - 2.5.1 Maven的萬無一失,插件 - 2.8

這是日誌做參考

[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building <ProjectName> 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 1 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory 
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-surefire-plugin:2.8:test (default-test) @ --- 
[INFO] Surefire report directory: C:\Automation\......\target\surefire-reports 

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Concurrency config is parallel='methods', perCoreThreadCount=false, threadCount=6, useUnlimitedThreads=false 
Destroying 1 processes 
Destroying process.. 
Destroyed 1 processes 
Terminate batch job (Y/N)? 
+0

你使用哪種JUnit版本? –

+0

4.9 luckyvasul

回答

1

的excuting一個萬無一失參數單個測試是test而不是Test因此您應該調用以下Maven命令:

mvn -Dtest=<ClassName>#<TestMethodName> test 

例如,給定...

  • 測試FooTest與測試方法:foo()
  • 測試BarTest與測試方法:bar()

...你可以調用以下:

  • 運行FooTest.foo()

    mvn -Dtest=FooTest#foo test 
    
  • 運行FooTest.foo()和BarTest.bar()

    mvn -Dtest=FooTest#foo,FooTest#bar test 
    
+0

試圖 MVN測試-Dtest = .....測試#policy_getcustomersummary MVN神火:測試-Dtest = .....測試#policy_getcustomersummary 仍然是其執行從給定的類 – luckyvasul

+1

@luckyvasul所有testmethods你能否更新這個問題來說明你正在使用的Maven Surefire插件的版本。'-Dtest'參數用Maven-surefire-plugin的JUnit4和> 2.12.1驗證。有關版本兼容性的更多詳細信息,請參閱[文檔](https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html)。 – glytching

+0

它工作..更新到Surefire插件後2.19 ...不是我只能執行1個特定的測試或一組測試方法...謝謝你們。 – luckyvasul

1

拼寫錯誤發現哥們,正確的是-Dtest,而不是-Dtest

mvn -Dtest=<Classname>#<testmethodname> test 

此外,由於您已經在並行運行多個測試,因此您還可以使用下面提到的模式執行類似測試方法秒。

例子:

mvn -Dtest=Classname#testMethod1+testMethod2+testMethod3 test 

享受... ...;)

+0

對不起,這是一個錯字.... MVN測試-Dtest = luckyvasul

+0

MVN測試-Dtest = ..仍在運行的所有測試方法 – luckyvasul

+0

沒有ü在最後提到的測試? –