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

mvn test -Dtest=classname

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


当前回答

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

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

其他回答

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

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

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

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

要在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戒毒

然后您可以使用运行特定的文件和函数

mvn test -Dtest=testClassName#testCaseName

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

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