1. 程式人生 > >maven執行junit用例並生成報告

maven執行junit用例並生成報告

測試類如下:

package com.mmnn.test.testcase;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class Demo1Test 
{
	@Test
	public void TestMth1() {
		assertTrue("msg : mth1 test test test test", true);
	}
	
	@Test
	public void TestMth2() {
		assertTrue("msg : mth2 test test test test", true);
	}
}

package com.mmnn.test.testcase;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class Demo2Test 
{
	@Test
	public void TestMth3() {
		assertTrue("msg : mth3 test test test test", true);
	}
	
	@Test
	public void TestMth4() {
		assertTrue("msg : mth4 test test test test", false);
	}
}
export JAVA_HOME=/opt/tools/jdk1.7.0_17

cd /path/to/testproject/whichhasPomFile

/opt/tools/apache-maven-3.1.1/bin/mvn -s /opt/tools/apache-maven-3.1.1/conf/settings.xml clean -Dtest=TestCal test

一、使用maven-surefire-plugin外掛自帶report功能

注意:

1、單獨執行mvn test,預設執行的就是maven-surefire-plugin

2、配置了maven-surefire-plugin後,執行mvn test,結果和1一致,生成xml、txt檔案

3、執行mvn test surefire-report:report 即是:執行mvn test的基礎上,根據生成的xml、txt檔案,再生成預設格式的report

mvn -s C:\maven\conf\settings.xml clean -Dtest=TestClass surefire-report:report test
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mmnn.test</groupId>
	<artifactId>mvnjunit1</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.8</source>		<!-- //////////// -->
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<testFailureIgnore>true</testFailureIgnore>	<!-- //////////// -->
					<includes>
						<include>**/*Test.java</include>	<!-- //////////// -->
					</includes>
					<excludes>
						<!-- -->
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.5</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>

執行 mvn test surefire-report:report 即可:



二、使用maven-antrun-extended-plugin

使用maven-surefire-plugin生成的報告太醜,可以通過maven-antrun系列外掛生成更友好的報告:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mmnn.test</groupId>
	<artifactId>mvnjunit2</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>

			<!-- mvn test生成xml txt測試報告(命令列不帶surefire-report:report時) -->
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<testFailureIgnore>true</testFailureIgnore>	<!-- //////////// -->
					<includes>
						<include>**/*Test.java</include>	<!-- //////////// -->
					</includes>
					<excludes>
						<!-- -->
					</excludes>
				</configuration>
			</plugin>

			<!-- 用mvn ant生成格式更友好的report -->
			<plugin>
				<groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
				<artifactId>maven-antrun-extended-plugin</artifactId>	<!-- //////////// -->
				<executions>
					<execution>
						<id>test-reports</id>
						<phase>test</phase>	<!-- //////////// -->
						<configuration>
							<tasks>
								<junitreport todir="${basedir}/target/surefire-reports">
									<fileset dir="${basedir}/target/surefire-reports">
										<include name="**/*.xml" />
									</fileset>
									<report format="frames" todir="${basedir}/target/surefire-reports" />	<!-- //////////// -->
								</junitreport>
							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>org.apache.ant</groupId>
						<artifactId>ant-junit</artifactId>
						<version>1.8.0</version>
					</dependency>
					<dependency>
						<groupId>org.apache.ant</groupId>
						<artifactId>ant-trax</artifactId>
						<version>1.8.0</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>


	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.5</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>
執行mvn test: