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

 

这是一个Maven提高篇的系列,包含有以下文章:

  

  1. Maven提高篇系列之(一)——多模块 vs 继承
  2. Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例)
  3. Maven提高篇系列之(三)——使用自己的Repository(Nexus)
  4. Maven提高篇系列之(四)——使用Profile
  5. Maven提高篇系列之(五)——处理依赖冲突
  6. Maven提高篇系列之(六)——编写自己的Plugin(本系列完)

  

 

 

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

 

  • 你开发了一个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

posted @ 2016-08-13 09:46  无知者云  阅读(9500)  评论(0编辑  收藏  举报