mvn执行制定的测试类或方法

Maven执行指定的测试类或方法,需要使用test属性。

运行测试类com.mytest.TestCircle

mvn -Dtest=com.mytest.TestCircle test
运行所有以Test开头已cle结尾的测试类
mvn -Dtest=com.mytest.Test*cle test
使用逗号分割要测试的类
mvn -Dtest=com.mytest.TestSquare,com.mytest.Test*cle test
使用#指定测试方法,使用*通配测试方法
mvn -Dtest=com.mytest.TestSquare#testABC,com.mytest.Test*cle test
mvn -Dtest=com.mytest.TestSquare#testA*,com.mytest.TestCircle test
使用+号指定一个类中的多个测试方法
mvn -Dtest=com.mytest.TestSquare#testABC+testEFG,com.mytest.TestCircle test

注意:写成如下方式是不行的,必须用加号来分割同一个类中测试方法。

mvn -Dtest=com.mytest.TestSquare#testABC,com.mytest.TestSquare#testABCtestEFG test  (JUnit测试报表中只有一个测试结果,另外一个被覆盖了)

在实际项目中遇到一个问题,就是使用下面的参数的时候

-Dtest=com.mytest.TestSquare#testABC+testEFG,com.mytest.TestCircle
找不到测试对象,把dependence surefire-junit47去掉后,就可以识别了。下面是Maven build配置。

        <plugins>
            <!-- unit tests: read more @ https://maven.apache.org/surefire/maven-failsafe-plugin/ -->
            <!-- default test case pattern: **/Test*.Java, **/*Test.java, **/*TestCase.java -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                     <argLine>-Dfile.encoding=UTF-8</argLine>
                    <skipTests>${skip.tests}</skipTests>
                    <!-- systemPropertyVariables>
                        <propertyName>username</propertyName>
                        <buildDirectory>${username}</buildDirectory>
                        <propertyName>env</propertyName>
                        <buildDirectory>${env}</buildDirectory>
                        <propertyName>catalog</propertyName>
                        <buildDirectory>${catalog}</buildDirectory>
                      </systemPropertyVariables-->
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.16</version>
                    </dependency>
                </dependencies>

            </plugin>
            <!-- integration tests: read more @ http://maven.apache.org/surefire/maven-failsafe-plugin/ -->
            <!-- default test case pattern: **/IT*.java, **/*IT.java, **/*ITCase.java -->
        </plugins>


参考:http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在运行Maven构建的过程中,compile和test是两个常见的阶段。compile阶段负责将Java源代码编译成字节码文件,而test阶段则负责执行测试并生成测试报告。 如果在Maven构建过程中没有编译测试,可能存在以下几种情况: 1. 没有编写测试:该项目可能没有编写任何单元测试,因此在执行test阶段时,Maven会跳过这个阶段,不会执行任何测试操作。推荐在项目中编写适当的单元测试,以确保代码的质量和可靠性。 2. 打开了skipTests选项:在执行mvn命令时,如果加上了-DskipTests参数,Maven会跳过test阶段,这意味着测试不会被执行。可以尝试去除该参数,让Maven正常执行测试。 3. 测试未按照约定放置:Maven默认会在src/test/java目录下查找测试,并将其编译并执行。如果测试没有按照约定的路径进行放置,可能导致Maven无法正确找到测试。在这种情况下,可以检查测试的存放位置是否正确,并确保项目的源码和测试代码正确分离。 4. 测试存在编译错误:如果测试中存在编译错误,Maven在编译阶段时会停止构建过程,并将错误信息输出到控制台。必须修复测试中的错误才能继续运行测试。 总之,当Maven没有编译测试时,需要检查是否存在测试、是否开启了跳过测试选项、测试是否按照约定放置以及测试是否存在编译错误等因素,才能确定解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值