在集成测试之前启动Apache tomcat服务器

时间:2014-08-18 05:11:01

标签: maven maven-2 testng pom.xml

我一直在寻找过去4天的解决方案,并提出这个问题作为赏金,但仍未得到答案。

我在pf pom.xml文件的帮助下成功了: -   a)使用命令手动启动tomcat服务器,即mvn tomcat7:run。这个命令也      帮助我将我的war文件部署到tomcat服务器并启动服务器。   b)在eclipse上使用testng.xml文件配置运行我的集成测试。

我在帮助pf pom.xml文件时失败: -

a)自动启动tomcat服务器。    b)运行所有集成测试。    c)停止tomcat服务器。

这个问题由我发布但未找到答案     Starting apache server before integration testing not working

请在我错的地方帮忙。

3 个答案:

答案 0 :(得分:1)

看起来你有tomcat开始和停止绑定到预集成测试和集成后测试阶段,但TestNG的东西正在测试阶段运行,这是在集成测试阶段之前。就像其他响应者说的那样 - 你应该跑步:

mvn clean verify -X

...以便通过集成后测试(并捕获所有调试信息以进行故障排除)捕获所有阶段,但您还应将TestNG组件绑定到集成测试阶段。

答案 1 :(得分:1)

最小POM

这是我用来实现你想要的最小POM文件。如果它对您不起作用,请发布mvn -X clean verify的输出,如@BrennaFlood所说。 tomcat7-maven-plugin和maven-failsafe-plugin的配置分别取自http://tomcat.apache.org/maven-plugin-2.2/run-mojo-features.html#Use_it_with_selenium_mojohttp://maven.apache.org/surefire/maven-failsafe-plugin/usage.html

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>tomcat-with-failsafe</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <prerequisites>
    <maven>2.2.1</maven>
  </prerequisites>

  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.8.8</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <id>tomcat7-run</id>
            <goals>
              <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
              <fork>true</fork> 
            </configuration>
          </execution>
          <execution>
            <id>tomcat7-shutdown</id>
            <goals>
              <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
          </execution>
        </executions>
      </plugin> 
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.17</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

答案 2 :(得分:0)

我只是想为那些希望使用maven + tomcat7 + testng的人添加这个。基本上我的情况是我们的一些IT测试需要正在运行的应用程序,因此他们可以发送一些REST调用,但有些IT不需要服务器,我将测试用例分成两个不同的套件,一个用于需要服务器的IT在ServerRequiredIT.xmlNonServerRequiredIT.xml中的其他人,基于此我创建了两个配置文件。

<profiles>
        <profile>
            <id>run-its</id>
            <properties>
                <build.profile.id>integration-test</build.profile.id>
                <skip.integration.tests>false</skip.integration.tests>
                <skip.unit.tests>true</skip.unit.tests>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.0</version>
                        <configuration>
                            <path>/</path>
                        </configuration>
                        <executions>
                            <execution>
                                <id>start-tomcat</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <fork>true</fork>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop-tomcat</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>shutdown</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <argLine>-Xmx2048m</argLine>
                            <printSummary>true</printSummary>
                            <redirectTestOutputToFile>true</redirectTestOutputToFile>
                            <systemProperties>
                                <property>
                                    <name>log4j.configuration</name>
                                    <value>file:${project.build.testOutputDirectory}/resources/log4j-surefire.properties</value>
                                </property>
                            </systemProperties>
                            <suiteXmlFiles>
                                <suiteXmlFile>src/test/scripts/ServerRequiredIT.xml</suiteXmlFile>
                            </suiteXmlFiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>testNG-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <argLine>-Xmx2048m</argLine>
                            <printSummary>true</printSummary>
                            <redirectTestOutputToFile>true</redirectTestOutputToFile>
                            <systemProperties>
                                <property>
                                    <name>log4j.configuration</name>
                                    <value>file:${project.build.testOutputDirectory}/resources/log4j-surefire.properties</value>
                                </property>
                            </systemProperties>
                            <suiteXmlFiles>
                                <suiteXmlFile>src/test/scripts/NonServerRequiredIT.xml</suiteXmlFile>
                            </suiteXmlFiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

要运行配置文件,请使用mvn test -P 'nameOfProfile'。这里重要的是文档所说的

  

Failsafe插件旨在运行集成测试   Surefire插件旨在运行单元测试

希望有所帮助。