maven - 如何在 Maven 中运行单个测试?

标签 maven junit surefire

我们正在使用 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/

相关文章:

java - 从 Maven 原型(prototype)初始化 Mercurial 存储库

java - 所有 Maven 项目模块具有相同的时间戳值

junit - 使用 TestNG 进行 Spring 依赖注入(inject)

java - 如何在另一个测试类中使用现有的 JUnit 测试,使其通过@before?

maven-2 - 如何使用 Maven Surefire 插件与不同的组进行测试和集成测试?

unit-testing - 包括新的测试目录 maven surefire 插件

java - 无法执行目标 org.apache.maven.plugins :maven-surefire-plugin:3. 0.0-M3:test

java - Maven 打包失败 Lucene,SPI 不存在异常

java - 同一项目,同一分支,两台机器: unit tests producing inconsistent results

java - Jacoco 的 Maven 多模块项目报告