我知道您可以使用以下方法在某个类中运行所有测试:
mvn test -Dtest=classname
但我想运行一个单独的方法和-Dtest=classname。Methodname似乎不起作用。
我知道您可以使用以下方法在某个类中运行所有测试:
mvn test -Dtest=classname
但我想运行一个单独的方法和-Dtest=classname。Methodname似乎不起作用。
当前回答
要在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
其他回答
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
首先,你需要清理你的专业项目
mvn戒毒
然后您可以使用运行特定的文件和函数
mvn test -Dtest=testClassName#testCaseName
这个命令有效!! 请注意,“-DTest”以大写字母“T”开头。
您可以运行单个测试类,但不能运行测试类中的单个方法。使用类的简单名称,而不是类的完全限定名称。如果你在"org。sontype。test "中有一个测试。MyTest”,这是你唯一想要运行的测试,你的命令行看起来是这样的:
mvn test -Dtest=MyTest
在surefire插件2.22.1版本(可能更早),你可以在使用testng.xml时使用testnames属性运行单个测试
给定下面的test .xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="all-tests">
<classes>
<class name="server.Atest"/>
<class name="server.Btest"/>
<class name="server.Ctest"/>
</classes>
</test>
<test name="run-A-test">
<classes>
<class name="server.Atest"/>
</classes>
</test>
<test name="run-B-test">
<classes>
<class name="server.Btest"/>
</classes>
</test>
<test name="run-C-test">
<classes>
<class name="server.Ctest"/>
</classes>
</test>
</suite>
使用pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
[...]
<properties>
<selectedTests>all-tests</selectedTests>
</properties>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<suiteXmlFiles>
<file>src/test/resources/testng.xml</file>
</suiteXmlFiles>
<properties>
<property>
<name>testnames</name>
<value>${selectedTests}</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
[...]
</project>
从命令行
mvn clean test -DselectedTests=run-B-test
进一步阅读- Maven surefire插件使用testng