在Eclipse IDE中使用JUnit 4排除集成测试

Rap*_*oth 6 eclipse junit maven

我在Java(Maven)Web项目中有两种测试:使用嵌入式Tomcat 7服务器进行"正常"单元测试和集成测试,使用Selenium进行Jenkins上的自动GUI测试.所有测试都使用JUnit进行注释@Test,正常测试以"Test.java"结束,而集成测试以"IntegrationTest.java"结束.所有测试类都位于src/test/java中

我通常使用构建我的项目mvn clean verify,而我的相关部分pom.xml启动tomcat服务器并相应地拆分测试类别如下所示:

    <!-- For front-end testing -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <uriEncoding>UTF-8</uriEncoding>
                <additionalConfigFilesDir>${basedir}/conf</additionalConfigFilesDir>
                <contextFile>${basedir}/src/test/resources/context.xml</contextFile>
            </configuration>
            <executions>
                <execution>
                    <id>start-tomcat</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run-war-only</goal>
                    </goals>
                    <configuration>
                        <fork>true</fork>
                        <port>9090</port>
                    </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-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <excludes>
                    <exclude>**/*IntegrationTest*</exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <includes>
                    <include>**/*IntegrationTest*</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

这个过程工作正常,除非我想在eclipse中运行我的测试,我通常右键单击我的项目 - >运行为 - > JUnit测试.通过选择此选项,可以运行所有测试(包括集成测试).在这种情况下,集成测试失败,因为Tomcat没有运行(它只在Maven的pre-integration-test阶段启动).

如何使用JUnit插件在Eclipse中排除这些测试?

Jen*_*gsa 3

我使用junit-toolbox来实现这一点。它通过通配符模式为单独的单元测试和集成测试提供注释。

\n\n
<dependency>\n    <groupId>com.googlecode.junit-toolbox</groupId>\n    <artifactId>junit-toolbox</artifactId>\n    <version>2.2</version>\n    <scope>test</scope>\n</dependency>\n
Run Code Online (Sandbox Code Playgroud)\n\n

eg下的基础包/src/test/java/base包含两个类\xe2\x80\x93

\n\n

AllUnitTests.java

\n\n
package base;\n\nimport org.junit.runner.RunWith;\n\nimport com.googlecode.junittoolbox.ParallelSuite;\nimport com.googlecode.junittoolbox.SuiteClasses;\n\n/**\n * This detects all (fast running) unit test classes by the given naming pattern.\n * \n */\n@RunWith(ParallelSuite.class)\n@SuiteClasses({ "**/*Test.class", "!**/*IntegrationTest.class", "!**/*LearningTest.class" })\npublic class AllUnitTests {\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

和\n AllIntegrationTests.java

\n\n
package base;\n\nimport org.junit.runner.RunWith;\n\nimport com.googlecode.junittoolbox.SuiteClasses;\nimport com.googlecode.junittoolbox.WildcardPatternSuite;\n\n/**\n * This detects all integration test classes by the given naming pattern.\n * \n */\n@RunWith(WildcardPatternSuite.class)\n@SuiteClasses({ "**/*IntegrationTest.class", "**/*IT.class" })\npublic class AllIntegrationTests {\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以通过 Eclipse 运行这两个通用测试套件。

\n\n

为了能够通过 Maven 运行测试,我使用了一种与您所展示的方法类似的方法。

\n