Starting of the Apache tomcat server before integration test

故事扮演 提交于 2021-02-18 12:38:28

问题


I've been looking for an solution for the last 4 days and raised this question as a bounty but still not getting my answer.

Where i've succeeded with the help pf pom.xml file:- a) Starting the tomcat server manually using command i.e mvn tomcat7:run. This command also help me deploying of my war file to tomcat server and starting the server. b) Running my integration tests using testng.xml file configuration on eclipse.

Where i'm failed with the help pf pom.xml file:-

a) Automatically starting of tomcat server. b) Running all the integration tests. c) Stopping of tomcat server.

This question is posted by me but couldn't find the answer Starting apache server before integration testing not working

Please help where i'm wrong.


回答1:


Minimal POM

Here is a minimal POM file that I used to achieve what you want. If it doesn't work for you, please post the output of mvn -X clean verify like @BrennaFlood said. Configurations for tomcat7-maven-plugin and maven-failsafe-plugin taken from http://tomcat.apache.org/maven-plugin-2.2/run-mojo-features.html#Use_it_with_selenium_mojo and http://maven.apache.org/surefire/maven-failsafe-plugin/usage.html, respectively.

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


It looks like you have tomcat start and stop bound to pre-integration-test and post-integration-test phases, but the TestNG stuff is being run during the test phase, which comes before the integration-test phases. Like the other responder said - you should be running:

mvn clean verify -X

... so that you're catching all the phases up through post-integration-test (and catching all the debug information for troubleshooting), but you should also bind your TestNG components to the integration-test phase.




回答3:


I just want to add this for anyone that is looking to use maven + tomcat7 + testng. Basically my scenario is that some of our IT test needs the running application so they can send some REST call, but some of the IT does not require the server, I split the test cases in two different suites one for the IT that requires the server in the ServerRequiredIT.xml and others in NonServerRequiredIT.xml, based on that I create two profiles as it follows.

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

To run the profiles I use mvn test -P 'nameOfProfile'. Important thing here is what the documentation said

The Failsafe Plugin is designed to run integration tests while the Surefire Plugin is designed to run unit tests

Hope that helps.



来源:https://stackoverflow.com/questions/25356685/starting-of-the-apache-tomcat-server-before-integration-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!