我们正在使用 Maven 3.0.3 并且正在使用 JUnit 4.8.1 ...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
我们有这个测试文件...
./src/test/java/com/myco/clearing/common/xml/TextNodeTest.java
如何运行这个单独的测试?当我尝试时
mvn -Dtest=TextNodeTest test
我收到一条错误消息,提示未运行任何测试。如果我为我的测试指定整个包名,我会得到同样的错误。 ...
mvn clean -Dtest=com.myco.clearing.common.xml.TextNodeTest test
产生错误信息...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project myco-productplus-web: No tests were executed!
(Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
这是我正在使用的万无一失的配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skip>false</skip>
<additionalClasspathElements>
<additionalClasspathElement>${project.build.sourceDirectory}</additionalClasspathElement>
<additionalClasspathElement>${project.build.testSourceDirectory}</additionalClasspathElement>
</additionalClasspathElements>
<useManifestOnlyJar>false</useManifestOnlyJar>
<forkMode>always</forkMode>
<systemProperties>
<property>
<name>gwt.args</name>
<value>-out "${webappDirectory}"</value>
</property>
</systemProperties>
<systemPropertyVariables>
<tomcat.port>${tomcat.servlet.port}</tomcat.port>
<project.artifactId>${project.artifactId}</project.artifactId>
</systemPropertyVariables>
</configuration>
</plugin>
最佳答案
在surefire中运行单个测试(方法)的方法是:
mvn test -Dtest=uk.co.farwell.AppTest#testSlow
注意#而不是类名和方法名之间的空格。
但是,正如@Andrew 所说,2.12 ( SUREFIRE-827: Surefire 2.12 cannot run a single test, regression from 2.11 ) 中有一个错误,但它在 2.11 中有效。
上述错误仍然存在(截至 24.02.2012),但实际上对我使用 2.13-SNAPSHOT 有效。
编辑:现在在 2.12.1 中将其标记为已修复。
关于maven - 如何在 Maven 中运行单个测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9331421/