【问题标题】:Maven unit test concurrency and resource limitsMaven 单元测试并发和资源限制
【发布时间】:2013-11-21 17:07:38
【问题描述】:

我正在尝试将 ant 项目迁移到 maven 3.1。目前,我们为每个要测试的环境(服务器)设置了目标,这些目标对 Chrome 和 Firefox 运行相同的测试集。由于一些限制,我们不能同时运行两个相同类型的浏览器。如果我设置 maven surefire 插件一次运行 1 个测试,则结果有效。如果我用两个线程进行测试,最终它们开始失败,因为一个浏览器被多次执行,因为 Firefox 在我们的网站上运行速度较慢。

在 ant 中,测试目标包含以下内容:

<parallel threadCount="2">
        <junit fork="yes" printsummary="withOutAndErr" haltonfailure="no">
            <formatter type="xml" />
            <batchtest fork="true" todir="${junit.output.dir}">
                <fileset dir="target/test-classes/" includes="**/TestFirefox.class">
                </fileset>
            </batchtest>
            <classpath refid="DartSeleniumTest.classpath" />
        </junit>

        <junit fork="yes" printsummary="withOutAndErr" haltonfailure="no">
            <formatter type="xml" />
            <batchtest fork="yes" todir="${junit.output.dir}">
                <fileset dir="target/test-classes/" includes="**/TestChrome.class" />
            </batchtest>
            <classpath  refid="DartSeleniumTest.classpath" />
        </junit>
    </parallel>

我曾尝试使用 antrun 插件作为 Surefire 的替代品,但它随 junit 3.x 一起提供。是否有任何其他选项可以并行但分组运行测试?

【问题讨论】:

  • 可能 JUnit 3.x 是个问题,因为您的测试是为 JUnit 4.x 编写的?
  • 是的,我们有 400 个测试,我宁愿不切换到 junit 3。

标签: java unit-testing maven ant


【解决方案1】:

好的 - 根据您的评论,您希望能够使用 Maven Antrun 插件运行 JUnit 4.x 测试,但它依赖于 JUnit 3.x。

pom 中的插件部分允许您指定依赖项,这将覆盖插件默认具有的依赖项 - 它在 POM Reference - plugins 中进行了描述

在你的情况下,这意味着你最终会得到一个看起来像这样的插件:

<plugin>

  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>

  <dependencies>

    <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant-junit</artifactId>
      <version>1.8.4</version>
    </dependency>

  </dependencies>

  <executions>

    <execution>

      <id>test</id>
      <phase>test</phase>

      <configuration>
        <target>

          <property name="reports.tests" value="${basedir}/target/ant-test-reports"/>
          <property name="compile.classpath" refid="maven.compile.classpath"/>
          <property name="test.classpath" refid="maven.test.classpath"/>

          <mkdir dir="${reports.tests}"/>

          <junit printsummary="yes" haltonfailure="yes">

            <classpath>
              <pathelement path="${compile.classpath}"/>
              <pathelement path="${test.classpath}"/>
            </classpath>

            <formatter type="plain"/>

            <batchtest fork="yes" todir="${reports.tests}">

              <fileset dir="${basedir}/src/test/java">
                <include name="**/*Test.java"/>
              </fileset>

            </batchtest>

          </junit>

        </target>

      </configuration>

      <goals>
        <goal>run</goal>
      </goals>

    </execution>

  </executions>

</plugin>

您可能想要禁用 Surefire 插件以阻止它运行您的测试以及 ANT,可以这样完成:

<plugin>

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

  <configuration>
    <skipTests>true</skipTests>
  </configuration>

</plugin>

【讨论】:

  • 当我尝试使用此解决方案运行时,我收到以下错误“未找到类 org.apache.tools.ant.taskdefs.optional.junit.JUnitTask。”
  • 抱歉,我输入了错误的依赖项。我刚刚使用 JUnit 4.x 测试尝试了上述(使用正确的依赖项),它可以工作:-)
  • 谢谢尼克。我能够通过使用 ant build.xml 而不是内联配置来启动 junit 任务,否则这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多