qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

利用maven与testng来进行测试Maven2 基础教程(3) - pom.xml 文件简介

Maven2 基础教程(3) - pom.xml 文件简介
目标

本文用以说明如何修改maven2的主要配置文件pom.xml在适应我们的项目需要,通过本文您可以了解到

  1. 如何设定编译参数
  2. 设定编译环境为UTF-8编码
  3. 添加依赖项
  4. 添加TestNG框架支持
pom.xml 简介

如下是一个最基础的pom.xml

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.velcro7.framework</groupId> <artifactId>velcro7-base</artifactId> <packaging>jar</packaging> <version>0.1-PROTOTYPE</version> <name>velcro7-base</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies></project>

说明了项目的名称,以及依赖于junit的项目。接下来,我们要调整一下编译参数

修改pom.xml调整编译参数

编译参数,主要通过使用设定maven-compile-plugin来实现
我们加入如下配置信息

<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins></build>

如上,可以设定编译使用UTF-8编码,源码为JDK1.5的版本,目标也为JDK1.5的版本。

设定使用UTF-8编码

除了编译外,还有资源文件、javadoc等都需要告诉maven使用UTF-8编码,我们可以设定如下两个插件

<plugin> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration></plugin><plugin> <artifactId>maven-javadoc-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration></plugin>添加TestNG的测试框架支持

由于自动生成的项目为使用JUnit的测试框架,但是我们的项目使用TestNG的测试框架,所以我们需要调整一下项目的依赖关系,并且设定项目使用的TesgNG配置文件。
首先删除对于JUnit的依赖

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope></dependency>

然后加入如下内容

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

由于TestNG需要不同的包支持JDK15和JDK14所以在此我们要特别指定<classifier>属性。
如果设定了<version>属性,maven会自动下载依赖项的对应版本,如果没有设置<version>属性,Maven会自动下载最新版本。由于我们项目的开发周期比较长,所以需要指定版本,防止开发过程中由Maven自动更换我们使用的依赖库。
<scope>属性,设定了依赖项的使用范围。如果设定为test表示近测试时使用,在打包时不会打包该文件。
接下来,我们使用插件maven-surfire-plugin设定testNG的配置文件位置,如下

<plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration></plugin>

如上,表示使用testng.xml作为testNG的配置文件。

结束语

如上配置,我们最后的配置文件为

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.velcro7.framework</groupId> <artifactId>velcro7-base</artifactId> <packaging>jar</packaging> <version>0.1-PROTOTYPE</version> <name>velcro7-base</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <artifactId>maven-javadoc-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>5.8</version> <scope>test</scope> <classifier>jdk15</classifier> </dependency> </dependencies></project>

posted on 2013-02-19 14:01 顺其自然EVO 阅读(4359) 评论(0)  编辑  收藏 所属分类: selenium and watir webdrivers 自动化测试学习


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年2月>
272829303112
3456789
10111213141516
17181920212223
242526272812
3456789

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜