如何在运行集成测试之前启动tomcat?

发布于 2024-12-27 13:14:45 字数 7823 浏览 1 评论 0原文

我正在尝试在单独的 Maven 配置文件中运行集成测试,因此我需要按照以下步骤执行此操作:

  1. 运行 tomcat 服务器。
  2. 运行 Selenium 服务器。
  3. 运行集成测试。

我按如下方式运行集成测试:

mvn install -Pit

但是发生的情况是集成测试在服务器启动之前首先运行,这将导致测试失败,以下是我的配置:

<profile>
          <id>it</id>
          <build>
           <plugins>


        <plugin>

            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.1.4</version>
            <configuration>

                <wait>false</wait> 
                <container>
                 <containerId>tomcat7x</containerId>
                 <home>${env.CATALINA_HOME}</home>  
                 <timeout>300000</timeout>                  
                </container>

                <configuration>
                 <type>standalone</type>
                 <home>target/tomcat7x</home> 
                 <properties>
                  <cargo.jvmargs>-XX:PermSize=256m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled</cargo.jvmargs>
                </properties> 
                </configuration>



            </configuration>
                <executions>
                    <execution>
                    <id>start-container</id>
                    <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                    <execution>
                    <id>stop-container</id>
                    <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
          </plugin> 



        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>selenium-maven-plugin</artifactId>
              <executions>
                    <execution>
                    <id>start</id>
                    <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-server</goal>
                        </goals>
                    <configuration>
                        <background>true</background>
                        <logOutput>true</logOutput>
                    </configuration>
                </execution>

                <execution>
                <id>stop</id>
                <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop-server</goal>
                        </goals>
                </execution> 
            </executions>
    </plugin>


             <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.8</version>

                <configuration>
                    <junitArtifactName>
                    org.junit:com.springsource.org.junit
                    </junitArtifactName>
                    <excludes>

                        <exclude>**/unit/*Test.java</exclude>
                    </excludes>
                </configuration>


                <executions>
                    <execution>

                    <id>integration-tests</id>
                    <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    <configuration>
                    <skip>false</skip>
                    <excludes>
                        <exclude>none</exclude>
                    </excludes>

                    <includes>
                       <include>**/integration/*Test.java</include>
                    </includes>
                    </configuration>
                    </execution>
            </executions>

                </plugin>
              </plugins>
            </build>

            <activation>
              <property>
                <name>it</name>
              </property>
            </activation>

        </profile>

请告知我的配置有什么问题。

更新:Maven日志

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building My APP
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [jaxws:wsimport {execution: default}]
[debug] execute contextualize
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[debug] execute contextualize
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\Workspace\MyAPP\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running integration.MyTest

更新3

删除后用以下内容替换旧的surefire插件后工作正常:

<plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <executions>

                    <execution>
                        <id>default-test</id>                                
                        <configuration>
                            <skipTests>true</skipTests>
                        </configuration>
                    </execution>

                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/integration/*Test.java</include>
                            </includes>
                            <skipTests>false</skipTests>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <argLine>-Xms256M -Xmx768M -XX:MaxPermSize=256M</argLine>
                </configuration>
            </plugin>

但是货物插件有一个问题,是它随后部署了war文件运行集成测试,并在调用任何测试方法之前取消部署 war 文件?

i am trying to run integration test in separate maven profile, so i need to do it in following steps:

  1. Run tomcat server.
  2. Run Selenium server.
  3. Run integration tests.

i run the integration test as follows:

mvn install -Pit

but what happens is that integration test runs first before server starts, which will cause test to fail, following is my configuration:

<profile>
          <id>it</id>
          <build>
           <plugins>


        <plugin>

            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.1.4</version>
            <configuration>

                <wait>false</wait> 
                <container>
                 <containerId>tomcat7x</containerId>
                 <home>${env.CATALINA_HOME}</home>  
                 <timeout>300000</timeout>                  
                </container>

                <configuration>
                 <type>standalone</type>
                 <home>target/tomcat7x</home> 
                 <properties>
                  <cargo.jvmargs>-XX:PermSize=256m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled</cargo.jvmargs>
                </properties> 
                </configuration>



            </configuration>
                <executions>
                    <execution>
                    <id>start-container</id>
                    <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                    <execution>
                    <id>stop-container</id>
                    <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
          </plugin> 



        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>selenium-maven-plugin</artifactId>
              <executions>
                    <execution>
                    <id>start</id>
                    <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-server</goal>
                        </goals>
                    <configuration>
                        <background>true</background>
                        <logOutput>true</logOutput>
                    </configuration>
                </execution>

                <execution>
                <id>stop</id>
                <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop-server</goal>
                        </goals>
                </execution> 
            </executions>
    </plugin>


             <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.8</version>

                <configuration>
                    <junitArtifactName>
                    org.junit:com.springsource.org.junit
                    </junitArtifactName>
                    <excludes>

                        <exclude>**/unit/*Test.java</exclude>
                    </excludes>
                </configuration>


                <executions>
                    <execution>

                    <id>integration-tests</id>
                    <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    <configuration>
                    <skip>false</skip>
                    <excludes>
                        <exclude>none</exclude>
                    </excludes>

                    <includes>
                       <include>**/integration/*Test.java</include>
                    </includes>
                    </configuration>
                    </execution>
            </executions>

                </plugin>
              </plugins>
            </build>

            <activation>
              <property>
                <name>it</name>
              </property>
            </activation>

        </profile>

please advise what's wrong with my configuration.

UPDATE: Maven Logs

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building My APP
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [jaxws:wsimport {execution: default}]
[debug] execute contextualize
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[debug] execute contextualize
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\Workspace\MyAPP\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running integration.MyTest

UPDATE3:

it worked fine after removing replacing the old surefire plugin with the following:

<plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <executions>

                    <execution>
                        <id>default-test</id>                                
                        <configuration>
                            <skipTests>true</skipTests>
                        </configuration>
                    </execution>

                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/integration/*Test.java</include>
                            </includes>
                            <skipTests>false</skipTests>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <argLine>-Xms256M -Xmx768M -XX:MaxPermSize=256M</argLine>
                </configuration>
            </plugin>

BUT there's a problem with cargo plugin, is that it deploys the war file then runs the integration test, and before invoking any test method it un-deploys the war file ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2025-01-03 13:14:45

根据日志,很明显失败是因为 Surefire 正在 test 阶段运行集成测试,而不是 integration-test 阶段。

此链接说明如何仅使用 Surefire 进行集成测试。

本文档提供了有关 Maven 和集成测试最佳实践的更多见解。

Based on the log, it is evident that the failure is because surefire is running the integration test in the test phase and not integration-test phase.

This link says, how to use surefire only for integration testing.

This documentation gives more insights into best practices of maven and integration testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文