使用Maven排除特定文件的单元测试

时间:2019-03-13 09:37:16

标签: java maven

我正在研究一个最近获得的项目,并且正在使用mvn test进行单元测试。

因此,我转到不同的模块并使用cmd和运行上面的命令,同时我发现2个文件中有两个测试用例,它们都间歇性地失败(提到了在该方法上方的注释中)。

因此,我正在考虑仅跳过这些测试用例,而现在继续进行。我试图在这里找到它,但我唯一能找到的就是排除特定模块的测试用例。

我想排除A.java的test1()和B.java的test2()

我在下面找到了

# Exclude one test method 
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar

this链接中,但是不确定上述方法的正确性以及在我的情况下如何使用它,因为我以前没有使用过maven

我尝试过的命令:

mvn verify -X -Dtest=!A#test1,!B#test2
mvn test -X -Dtest=!A#test1,!B#test2

运行上述命令后得到的错误跟踪:

[INFO] Selera Utils - Database ............................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.419 s
[INFO] Finished at: 2019-03-13T19:21:16+05:30
[INFO] Final Memory: 17M/205M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project log4j: No tests were executed!  (Set -DfailIfNoTests=false to ignore this
 error.) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project log4j: No tests were exec
uted!  (Set -DfailIfNoTests=false to ignore this error.)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoFailureException: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)
        at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:748)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        ... 19 more
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :log4j

1 个答案:

答案 0 :(得分:2)

在链接中,他们说下面的代码运行多个测试:

mvn test -Dtest=AppTest,Web*

因此可能要排除多个:

mvn verify -Dtest=!A#test1,B#test2

我没有尝试,但似乎合乎逻辑。

---编辑---
您也可以使用that

这样的插件maven-surefire-plugin

---编辑2 ---
我尝试并成功了,首先要做的是验证您使用的Maven-surefire-plugin版本为2.19(添加-X以获得已打印版本的调试信息);那么您的命令将是:

mvn test -Dtest=!A#test1,!B#test2