【问题标题】:Starting of the Apache tomcat server before integration test在集成测试之前启动 Apache Tomcat 服务器
【发布时间】:2014-10-10 23:25:01
【问题描述】:

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

请帮助我哪里错了。

【问题讨论】:

    标签: maven maven-2 testng pom.xml


    【解决方案1】:

    最小的 POM

    这是一个最小的 POM 文件,我用它来实现你想要的。如果它不适合您,请像@BrennaFlood 所说的那样发布mvn -X clean verify 的输出。 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】:

      您似乎已将 tomcat 启动和停止绑定到集成测试前和集成测试后阶段,但 TestNG 的东西正在测试阶段运行,该测试阶段在集成测试阶段之前。就像其他响应者所说 - 你应该跑步:

      mvn clean verify -X

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

      【讨论】:

        【解决方案3】:

        我只想为希望使用 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 插件旨在运行单元测试

        希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-24
          • 1970-01-01
          • 2018-08-26
          • 2013-04-08
          • 2013-03-18
          • 1970-01-01
          • 2018-01-14
          相关资源
          最近更新 更多