如何在Maven中运行单独的测试?

Dav*_*ave 2 junit surefire maven

我们正在使用Maven 3.0.3并使用JUnit 4.8.1 ...

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
        <scope>test</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

我们有这个测试文件......

./src/test/java/com/myco/clearing/common/xml/TextNodeTest.java
Run Code Online (Sandbox Code Playgroud)

我怎样才能运行这个单独的测试?当我尝试

mvn -Dtest=TextNodeTest test
Run Code Online (Sandbox Code Playgroud)

我得到一个错误,说没有运行测试.如果我将整个包名称指定给我的测试,我会得到相同的错误....

mvn clean -Dtest=com.myco.clearing.common.xml.TextNodeTest test
Run Code Online (Sandbox Code Playgroud)

产生错误信息......

[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
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的万无一失的配置

        <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>
Run Code Online (Sandbox Code Playgroud)

Mat*_*ell 5

在surefire中运行单个测试(方法)的方法是:

mvn test -Dtest=uk.co.farwell.AppTest#testSlow
Run Code Online (Sandbox Code Playgroud)

注意#而不是类名和方法名之间的空格.

然而,正如@Andrew所说,2.12中存在一个错误(SUREFIRE-827:Surefire 2.12无法运行单个测试,从2.11回归),但它在2.11中有效.

以上错误仍然是开放的(截至2012年2月24日),但实际上对我使用2.13-SNAPSHOT.

编辑:现在在2.12.1中标记为已修复.