我知道您可以使用以下方法在某个类中运行所有测试:

mvn test -Dtest=classname

但我想运行一个单独的方法和-Dtest=classname。Methodname似乎不起作用。


当前回答

这个命令有效!! 请注意,“-DTest”以大写字母“T”开头。

其他回答

surefire 2.12有一个问题。 这是我把maven-surefire-plugin从2.12修改到2.11的结果:

mvn test -Dtest=DesignRulesTest 结果: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project pmd: No tests were executed! mvn test -Dtest=DesignRulesTest 结果: [INFO]——maven-surefire-plugin:2.11:test (default-test) @ pmd—— ... 运行net.sourceforge.pmd.lang.java.rule.design.DesignRulesTest 测试运行:5,失败:0,错误:0,跳过:4,时间流逝:4.009秒

您可以使用以下语法运行特定的测试类和方法:

完整包:mvn test -Dtest="com.oracle.tests.**" mvn test -Dtest=CLASS_NAME1 mvn test -Dtest=CLASS_NAME1#METHOD_NAME1 来自多个类的多个方法:mvn test -Dtest=CLASS_NAME1#METHOD_NAME1,CLASS_NAME2#METHOD_NAME2

从一个测试类运行一个测试方法。

mvn test -Dtest=Test1#方法名


其他相关的用例

mvn test //运行所有单元测试类 mvn test -Dtest=Test1 //运行单个测试类 mvn test -Dtest=Test1,Test2 //运行多个测试类 mvn test -Dtest=Test1#testFoo* //运行测试类中匹配模式'testFoo*'的所有测试方法。 mvn test -Dtest=Test1#testFoo*+testBar* //运行一个测试类中匹配'testFoo*'和'testBar*'模式的所有测试方法。

我尝试了这个线程中提供的几个解决方案,但它们不适用于依赖于不同的模块。在这种情况下,我必须使用额外的参数从根模块运行mvn: -am(——same -make),它告诉maven构建您的测试模块所依赖的模块,并且-DfailIfNoTests=false,否则会出现“没有测试被执行!”错误。

mvn test -pl B -Dtest=MyTestClass#myTest -am -DfailIfNoTests=false

root目录下的Pom.xml部分:

<modules>
    <module>A</module>
    <module>B</module>
<modules>

B取决于A。

tobrien提到的test参数允许您在方法名称前使用#来指定方法。这应该适用于JUnit和TestNG。我从来没有尝试过,只是在Surefire插件页面上阅读:

Specify this parameter to run individual tests by file name, overriding the includes/excludes parameters. Each pattern you specify here will be used to create an include pattern formatted like **/${test}.java, so you can just type "-Dtest=MyTest" to run a single test called "foo/MyTest.java". This parameter overrides the includes/excludes parameters, and the TestNG suiteXmlFiles parameter. since 2.7.3 You can execute a limited number of method in the test with adding #myMethod or #my*ethod. Si type "-Dtest=MyTest#myMethod" supported for junit 4.x and testNg