运行JUnit 4测试并行使用FailSafe& SureFire插件

时间:2011-12-30 01:16:19

标签: java maven-2 junit4 surefire maven-failsafe-plugin

我们在maven中创建了一个配置文件来运行我们的 Selenium junit4类型测试,下面是没有执行标记的代码片段。

<profile>
    <id>selenium-tests</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.11</version>
                <dependencies>
                    <!-- Force using the latest JUnit 47 provider -->
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.11</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <parallel>classes</parallel>
                    <threadCount>5</threadCount>
                    <forkMode>pertest</forkMode>
                    <useManifestOnlyJar>false</useManifestOnlyJar>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <skip>false</skip>
                    <includes>
                         <include>**/regtests/*.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

我的TestClass看起来像这样。

@RunWith(HTMLSourceDumperJUnit4Runner.class) //Our own Runner
public class MyTestClass extends Assert {

     private int x = 1;
     private int y = 1;

     @Test
     public void testAddition() {
         int z = x + y;
         assertEquals(2, z);
     }

}

当我通过带有并行配置的failsafe插件2.11运行此测试类时,它会因以下错误而失败。

java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:171)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:115)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:269)
    at org.junit.runners.ParentRunner.(ParentRunner.java:66)
    at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:59)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runner.Computer.getRunner(Computer.java:38)
    at org.apache.maven.surefire.junitcore.ConfigurableParallelComputer.getRunner(ConfigurableParallelComputer.java:142)
    at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:93)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
    at org.junit.runners.Suite.(Suite.java:79)
    at org.junit.runner.Computer.getSuite(Computer.java:26)
    at org.apache.maven.surefire.junitcore.ConfigurableParallelComputer.getSuite(ConfigurableParallelComputer.java:134)
    at org.junit.runner.Request.classes(Request.java:69)
    at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:53)
    at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:188)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:166)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:101)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)

这里有什么我想念的吗?如果我对这篇文章的任何信息都滞后,请回复。

2 个答案:

答案 0 :(得分:4)

根据插件上的maven文档,特别是<includes>标记。测试类名称模式为:**/IT*.java**/*IT.java**/*ITCase.java。因此,您需要将班级名称更改为MyITMyITCase或类似名称。

http://maven.apache.org/plugins/maven-failsafe-plugin/integration-test-mojo.html#includes

答案 1 :(得分:2)

在surefire 2.11中似乎存在一个错误。它不喜欢用

运行
<useManifestOnlyJar>false</useManifestOnlyJar>

我提交了一个错误。 http://jira.codehaus.org/browse/SUREFIRE-819