【问题标题】:How to Control JUnit & Ant flow and formatting如何控制 JUnit & Ant 流程和格式
【发布时间】:2013-12-26 23:36:30
【问题描述】:

我想通过 Ant 进行设置,以运行我的 JUnit 测试并报告摘要级别和单个类级别的内容。我想让所有的测试都运行,即使有一些错误,但如果有任何错误,整个构建都会失败。

这是我目前在 build.xml 文件中的内容:

  <target name = "test" depends="compiletest" description="run unit tests">
    <junit haltonfailure="yes">
      <classpath path="${buildtest}">
        <path refid="test.classpath"/>
      </classpath>
      <batchtest fork="yes">
        <formatter type="brief" usefile="false"/>
        <fileset dir="${test}">
          <include name="**/*Test.java"/>
          <include name="**/Test*.java"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

假设我有两个测试类,每个测试类都有 1 个错误,它会产生以下输出:

Buildfile: /opt/project/build/build.xml

init:

compile:

compiletest:

test:
    [junit] Testsuite: com.company.foo.TestQuoteParser
    [junit] Tests run: 3, Failures: 1, Errors: 0, Time elapsed: 0.013 sec
    [junit]
    [junit] Testcase: thisShouldThrowException(com.company.foo.TestQuoteParser):    FAILED
    [junit] Expected exception: java.lang.RuntimeException
    [junit] junit.framework.AssertionFailedError: Expected exception: java.lang.RuntimeException
    [junit]
    [junit]

BUILD FAILED
/opt/project/build/build.xml:88: Test com.company.foo.TestQuoteParser failed

Total time: 1 second

我希望执行每个测试类,无论之前的测试类是通过还是失败,但总体构建仍然失败。似乎我可以在第一次失败/错误时使构建失败,或者让所有测试运行但构建成功。我还想在最后打印一份总体摘要。像这样的:

Buildfile: /opt/project/build/build.xml

init:

compile:

compiletest:

test:
    [junit] Testsuite: com.company.foo.TestQuoteParser
    [junit] Tests run: 3, Failures: 1, Errors: 0, Time elapsed: 0.013 sec
    [junit]
    [junit] Testcase: thisShouldThrowException(com.company.foo.TestQuoteParser):    FAILED
    [junit] Expected exception: java.lang.RuntimeException
    [junit] junit.framework.AssertionFailedError: Expected exception: java.lang.RuntimeException
    [junit]
    [junit]

    [junit] Testsuite: com.company.foo.TestEntityParser
    [junit] Tests run: 6, Failures: 1, Errors: 0, Time elapsed: 0.023 sec
    [junit]
    [junit] Testcase: thisShouldThrowException(com.company.foo.TestEntityParser):   FAILED
    [junit] Expected exception: java.lang.RuntimeException
    [junit] junit.framework.AssertionFailedError: Expected exception: java.lang.RuntimeException
    [junit]
    [junit] 
    [junit] Summary: com.company
    [junit] Tests run: 9, Failures: 2, Errors: 0, Time elapsed: 0.038 sec

BUILD FAILED
/opt/project/build/build.xml:88: 2 failures 
Total time: 1 second

我宁愿将所有这些都显示在屏幕上,而不必查看文件(甚至是漂亮的 HTML 报告文件),因为工作将通过 SSH 完成。有没有那么容易做到的?

【问题讨论】:

  • 您是否考虑过使用 Jenkins 来构建您的项目。我知道 Jenkins 会运行你所有的测试用例,即使其中一些容易出错
  • Here is (link) 如何继续执行测试但最终构建失败。

标签: java unit-testing ant junit


【解决方案1】:

设置 JUnit 时,将 haltonerrorhaltonfailure 都设置为 false。这样,JUnit 将继续处理。然后,还要设置errorpropertyfailureproperty 参数。像往常一样运行 JUnit。如果要生成报告,请生成报告。如果错误属性或失败属性报告错误,则使用&lt;fail/&gt; 任务使构建失败:

<target name = "test"
    depends="compiletest"
    description="run unit tests">
    <junit haltonfailure="false"
        haltonerror="false"
        failureproperty="failure.in.testing"
        errorproperty="error.in.testing">
        <classpath path="${buildtest}">
            <path refid="test.classpath"/>
        </classpath>
        <batchtest fork="yes">
            <formatter type="brief" usefile="false"/>
            <fileset dir="${test}">
                <include name="**/*Test.java"/>
                <include name="**/Test*.java"/>
            </fileset>
        </batchtest>
    </junit>

    <!-- Now that everything has run, we'll check the error.in.testing -->
    <!-- and the failure.in.testing properties to see if there were errors -->
    <!-- of failed tests. -->
    <fail message="JUnit had at least one error in a test${line.break}${error.in.testing}">
        <condition>
            <length string="${error.in.testing}"
                when="greater" length="0"/>
        </condition>
    </fail>
    <fail message="JUnit had at least one failed test${line.break}${failure.in.testing}">
        <condition>
            <length string="${failure.in.testing}"
                when="greater" length="0"/>
        </condition>
    </fail>
</target>

我可能已经能够使用isset 条件:

    <fail message="JUnit had at least one error in a test${line.break}${error.in.testing}">
        <condition>
            <isset property"error.in.testing"/>
        </condition>
    </fail>
    <fail message="JUnit had at least one failed test${line.break}${failure.in.testing}">
        <condition>
            <isset property"failure.in.testing"/>
        </condition>
    </fail>

但是,我不确定这些属性是未设置还是仅等于空字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2019-04-13
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多