`

Maven 测试

 
阅读更多

1.maven-surefire-plugin插件

    默认情况下,插件的test目标会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式为:

    **/Test*.java

    **/*Test.java

    **/*TestCase.java

  还可以自定义测试类的模式,还支持更高级的TestNG测试集合xml文件。

  执行测试,需要在POM文件中引入JUNIT依赖。

 

2.跳过测试

在运行时加入skipTests,如mvn clean install -DskipTests

下面命令还可以跳过测试代码的编译mvn clean install -Dmaven.test.skip=true

可以在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>

 

    在添加了上面代码后,Maven编译出现了Java编译版本为1.4的情况,在account的POM文件中添加编译版本为1.5则正常运行,如果添加为1.6,则会出错“编码GBK的不要映射字符”的错误,还要添加编码为UTF-8。account下的POM文件修改后的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<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.iteye.xujava</groupId>
	<artifactId>account</artifactId>
	<version>1.0.0</version>
	<packaging>pom</packaging>
	<name>Account</name>

	<modules>
		<module>account-email</module>
		<module>account-persist</module>
		<module>account-captcha</module>
	</modules>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<springframework.version>2.5.6</springframework.version>
		<junit.version>4.9</junit.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.6</source>
						<target>1.6</target>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-resources-plugin</artifactId>
					<configuration>
						<encoding>UTF-8</encoding>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

 

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

 

 mvn test -Dtest=RandomGeneratorTest

 mvn test -Dtest=Random*Test

 mvn test -Dtest=RandomGeneratorTest,AccountCaptchaServiceTest

 mvn test -Dtest=Random*Test,AccountCaptchaServiceTest

 test参数匹配不到一个或多个测试类时会报错并导致构建失败,如mvn test -Dtest

 可以运行mvn test -Dtest -DfailIfNoTests=false,即使没有任何测试类也不报错

 

4.包含与排除测试用例

 

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugiin</artifactId>
				<version>2.5</version>
				<configuration>
					<includes>
						<include>**/*Tests.java</include>
					</includes>
					<excludes>
						<exclude>**/*ServiceTest.java</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

  

5.测试报告

 

    默认情况下maven-surefire-plugin会在项目target/surefire-reports目录下生成txt和xml两种格式的错误报告。

    测试覆盖率是衡量项目代码质量的一个重要参考指标。Maven通过cobertura-maven-plugin集成了Cobertura。运行mvn cobertura:cobertura会在项目target/site/cobertura下生成报告,打开index.html查看。

 

6.用TestNG测试(未测试)

删除Junit依赖,加入TestNG依赖

<dependencies>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>5.9</version>
			<scope>test</scope>
			<classifier>jdk15</classifier>
		</dependency>
	</dependencies>

 

测试类对Junit库的依赖改为对TestNG库的依赖

 

运行mvn test可以测试

TestNG允许用户配置一个testng.xml文件画配置想要运行的测试集合

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置只运行RandomGeneratorTest -->
<suite name="Suite1" version="1">
	<test name="Regression1">
		<classes>
			<class name="com.iteye.xujava.account.captcha.RandomGeneratorTest"/>
		</classes>
	</test>
</suite>

同时在POM文件中配置maven-surefire-plugin使用该testng.xml

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<suiteXmlFiles>
						<suiteXmlFile>testng.xml</suiteXmlFile>
					</suiteXmlFiles>
				</configuration>
			</plugin>
		</plugins>
	</build>

 

TestNG较Junit的一大优势在于它支持测试组的概念
@Test{groups={"util","medium"}}
可以如下配置运行测试组
<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<groups>util,medium</groups>
				</configuration>
			</plugin>
		</plugins>
	</build>
 
7.重用测试代码(未测试)
配置以下代码,会将测试代码一起打包
<build>
		<plugins>
			<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>
		</plugins>
	</build>
 
 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics