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的一大优势在于它支持测试组的概念
相关推荐
根据老师讲解,将我自己写的代码写出来,测试没有问题!
spring mybatis maven测试项目
selenium+java+testng+maven测试用例执行,testng执行
maven常用命令以及以个测试项目方便测试
maven资源测试一下,请不要下载
基于Elasticsearch的测试Demo
Maven 测试模式 这种次要模式为 java-mode 提供了一些增强功能,以便轻松使用 maven 测试任务。 它主要基于 Peter Williams 的“rspec-mode”哲学。 即,它提供以下功能: 在测试和它的类之间来回切换(绑定到\Cc ...
Spring 4.0 AOP 面向切面测试程序,涉及通知类型包括前置通知\后置通知\返回通知\异常通知\环绕通知,项目采用Maven搭建!
NULL 博文链接:https://lipaneyeti.iteye.com/blog/2041668
maven 测试写入JRE参数实例详解 项目在测试时碰到一个问题,就是JVM加载参数的问题。 web项目本身在注入配置信息的时候,读取的是本地的配置文件,但是配置文件的位置是卸载tomcat 里面配置的JAVA_OPTS里面的。 ...
Maven测试应用程序 将 Maven 测试应用程序添加到 GitHub
Maven项目测试 Maven 测试项目的存储库。
Maven 测试项目创建 .gitignore 1.create .gitignore.txt 2.open 命令行 3. ren .gitignore.txt .gitignore
一个简单的 maven 测试项目,用于确定对单例的更改是否在以下位置可见: 同一测试类中的测试方法 测试同一模块中的类 测试其他模块中的类 为什么? 我在 Apache Continuum 的测试套件试图找到一些间歇性测试失败的...
maven
Maven测试 测试Maven的工作方式
Maven_02:第一个Maven测试
测试maven 功能的一部分demo,以下可以按照该范例继续补充。
ADP_Testing_Project 使用jUnit和maven测试ObjectIdentity,ObjectEquality,超时和禁用测试。