在maven的集成测试中启动外部流程

问题描述 投票:4回答:5

我想为一个Maven项目进行完全自动化的集成测试。 集成测试需要在运行前启动一个外部(平台依赖性)程序。 理想的情况是,在单元测试完成后,外部程序会被杀死,但这不是必要的。

有没有一个Maven插件可以实现这个功能? 还有其他的想法吗?

java maven-2 build-process automated-tests integration-testing
5个回答
4
投票

你可以使用 蚁群 插件。在里面你可以使用蚂蚁的 执行员 应用 任务。

类似这样。

<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>

当然,蚂蚁支持特定的命令,通过 条件任务.


2
投票

如果你正在进行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>

2
投票

我目前正在开发一个更具体的插件,它可以很容易地被 "降级 "为一个简单的外部任务执行器,但是......还有不少其他的事情需要考虑。

  1. 你怎么知道这个过程实际上已经开始了?
  2. 你怎么处理返回代码?
  3. 如何确保执行器插件先运行(将其绑定到测试编译阶段)?

如果我真正开始开发插件,我相信会有更多的内容,但真的需要一个通用的执行器吗?

更新。

我想是的......在CodeHaus有一套优秀的Maven插件。 这是你想要的那个。 http:/mojohaus.orgexec-maven-plugin。.


1
投票

你可能想把实际的集成测试绑定到maven生命周期的集成测试阶段。 如果你使用了一个故障安全插件(如故障安全插件)来进行实际测试,那么你可以像这样运行你的阶段。

预集成测试启动外部应用程序(使用exec插件或这里的其他建议)。

融合测试: 使用故障保护插件运行实际的集成测试。

合并后测试: 关闭外部应用程序并进行任何其他必要的清理工作。

验证: 让故障保护插件验证测试结果,此时构建失败。

使用exec插件是相当直接的,诀窍是让你的应用程序启动起来。背后. 在开始下一阶段的测试之前,你应该注意确保应用程序已经完全启动。 不幸的是,让你的应用程序启动,并确保它在后台运行时有足够的速度,这并不总是一个琐碎的任务,如何做到这一点的具体情况取决于你的应用程序。 这往往涉及到应用程序中的自定义代码。


0
投票

你想启动一个应用服务器吗?请看一下 货物 及其 Maven插件.

© www.soinside.com 2019 - 2024. All rights reserved.