四、使用maven测试(学习笔记)

1. maven-surefire-plugin 简介

default生命周期test阶段内置绑定maven-surefire-plugin的test目标。

maven-surefire-plugin的test目标会自动执行测试源代码路径,测试类命名模式:

**/Test*.java:   子目录下所有命名以Test开头的Java类。

**/*Test.java:  ....

**/*TestCase.java:  ....

 

2. 跳过测试

2.1 命令行模式

$mvnpackage -DskipTests

 

2.2 POM文件中配置

<build>

    <plugins>

       <plugin>

           <groupId>org.apache.maven.plugins</groupId>

           <artifactId>maven-surefire-plugin</artifactId>

           <version>2.5</version>

           <configuration>

              <skipTests>true</skipTests>

           </configuration>

       </plugin>

    </plugins>

  </build>

 

3 跳过测试和测试代码编译

3.1 命令行模式

$mvnpackage -Dmaven.test.skip=tue

 

3.2 POM文件中配置

<build>

    <plugins>

        <plugin>

           <groupId>org.apache.maven.plugins</groupId>

               <artifactId>maven-compiler-plugin</artifactId>

               <version>2.1</version>

               <configuration>

                  <skip>true</skip>

               </configuration>

        </plugin>

       <plugin>

           <groupId>org.apache.maven.plugins</groupId>

           <artifactId>maven-surefire-plugin</artifactId>

           <version>2.5</version>

           <configuration>

              <skip>true</skip>

           </configuration>

       </plugin>

    </plugins>

  </build>

 

3.  动态指定要运行的测试用例

maven-surefire-plugin:test的参数test指定

$mvntest -Dtest=RandonGeneratorTest

$mvntest -Dtest=Randon*Test

$mvntest -Dtest=RandonGeneratorTest, RandonGeneratorTestCase

$mvntest -Dtest=Randon*Test, RandonGeneratorTestCase

 

test参数的值必须匹配一个或者多个测试类。

$mvntest -Dtest 会显示构建失败

使用 -DfailIfNoTests=false 即时没指定测试类也不要报错(另外一种跳过测试方式)

$mvntest -Dtest -DfailIfNoTests=false

 

 

5. 生成测试报告

cobertura: cobertura

绑定到test

<plugin>

              <groupId>org.codehaus.mojo</groupId>

              <artifactId>cobertura-maven-plugin</artifactId>

              <version>2.5</version>

              <configuration>

                  <formats>

                     <format>html</format>

                     <format>xml</format>

                  </formats>

              </configuration>

              <executions>

                  <execution>

                     <id>cobertura-report</id>

                     <phase>test</phase>

                     <goals>

                         <goal>cobertura</goal>

                     </goals>

                  </execution>

              </executions>

           </plugin>

 

4. 包含与排除测试用例

自动运行以Tests结尾的测试类:

<build>

    <plugins>

       <plugin>

           <groupId>org.apache.maven.plugins</groupId>

           <artifactId>maven-surefire-plugin</artifactId>

           <version>2.5</version>

           <configuration>

               <includes>

                  <include>**/*Tests.java</include>

               </includes>

           </configuration>

       </plugin>

    </plugins>

  </build>

 

排除测试类:

<build>

    <plugins>

       <plugin>

           <groupId>org.apache.maven.plugins</groupId>

           <artifactId>maven-surefire-plugin</artifactId>

           <version>2.5</version>

           <configuration>

              <excludes>

                  <exclude>**/*ServiceTest.java</exclude>

                  <exclude>**/TmpDapTest.java</exclude>

              </excludes>

           </configuration>

       </plugin>

    </plugins>

  </build>

 


学习资料:《MAVEN实战》  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值