gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:03 24 4
gpt4 key购买 nike

我正在做一个项目,这是我最近得到的,同时使用 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

最佳答案

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

mvn test -Dtest=AppTest,Web*

所以可能是要排除多个:

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

我没试过,但看起来合乎逻辑。

---编辑---
您还可以使用插件 maven-surefire-plugin,如 that

---编辑 2 ---
我尝试并成功,首先要验证您使用版本> 2.19 的 maven-surefire-plugin(添加 -X 以调试版本已打印);那么你的命令将是:

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

关于java - 使用 Maven 排除特定文件的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55138530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com