2017-08-29 67 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

没有ü在最后提到的测试? –