4

我想完全自動化Maven項目的集成測試。集成測試需要在運行之前啓動外部(平臺相關)程序。理想情況下,外部程序將在單元測試完成後被殺死,但並非必要。在maven集成測試期間啓動外部進程

有沒有一個Maven插件來完成這個?其他想法?

回答

4

您可以使用antrun插件。裏面你會用螞蟻的 exec apply任務。

就是這樣。當然通過condition task

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.2</version> 
    <executions> 
     <execution> 
     <phase> <!-- a lifecycle phase --> </phase> 
     <configuration> 

      <tasks> 
      <apply os="unix" executable="cmd"> 
       <arg value="/c"/> 
       <arg value="ant.bat"/> 
       <arg value="-p"/> 
      </apply> 
      <apply os="windows" executable="cmd.exe"> 
       <arg value="/c"/> 
       <arg value="ant.bat"/> 
       <arg value="-p"/> 
      </apply> 
      </tasks> 

     </configuration> 
     <goals> 
      <goal>run</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 

螞蟻支持OS特定的命令。

+0

antrun插件的一個有趣的,非直觀的用法。我希望有更清潔的東西,但這可能對我有用。 – flicken 2008-10-06 19:02:38

2

我目前正在研究一個更具體的插件,可能很容易被「降級」成爲一個簡單的外部任務執行器,但是...還有其他一些事情需要考慮。

  1. 你怎麼知道這個過程已經開始了?
  2. 你如何處理返回碼?
  3. 如何確保執行程序插件先運行(將其綁定到測試編譯階段)?

我確定如果我真的開始開發插件,會有更多,但真的需要一個通用執行器嗎?

UPDATE:

我想有...有一個很好的集合在Codehaus Maven插件的。這是你想要的:http://mojo.codehaus.org/exec-maven-plugin/

2

如果您正在進行servlet開發並希望部署生成的WAR進行集成測試,那麼貨運maven插件是一個好方法。

當我自己做這件事時,我經常建立一個多模塊項目(儘管這不是嚴格意義上的要求),並將所有集成測試封裝到一個模塊中。然後,我使用配置文件啓用模塊(或不啓用),以便它不會阻止即時的「是的,我知道我打破了它」構建。

下面是從功能測試模塊的POM - 它做什麼,你會:

<?xml version="1.0"?><project> 
    <parent> 
    <artifactId>maven-example</artifactId> 
    <groupId>com.jheck</groupId> 
    <version>1.5.0.4-SNAPSHOT</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.jheck.example</groupId> 
    <artifactId>functional-test</artifactId> 
    <name>Example Functional Test</name> 
    <packaging>pom</packaging> 

    <dependencies> 
    <dependency> 
     <groupId>com.jheck.example</groupId> 
     <artifactId>example-war</artifactId> 
     <type>war</type> 
     <scope>provided</scope> 
     <version>LATEST</version> 
    </dependency> 
    <dependency> 
     <groupId>httpunit</groupId> 
     <artifactId>httpunit</artifactId> 
     <version>1.6.1</version> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 

    <build> 
    <plugins> 

     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <executions> 
      <execution> 
      <goals> 
       <goal>testCompile</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 

     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <executions> 
      <execution> 
      <phase>integration-test</phase> 
      <goals> 
       <goal>test</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 

     <plugin> 
     <groupId>org.codehaus.cargo</groupId> 
     <artifactId>cargo-maven2-plugin</artifactId> 
     <version>0.3</version> 
     <configuration> 
      <wait>false</wait> <!-- don't pause on launching tomcat... --> 
      <container> 
      <containerId>tomcat5x</containerId> 
      <log>${project.build.directory}/cargo.log</log> 
      <zipUrlInstaller> 
       <!-- 
       <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.0.30/bin/jakarta-tomcat-5.0.30.zip</url> 
       --> 
       <!-- better be using Java 1.5... --> 
       <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.5.26/bin/apache-tomcat-5.5.26.zip</url> 

       <installDir>${installDir}</installDir> 
      </zipUrlInstaller> 
      </container> 
      <configuration> 
      <!-- where the running instance will be deployed for testing --> 
      <home>${project.build.directory}/tomcat5x/container</home> 
      </configuration> 
     </configuration> 

     <executions> 
      <execution> 
      <id>start-container</id> 
      <phase>pre-integration-test</phase> 
      <goals> 
       <goal>start</goal> 
       <goal>deploy</goal> 
      </goals> 
      <configuration> 
       <deployer> 
       <deployables> 
        <deployable> 
        <groupId>com.jheck.example</groupId> 
        <artifactId>example-war</artifactId> 
        <type>war</type> 
        <!-- <properties> 
         <plan>${basedir}/src/deployment/geronima.plan.xml</plan> 
        </properties> --> 
        <pingURL>http://localhost:8080/example-war</pingURL> 
        </deployable> 
       </deployables> 
       </deployer> 
      </configuration> 
      </execution> 
      <execution> 
      <id>stop-container</id> 
      <phase>post-integration-test</phase> 
      <goals> 
       <goal>stop</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 

</project> 
1

你可能想你的實際集成測試綁定到maven的生命週期的集成測試階段。如果您使用的是失敗的安全(如適當命名的故障安全插件)插件來進行實際的測試,你就可以運行你的階段是這樣的:

預集成測試:啓動外部應用程序(使用exec插件或這裏的其他建議)

集成測試之一:使用故障安全插件運行實際的集成測試

後的集成測試:關閉外部應用程序並執行其他必要的清理

驗證:有故障安全插件驗證測試的結果,在這一點上

這是相當直接的使用exec插件構建失敗,關鍵是讓你的應用程序在啓動背景。在下一階段開始測試之前,您應該小心確保應用程序已完全啓動。不幸的是,在後臺運行時確保應用程序已經夠用了,這並不總是一件簡單的事情,而且具體如何去做取決於你的應用程序。它通常涉及應用程序中的自定義代碼。