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

6
我在我的Java (Maven) Web项目中有两种测试:普通的单元测试和使用嵌入式Tomcat 7服务器和Selenium进行自动化GUI测试的集成测试。所有测试都带有JUnit的@Test注释,普通测试以"Test.java"结尾,而集成测试以"IntegrationTest.java"结尾。所有测试类都位于src/test/java目录下。
我通常使用"mvn clean verify"构建我的项目,而启动tomcat服务器并相应地分配测试类别的pom.xml文件的相关部分如下:
    <!-- 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>

这个过程很好用,但是当我想在eclipse中运行测试时,我通常右键单击我的项目 -> 运行为 -> JUnit测试。选择此选项将运行所有测试(包括集成测试)。在这种情况下,集成测试失败,因为Tomcat没有运行(它只在Maven的pre-integration-test阶段启动)。

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

1个回答

3

我使用junit-toolbox来实现这个目的。它提供了通配符模式的注释,以区分单元测试和集成测试。

<dependency>
    <groupId>com.googlecode.junit-toolbox</groupId>
    <artifactId>junit-toolbox</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

基础包位于例如/src/test/java/base下,包含两个类 -

AllUnitTests.java:

package base;

import org.junit.runner.RunWith;

import com.googlecode.junittoolbox.ParallelSuite;
import com.googlecode.junittoolbox.SuiteClasses;

/**
 * This detects all (fast running) unit test classes by the given naming pattern.
 * 
 */
@RunWith(ParallelSuite.class)
@SuiteClasses({ "**/*Test.class", "!**/*IntegrationTest.class", "!**/*LearningTest.class" })
public class AllUnitTests {

}

AllIntegrationTests.java

package base;

import org.junit.runner.RunWith;

import com.googlecode.junittoolbox.SuiteClasses;
import com.googlecode.junittoolbox.WildcardPatternSuite;

/**
 * This detects all integration test classes by the given naming pattern.
 * 
 */
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({ "**/*IntegrationTest.class", "**/*IT.class" })
public class AllIntegrationTests {

}

你可以通过Eclipse运行通用测试套件。
为了能够通过Maven运行测试,我使用了与您展示的方法类似的方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接