【问题标题】:Running "pure" JUnit 4 tests using ant使用 ant 运行“纯”JUnit 4 测试
【发布时间】:2009-03-11 17:08:23
【问题描述】:

我们已迁移到 JUnit 4 和 ant 1.7

测试在 Eclipse 中运行良好,但在使用 ant 运行测试时会忽略注释。

根据Ant junit任务documentation

它也适用于 JUnit 4.0,包括仅使用注释而不使用 JUnit4TestAdapter 的“纯”JUnit 4 测试。

但是文档没有详细说明它应该如何配置。

junit 任务需要什么特殊设置吗?我错过了什么吗? 我们有扩展 TestCase(即 3.8 样式)的测试和“纯”Junit 4 测试,这可能是问题吗?

【问题讨论】:

    标签: ant junit junit4


    【解决方案1】:

    我在 Ant 中使用纯 JUnit4 测试。

    这是我的构建文件中有趣的部分:

    <junit printsummary="yes" haltonfailure="yes">
        <formatter type="xml"/>
        <classpath refid="path.test"/>
        <batchtest fork="yes" todir="${dir.report.unittests.xml}">
            <fileset dir="src">
                <include name="**/*Test*.java"/>
            </fileset>
        </batchtest>
    </junit>
    

    确保您在 Ant 的 lib 目录中拥有最新版本的 junit.jar 文件。据我所知,所需版本是随 ant 1.7 或更高版本一起提供的......

    【讨论】:

    • 即使 JUnit 4 不需要,也无法避免必须为测试类使用命名模式(如“*Test.java”)?
    • @jonik 您不必使用命名模式,但它可以帮助您避免针对您可能拥有的任何其他类运行 JUnit,这些类不包含与您的测试在同一包中的测试类。像这样的类会在 JUnit 输出中产生错误。
    • 如果您的实用程序类在开始时碰巧有 Test 但不是测试类怎么办?通常,“使用 JUnit 4”意味着“@Test 注释标记测试,而不是类名”。应该可以包含所有 .java 文件并让它只运行其中包含测试的文件。
    • @Trejkaz 添加了一个新参数,它将为您过滤掉非测试类。它位于answer below.
    【解决方案2】:

    Ant 默认附带一个 JUnit 3 版本。 JUnit 3 不支持测试注释。

    要使用 junit 任务中的 JUnit 4 注释,请确保在 junit 任务的嵌套类路径元素中提供 JUnit 4 jar 的位置(请参阅this entry in the ant FAQ)。

    <junit showoutput="yes" fork="true">
        <classpath>
            <!-- The location of the JUnit version that you want to use -->
            <pathelement location="lib/junit-4.9b1.jar"/>
        </classpath>
    
        <formatter type="plain" usefile="false" />
    
        <batchtest>
            <fileset dir="${tests.dir}"/>
        </batchtest>
    </junit>
    

    这是覆盖 ANT_HOME/lib 中的 ant-junit.jar 的更好解决方案,因为这意味着您可以将 JUnit jar 与代码一起保存在源代码控制中,从而直接升级到更高版本。

    请注意,虽然我没有在上面的文件集中指定任何包含模式,但这确实意味着 junit 任务将尝试针对该目录结构中的所有类运行 JUnit,这可能导致包含许多不包含的类'不包含任何测试,具体取决于您构建源文件的方式。

    【讨论】:

    • 我在 Debian 中遇到了完全相同的问题:ant 1.8.0,junit 4.10,作为 junit 任务的一部分添加的 junit-4.10.jar 的类路径,但注释 @Task 不是认可,仅重命名以 test* 开头的方法才有效...
    • @MenelaosPerdikeas 你的意思是@Task?我希望您使用 @Test 作为注释。假设您正在使用@Test 尝试使用 -v 标志运行 ant,junit 任务的输出将显示一个类路径。然后,您可以确保您添加到任务的 junit-4.10.jar 类路径存在。我发现 -v 在尝试诊断此问题时非常有用。
    • 除了fork="true" 选项外,我完全有这个&lt;junit&gt; 元素。添加该选项为我解决了这个问题。
    【解决方案3】:

    您终于只能通过 ant 1.9.3+ 中添加的 skipNonTests 参数来查找和执行测试!

    这是上面接受的答案中的代码 sn-p(除了新的 skipNonTests 参数和去掉文件名要求中的“测试”):

    <junit printsummary="yes" haltonfailure="yes">
        <formatter type="xml"/>
        <classpath refid="path.test"/>
        <batchtest skipNonTests="true" fork="yes" todir="${dir.report.unittests.xml}">
            <fileset dir="src">
                <include name="**/*.java"/>
            </fileset>
        </batchtest>
    </junit>
    

    【讨论】:

    • 是的,很高兴他们终于添加了这个。现在,如果他们能够修复 Ant 中的所有其他问题,Ant 可能实际上会成为一个不错的工具。哈哈。
    【解决方案4】:

    这发生在我身上,这是因为我既使用了注解又扩展了 TestCase。

    public class TestXXX extends TestCase {
    
        @Test
        public void testSimpleValidCase() {
            // this was running
        }
    
        @Test
        public void simpleValidCase() {
            // this wasn't running
        }
    }
    

    当您扩展 TestCase 时,您假设采用 JUnit3 样式,因此忽略 JUnit4 注释。

    解决办法是停止扩展TestCase。

    【讨论】:

      【解决方案5】:

      验证您的类路径定义... 这解决了我的问题。

      <path id="classpath" description="Classpath do Projeto">
          <fileset dir="${LIB}">
              <include name="**/*.jar" />
              <exclude name="**/.SVN/*.*"/>
          </fileset>
      </path>
      

      【讨论】:

        【解决方案6】:

        这是我的通用 ant 脚本的相关部分...不确定这是否对您有帮助..

         <junit fork="true"
                forkmode="once"
                haltonfailure="false"
                haltonerror="false"
                failureproperty="tests.failures"
                errorproperty="tests.errors"
                includeantruntime="true"
                showoutput="true"
                printsummary="true">
             <classpath>
                 <path refid="path-id.test.classpath.run"/>
             </classpath>
        
             <formatter type="xml"/>
        
             <batchtest fork="yes"
                        todir="${dir.build.testresults}">
                 <fileset dir="${dir.src.tests}">
                     <include name="**/*Test.java"/>
                 </fileset>
             </batchtest>
         </junit>
        

        【讨论】:

          【解决方案7】:

          将此注释应用到其他类 org.junit.Ignore

          【讨论】:

            【解决方案8】:

            我还尝试在没有 JUnit4TestAdapter 的情况下使用 JUnit 4.0 进行测试,即没有方法
            公共静态junit.framework.Test套件(){ 返回新的 JUnit4TestAdapter(SomeTestClass.class); }

            我使用 ant 1.9.4。 运行 ant test verbose (ant -v) 显示

            [junit] 在同一个虚拟机中运行多个测试
            [junit] 隐式添加/usr/share/java/junit.jar:/usr/sharejava/ant-launcher.jar:/usr/share/java/ant.jar:/usr/share/java/ant/ant-junit .jar 到 CLASSPATH

            啊哈,但仍然有一些 ant-junit-task。 另外下载这个节目 /usr/share/java/ant/ant-junit4.jar 不是隐式添加的。 我只是明确地添加了它:

            <junit printsummary="yes" 
                fork="yes" 
                forkmode="once"
                maxmemory="1023m" 
                showoutput="no">
                ...
               <classpath>
                 <pathelement path="...:${junitJar}:${hamcrestJar}:/usr/share/java/ant/ant-junit4.jar" />
               </classpath>
            ...
            </junit>
            

            它奏效了。没有:没有。 我知道这个解决方案一点也不漂亮……

            【讨论】:

              【解决方案9】:

              我最终做的是将 Ant 添加到任务使用的定义之一中>。等等。

              【讨论】:

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