Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例)

来源:互联网 发布:linux同名文件夹合并 编辑:程序博客网 时间:2024/05/19 17:07

 

持续交付要“自动化所有东西”,对于集成测试也是一样。集成测试和单元测试相比需要更多的环境准备工作,包括测试数据的准备和启动服务器等。在本篇中我们设想以下一种场景:

 

  • 你开发了一个web应用,集成测试使用了Selenium,你希望通过一个Maven命令跑完所有的测试,包括集成测试。

 

Maven的plugin包含了一个或多个goal,每一个goal表示plugin的一个操作单位,在plugin开发中,一个goal通常对应于Java类的一个方法(Mojo的execute方法,请参考本系列之六)。一个goal可以默认绑定到Mavan的某个phase,比如Jar plugin的jar这个goal便默认绑定在了package这个phase上。当Maven执行到package时,Jar的jar goal将自动执行。当然,在默认情况下plugin的goal也可以不绑定在任何一个phase上,此时Maven将不做任何操作。但是,我们可以显式地手动将某个plugin的某个goal绑定在一个phase中。

 

对于上面的场景,我们的解决方案是:在集成测试之前(对应Maven的phase为pre-integration-test),我们使用jetty-maven-plugin启动web应用,在集成测试时通过Selenium访问网站进行验证,集成测试完毕之后(对应Maven的phase为post-integration-test),同样使用jetty-maven-plugin关闭web应用。

 

Github示例代码:https://github.com/davenkin/maven-plugin-binding.git

 

通过上一篇的方式创建一个web工程,向其中添加一个helloworld.html用于测试:

 

复制代码
<html><body><h2>Hello World!</h2></body></html>
复制代码

 

 

添加Selelium Webdriver集成测试如下:

 

复制代码
public class SafariIntegrationTest {   @Test   public void testHelloWorldIndexPage() {       WebDriver driver = new HtmlUnitDriver();       driver.get("http://localhost:8080/helloworld.html");       WebElement element = driver.findElement(By.tagName("h2"));       assertThat(element.getText(), is("Hello World!"));   }}
复制代码

 

 

为了使用Selenium,我们需要将Selenium依赖加入pom.xml文件中:

 

复制代码
       <dependency>           <groupId>org.seleniumhq.selenium</groupId>           <artifactId>selenium-java</artifactId>           <version>2.33.0</version>       </dependency>
复制代码

 

 

接下来就可以配置jetty-maven-plugin了:

 

复制代码
           <plugin>               <groupId>org.mortbay.jetty</groupId>               <artifactId>jetty-maven-plugin</artifactId>               <version>8.1.12.v20130726</version>                <configuration>                   <scanintervalseconds>0</scanintervalseconds>                   <stopKey>stop</stopKey>                   <stopPort>9999</stopPort>               </configuration>                <executions>                   <execution>                       <id>start-jetty</id>                       <phase>pre-integration-test</phase>                       <goals>                           <goal>run</goal>                       </goals>                       <configuration>                           <scanintervalseconds>0</scanintervalseconds>                           <daemon>true</daemon>                       </configuration>                   </execution>                    <execution>                       <id>stop-jetty</id>                       <phase>post-integration-test</phase>                       <goals>                           <goal>stop</goal>                       </goals>                   </execution>               </executions>           </plugin>
复制代码

 

 

可以看出,在pre-integration-test阶段,我们调用了jetty-maven-plugin的run,此时web服务器启动,在post-integration-test阶段,我们调用了jetty-maven-plugin的stop来关闭web服务器。

 

但是这里有个问题,在运行mvn clean install时,Maven会先运行单元测试,再运行集成测试,并且在默认情况下这两种测试都会运行以*Test.java结尾的测试类,结果在单元测试阶段也会运行上面的SafariIntegrationTest,结果还没有执行到集成测试阶段就挂了。

 

此时,我们需要将单元测试和集成测试分开,Maven使用maven-surefire-plugin执行测试,我们可以先将SafariIntegrationTest排除在测试之外,这样单元测试将不会运行该测试,然后在集成测试中,在将SafariIntegrationTest包含进来,此时我们需要修改maven-surefire-plugin的配置:

 

复制代码
           <plugin>               <groupId>org.apache.maven.plugins</groupId>               <artifactId>maven-surefire-plugin</artifactId>               <configuration>                   <includes>                       <include>**/unit/*Test.java</include>                   </includes>               </configuration>               <executions>                   <execution>                       <id>surefire-it</id>                       <phase>integration-test</phase>                       <goals>                           <goal>test</goal>                       </goals>                       <configuration>                           <includes>                               <include>**/*IntegrationTest.java</include>                           </includes>                       </configuration>                   </execution>               </executions>           </plugin>
复制代码

 

现在运行mvn clean install,成功。在上面的例子中,我们使用了一个小trick,先将只有在unit目录下的以*Test.java结尾的类看做测试类,此时不包含SafariIntegrationTest,而在itegration-test阶段,我们将运行以*IntegrationTest.java结尾的测试类。

 

你可能还是不怎么放心,除非自己看到了实际的页面为止。要达到这样的目的,我们可以将SafariIntegrationTest中的:

       

WebDriver driver = new HtmlUnitDriver();//使用HtmlUnit

 

修改为:

 

 WebDriver driver = new SafariDriver();//打开Safari浏览器

 

或者:

 

WebDriver driver = new InternetExplorerDriver();//打开IE浏览器

 

此时再运行mvn clean install,浏览器窗口将打开。

 

在下一篇中,我们将讲到如何使用自己的repository——Nexus。

阅读全文
0 0
原创粉丝点击