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

mvn test -Dtest=classname

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


当前回答

在单个测试类中运行一组方法 使用版本2.7.3,您只能在单个测试类中运行n个测试。

注意:junit 4支持。x和TestNG。

必须使用以下语法

mvn -Dtest=TestCircle#mytest test

你也可以使用模式

mvn -Dtest=TestCircle#test* test

从surefire 2.12.1开始,您可以选择多种方法(目前仅限JUnit4X,欢迎补丁)

mvn -Dtest=TestCircle#testOne+testTwo test

查看有关单个测试的链接

其他回答

您可以运行单个测试类,但不能运行测试类中的单个方法。使用类的简单名称,而不是类的完全限定名称。如果你在"org。sontype。test "中有一个测试。MyTest”,这是你唯一想要运行的测试,你的命令行看起来是这样的:

mvn test -Dtest=MyTest

要在Maven中运行单个测试方法,您需要提供如下命令:

mvn test -Dtest=TestCircle#xyz test

TestCircle是测试类名,xyz是测试方法。

通配符也可以;包括方法名和类名。

如果在多模块项目中进行测试,则使用-pl <module-name>指定测试所在的模块。

对于集成测试,请使用it.test=…选项代替test=…

mvn -pl <module-name> -Dit.test=TestCircle#xyz integration-test

我尝试了这个线程中提供的几个解决方案,但它们不适用于依赖于不同的模块。在这种情况下,我必须使用额外的参数从根模块运行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。

您需要指定JUnit测试类及其要执行的方法。

mvn test -Dtest=com.mycompany.AppTest#testMethod

https://metamug.com/article/java/build-run-java-maven-project-command-line.html#running-unit-tests

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

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*'模式的所有测试方法。