我想为一个Maven项目进行完全自动化的集成测试。 集成测试需要在运行前启动一个外部(平台依赖性)程序。 理想的情况是,在单元测试完成后,外部程序会被杀死,但这不是必要的。
有没有一个Maven插件可以实现这个功能? 还有其他的想法吗?
你可以使用 蚁群 插件。在里面你可以使用蚂蚁的 执行员 应用 任务。
类似这样。
<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>
当然,蚂蚁支持特定的命令,通过 条件任务.
如果你正在进行servlet开发,并且希望部署由此产生的WAR进行集成测试,那么 cargo 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>
我目前正在开发一个更具体的插件,它可以很容易地被 "降级 "为一个简单的外部任务执行器,但是......还有不少其他的事情需要考虑。
如果我真正开始开发插件,我相信会有更多的内容,但真的需要一个通用的执行器吗?
更新。
我想是的......在CodeHaus有一套优秀的Maven插件。 这是你想要的那个。 http:/mojohaus.orgexec-maven-plugin。.
你可能想把实际的集成测试绑定到maven生命周期的集成测试阶段。 如果你使用了一个故障安全插件(如故障安全插件)来进行实际测试,那么你可以像这样运行你的阶段。
预集成测试启动外部应用程序(使用exec插件或这里的其他建议)。
融合测试: 使用故障保护插件运行实际的集成测试。
合并后测试: 关闭外部应用程序并进行任何其他必要的清理工作。
验证: 让故障保护插件验证测试结果,此时构建失败。
使用exec插件是相当直接的,诀窍是让你的应用程序启动起来。背后. 在开始下一阶段的测试之前,你应该注意确保应用程序已经完全启动。 不幸的是,让你的应用程序启动,并确保它在后台运行时有足够的速度,这并不总是一个琐碎的任务,如何做到这一点的具体情况取决于你的应用程序。 这往往涉及到应用程序中的自定义代码。