Jade Dungeon

使用Maven 进行测试

测试的插件是maven-surefire-plugin。默认会执行:

  • **/Test*.java
  • **/*Test.java
  • **/*TestCase.java

Debug

Forked Tests

以参数maven.surefire.debug打开调试服务器,默认是Forked模式:

mvn -Dmaven.surefire.debug test

这样程序会等待远程调试器来连接5005端口。比如可以用Eclipse的菜单: "Run" > "Open Debug Dialog..." > "Remote Java Application" 来连接。

要指定更加详细的参数:

mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" test

Non-forked Tests

强制不用fork调试:

mvn -DforkCount=0 test

Then all you need to do is debug Maven itself. Since Maven 2.0.8, Maven ships with a mvnDebug shell script that you can use to launch Maven with convenient debugging options:

mvnDebug -DforkCount=0 test

Then you can attach Eclipse to Maven itself, which may be easier/more convenient than debugging the forked executable.

指定跳过测试

通过参数:

mvn package -DskipTests

或:

mvn install -Dmaven.test.skip=true

或通过配置:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifacctId>maven-surefire-plugin</artifacctId>
	<version>2.5</version>
	<configuration>
		<skiipTests>true</skiipTests>
	</configuration>
</plugin>

还可能指定跳过编译测试用例:

mvn package -Dmaven.test.skip=true

通过配置的方式:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifacctId>maven-compile-plugin</artifacctId>
	<version>2.1</version>
	<configuration>
		<skiipTests>true</skiipTests>
	</configuration>
</plugin>

指定要运行的测试用例

命令行的方式:

mvn test -Dtest=ImageTest

可以匹配:

mvn test -Dtest=Image*Test

可以指定多个:

mvn test -Dtest=ImageTest,FileTest

或组合起来:

mvn test -Dtest=Image*Test,FileTest

如果找不到匹配的会报错,想不报错加上:

mvn test -DfailIfNoTests=false -Dtest=Image*Test,FileTest

包含和排除测试用例

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifacctId>maven-surefire-plugin</artifacctId>
	<version>2.5</version>
	<configuration>
		<includes>
			<include>**/*Tests.java</include>
		</includes>
		<excludes>
			<exclude>**/*TempTests.java</exclude>
		</excludes>
	</configuration>
</plugin>

测试报告

测试覆盖率

cobertura-maven-plugin

<project>
  ...
  <build>
    <!-- To define the plugin version in your parent POM -->
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>cobertura-maven-plugin</artifactId>
          <version>2.6</version>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
    <!-- To use the plugin goals in your POM or parent POM -->
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.6</version>
      </plugin>
      ...
    </plugins>
  </build>
  ...
  <!-- To use the report goals in your POM or parent POM -->
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.6</version>
      </plugin>
      ...
    </plugins>
  </reporting>
  ...
</project>

官方提供的 maven 插件,大致有以下 goal :

Goal Description
cobertura:check Check the Last Instrumentation Results.
cobertura:clean Clean up rogue files that cobertura maven plugin is tracking.
cobertura:dump-datafile Cobertura Datafile Dump Mojo
cobertura:instrument Instrument the compiled classes.
cobertura:cobertura Instruments, Tests, and Generates a Cobertura Report.

基本的配置如下:

<reporting>
      <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
            <configuration>
               <formats>
                  <format>html</format>
                  <format>xml</format>
               </formats>
            </configuration>
         </plugin>
      </plugins>
   </reporting>

通常情况下我们只需要进行如下配置就足可以应付当前的测试需求了:

<reporting>
	<outputDirectory>target/site</outputDirectory>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>cobertura-maven-plugin</artifactId>
		</plugin>
	</plugins>
</reporting>

当然,你也可以采用 Cobertura 的一些附加配置来限制那些不用进行覆盖率测试的类,具体做法如下:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>cobertura-maven-plugin</artifactId>
	<configuration>
		<instrumentation>
			<excludes>
				<exclude>**/*Test.class</exclude>
			</excludes>
		</instrumentation>
	</configuration>
	<executions>
		<execution>
			<goals>
				<goal>clean</goal>
			</goals>
		</execution>
	</executions>
</plugin>

打包测试代码

默认测试代码是不会放进jar包里的。如果想用重用测试用例(虽然这种情况不多), 把测试用例打成测试jar包:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.2</version>
	<executions>
		<execution>
			<goals>
				<goal>test-jar</goal>
			</goals>
		</execution>
	</executions>
</plugin>

其他工程要应用这个包,要指定type为test-jar,scope为test:

<dependency>
	<groupId>com.aa.</groupId>
	<artifactId>account-captcha</artifactId>
	<version>1.0.0</version>
	<type>test-jar</type>
	<scope>test</scope>
</dependency>