【问题标题】:Not able to run all junit tests in a category with ant无法使用 ant 运行类别中的所有 junit 测试
【发布时间】:2016-07-21 08:54:55
【问题描述】:

背景

我正在尝试创建一个 Ant 脚本:

  • 编译源文件
  • 编译junit测试
  • 运行特定类别的 junit 测试

到目前为止,我所做的大部分工作都来自this question

到目前为止,这是我的 ant 脚本中的内容:

<?xml version="1.0"?>
<project name="junit_tests">
  <property name="build-main" location="build/main"/>
  <property name="build-test" location="build/main"/>
  <property name="src-test" value="java/test" />
  <property name="src-com" value="java/com" />
  <property name="src-lib" value="webapp/WEB-INF/lib" />
  <property name="test-lib" value="java/test/lib" />
  <path id="classpath">
    <fileset dir="${src-lib}" includes="*.jar"/>
    <fileset dir="${test-lib}" includes="*.jar" />
  </path>
  <path id="classpath.test">
    <path refid="classpath"/>
    <pathelement location="${build}"/>
  </path>
  <target name="init">
    <mkdir dir="${build-main}" />
    <mkdir dir="${build-test}" />
  </target>

  <target name="compileSrc" depends="init" description="compile src files ">
    <javac debug="on" destdir="${build-main}" encoding="UTF-8" fork="true" memorymaximumsize="1024m" includeantruntime="true">
      <src path="${src-com}" />
      <classpath refid="classpath"/>
    </javac>
    <javac debug="on" destdir="${build-main}" encoding="UTF-8" fork="true" memorymaximumsize="1024m" includeantruntime="true">
      <src path="${src-test}" />
      <classpath refid="classpath"/>
    </javac>
  </target>


  <!-- Test and build all files  -->
  <!-- To run this: use "ant" (default) or "ant run" -->
  <target name="run" depends="compileSrc">
    <junit printsummary="on" haltonfailure="no" fork="true">
        <classpath>
          <path refid="classpath.test" />
          <pathelement location="${build-test}"/>
        </classpath>
        <formatter type="xml" />
        <batchtest>
            <fileset dir="${build-main}" includes="**/NonDBTest.class" />
        </batchtest>
    </junit>
  </target>
</project>

junit 任务的输出

运行: [junit] 警告:在 junit 的路径中检测到多个版本的 ant [junit] jar:file:/usr/local/Cellar/ant/1.9.7/libexec/lib/ant.jar!/org/apache/tools/ant/Project.class [junit] 和 jar:file:/Users/arnab/work/my_project/webapp/WEB-INF/lib/ant.jar!/org/apache/tools/ant/Project.class [junit] 运行 test.NonDBTest [junit] 测试运行:2,失败:0,错误:2,跳过:0,经过时间:0.178 秒

构建成功总时间:37 秒

问题 NonDBTest 这个类别中的测试类总共有 7 个测试,所以 junit 显然没有运行所有这些。

NonDBTest.java 只是一个没有方法的接口,这就是我的测试类的样子

@Category(NonDBTest.class)
public class MyTestClass extends AbstractTestCase{
....
    
    @Test
    public void testA(){ ... }
   
    @Test
    public void testB(){ ... }
   
    @Test
    public void testC(){ ... }
   
    @Test
    public void testD(){ ... }
   
    @Test
    public void testE(){ ... }

}

问题 我究竟做错了什么?我尝试将NonTestDB 更改为一个类。我已经尝试实现该接口,但到目前为止没有任何效果。如何让 junit 运行 NonTestDB 类别中的所有类?我错过了什么吗?

【问题讨论】:

  • 我已经为您的链接 question 添加了一个答案,这使得通过 ant 运行特定 Category 的测试成为可能。

标签: java testing junit ant


【解决方案1】:

使用 maven 更容易使用 JUnit 类别,因为您可以通过 group 属性配置类别以运行。

我怀疑ant 中的类别是否有明确的支持,并且我认为您不能在测试任务中include 一个JUnit 类别接口(就像您似乎所做的那样)。

解决方法是还为您的类别创建一个 JUnit Suite,然后仅从 ant 目标运行该套件:

@RunWith(Categories.class)
@IncludeCategory(NonDBTest.class)
@SuiteClasses( { MyTestClass1.class, MyTestClass2.class })
public class NonDBTestSuite {
  // will run only tests annotated with NonDBTest from
  // MyTestClass1 and MyTestClass2.
}

那么你的ant配置应该表明NonDBTestSuite应该被执行。

<fileset dir="${build-main}" includes="**/NonDBTestSuite.class" />

请注意,您所指的 answer 也已定义此套件。

其中不太好的部分是您需要维护所有套件类。我对多个类别所做的是维护一个带有 all 测试类的 AllTests 套件。然后,较小的套件将在 SuiteClasses 注释中使用此 AllTests

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 2012-11-09
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多