前两天开始了学习,但后来因为领导要我干别的,有一段时间没看了,这两天紧不紧慢不慢的,又看上了持续集成。
上次试验收在ff下的selenium IDE的使用,看到了好处,但用于实际工作中的集成测试,特别是自动化妆持续集成,还是需要代码一级的。于是开始找资料,搭环境了。
我的环境是m2eclipse,因为要持续集成,所以要mavn2的支持,selenium的例子倒是好找,到处都是,于是我用webapp的模式以maven2先搭起架子。再指定test的目录以输出目录,因为是测试类,所以要发到test-classes里,而不要混到原始代码中。由此还想到,单元测试与功能测试的区别,单元测试是可以由代码员自己来开发的,代到cvs中,而功能测试或集成测试应该由测试员来开发,放到持续集成服务器上,进行功能测试。
按网上的例子建的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>selenium</groupId>
<artifactId>selenium</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>selenium Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium.client-drivers</groupId>
<artifactId>selenium-java-client-driver</artifactId>
<version>1.0-beta-2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>selenium</finalName>
<defaultGoal>package</defaultGoal>
<plugins>
<!-- compiler插件,设定JDK版本及编码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- tomcat在集成测试时启动,在完成后结束 -->
<!--<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager
</url>
<server>myserver</server>
</configuration>
<executions>
<execution>
<id>tomcat_start</id>
<phase>pre-integration-test</phase>
<goals>
在没有tomcat的情况下,以RUN方式启动自带的TOMCAT
<goal>run</goal>
goal>undeploy</goal>
<goal>deploy-only</goal
</goals>
</execution>
<execution>
<id>tomcat_stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
--><!-- selenium也需要单独启动 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start-server</goal>
</goals>
<configuration>
<background>true</background>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-server</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>cn_repository</id>
<name>cn_repository</name>
<url>http://maven.net.cn/content/groups/public
</url>
</repository>
</repositories>
<reporting>
<outputDirectory>${basedir}/target/site/maven</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.0.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<outputDirectory>${basedir}/target/newsite</outputDirectory>
</configuration>
</plugin>
<!--plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.0-beta-6</version>
<configuration>
<outputDirectory>${basedir}/target/newsite</outputDirectory>
</configuration>
</plugin-->
</plugins>
</reporting>
</project>
由于是测google的例子,所以也没有启动tomcat,如果是自己的应用,则可以先启动tomcat之后,再测,如果是验收测试,则可以在演示服务器上跑,也不用启动内部的tomcat。
另,selenium的程序比较新,maven2的仓库里下不到,自己install了一个
mvn install:install-file -Dfile=selenium-java-client-driver.jar -DgroupId=org.seleniumhq.selenium.client-drivers -DartifactId=selenium-java-client-driver -Dversion=1.0-beta-2 -DgeneratePom=true -Dpackaging=jar
这里就是我老出错的地方,pom.xml中必须依赖这个类,后面的test才能运行,要不就说classnotfound。
结果如下:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building selenium Maven Webapp
[INFO]
[INFO] Id: selenium:selenium:war:0.0.1-SNAPSHOT
[INFO] task-segment: [integration-test]
[INFO] ------------------------------------------------------------------------
[INFO] Attempting to resolve a version for plugin: org.codehaus.mojo:selenium-maven-plugin using meta-version: LATEST
[INFO] Using version: 1.0-rc-1 of plugin: org.codehaus.mojo:selenium-maven-plugin
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] No sources to compile
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test]
[INFO] Tests are skipped.
[INFO] [war:war]
[INFO] Packaging webapp
[INFO] Assembling webapp[selenium] in [C:\eclipse\workspace\selenium\selenium\target\selenium]
[INFO] Processing war project
[INFO] Webapp assembled in[31 msecs]
[INFO] Building war: C:\eclipse\workspace\selenium\selenium\target\selenium.war
[INFO] [selenium:start-server]
Launching Selenium Server
Waiting for Selenium Server...
[INFO] User extensions: C:\eclipse\workspace\selenium\selenium\target\selenium\user-extensions.js
log4j:WARN No appenders could be found for logger (org.openqa.selenium.server.SeleniumServer).
log4j:WARN Please initialize the log4j system properly.
Selenium Server started
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\eclipse\workspace\selenium\selenium\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.tests.test
init selenium
start successfully
enter testGoogleTestSearch
open the google.com
input type condition
begin search
finsh assert
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 11.688 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16 seconds
[INFO] Finished at: Thu May 07 08:17:15 CST 2009
[INFO] Final Memory: 4M/13M
[INFO] ------------------------------------------------------------------------
附件中是原来的测试文件,POM.xml的部分是参看的mojo中selenium-maven-plugin的说明(with surefire)
对于maven的集成测试还刚明白一点儿。
分享到:
相关推荐
以上步骤概述了如何搭建一个完整的持续集成测试框架,涵盖了 Jenkins 的安装与配置、Maven 的安装与配置、TestNG 的安装、Selenium 的集成以及如何在 Eclipse 中创建 Maven 项目并进行测试。这些步骤有助于开发者...
在Eclipse中,右键点击项目,选择`Run As` -> `TestNG Suite`,然后选择你的测试类或测试套件,Eclipse将自动运行测试并展示结果。 **兼容性测试** 为了确保应用程序在不同浏览器和操作系统上表现一致,兼容性测试...
使用TESTNG,我们可以进行单元测试、集成测试以及系统测试。它的主要特点包括: 1. 注解支持:通过注解如@BeforeSuite、@AfterTest等,可以轻松定义测试前后的设置和清理动作。 2. 依赖性测试:允许测试方法之间有...
- `start-selenium.bat`:启动Selenium Server,便于进行自动化测试。 - `start-tomcat.bat`:启动内置的Tomcat服务器。 #### 三、生成项目 1. **环境配置**:将Maven的`bin`目录加入到系统的`PATH`环境变量中,以...