java 如何使用 Maven 运行多个测试类或测试方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27653510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:10:59  来源:igfitidea点击:

How to run multiple test classes or test methods using Maven?

javaunit-testingmaven

提问by TDHM

In order to run all Maven tests, we can use:

为了运行所有 Maven 测试,我们可以使用:

mvn clean test

If we want to run specific test class, we can use:

如果我们想运行特定的测试类,我们可以使用:

mvn clean test -Dtest=className

If we want to run specific method from specific test class, we can use:

如果我们想从特定的测试类运行特定的方法,我们可以使用:

mvn clean test -Dtest=className#methodName

But I want to run:

但我想跑:

  1. multiple test classes(not all that belong to src\test\java)
  2. multiple test methods from specific test class(not all test methods of specific test class that belong to src\test\java)
  1. 多个测试类(并非所有属于src\test\java
  2. 来自特定测试类的多个测试方法(并非属于特定测试类的所有测试方法src\test\java

Are there Maven commands using which I can achieve above two?

是否有 Maven 命令使用我可以实现以上两个?

回答by Subir Kumar Sao

If using surefire plugin then you may use the below options.

如果使用surefire插件,那么您可以使用以下选项。

For multiple classes you can use,

对于您可以使用的多个类,

mvn -Dtest=TestSquare,TestCi*le test

For multiple methods in same class you can use,

对于您可以使用的同一类中的多个方法,

mvn -Dtest=TestCircle#testOne+testTwo test

Refer docs

参考文档

回答by jorrocks

You can use wildcards - note that you have to quote the test argument so the shell does not expand the wildcard.

您可以使用通配符 - 请注意,您必须引用 test 参数,以便 shell 不会扩展通配符。

mvn -Dtest="TestSquare,TestCi*le" test

(using maven-surefire-plugin:2.17)

(使用 maven-surefire-plugin:2.17)

回答by Krzysztof Walczewski

If You want to launch all test Clases from subdirectory, eg: /doc/ You can use command:

如果您想从子目录启动所有测试类,例如:/doc/ 您可以使用命令:

mvn -Dtest=*/doc/* test

回答by Sidharth Taneja

You can add multiple classes in TestNG with their groups, like

您可以在 TestNG 中添加多个类及其组,例如

<groups>
  <run>
    <include name = "checkintest" />
    <include name = "videoSpider" />
    <include name = "xmlTCUploader" />
    <include name = "PALLogin" />
  </run>
</groups>
<classes>
  <class name="SeleniumUC"/>
  <class name="PALTestCasesSuite"/>
</classes>

After this, you can use these group with Maven like -

在此之后,您可以将这些组与 Maven 一起使用,例如 -

mvn -Dgroups=PALLogin test