【问题标题】:How to print string messages using JUnit even if the test method is passing?即使测试方法通过,如何使用 JUnit 打印字符串消息?
【发布时间】:2014-11-12 11:04:27
【问题描述】:

我想为通过的测试用例打印字符串消息。我可以使用fail() 方法打印失败案例的消息。如何打印通行证信息?

public class TestCaseTesting extends TestCase {

@Test
public void testFailTesting() {     

fail("Fail message");
}

@Test
public void testpassTesting() {     
    //what method i need to write here to show message of pass cases
//fail("Fail message");
    }

}

build.xml 的代码

<target name="generate-report">
    <junitreport todir="${reports}">
        <fileset dir="${reports}/raw/">
            <include name="TEST-*.xml" />
        </fileset>
        <report format="noframes" todir="${reports}\html\" />
    </junitreport>
</target>

【问题讨论】:

  • 打印消息以便它们出现在准确的位置?如果您从控制台运行测试,打印到标准输出的消息将出现在测试输出中。
  • 我正在生成 html 报告,在该报告中我想显示通过案例的消息。 stdout 对控制台很好,但我想在报告中打印消息。当我们使用 fail() 方法显示失败消息时,是否有任何传递案例消息的方法。
  • 您使用什么来生成 HTML 报告?你用什么来执行测试?
  • 我正在使用 Ant 脚本生成报告

标签: junit junit3


【解决方案1】:

所以,我一直在寻找这个问题的答案,它暗示了如何通过更改 ant 脚本中的 junit-frames.xsl 来自定义您的 html 报告以包含通过测试。为了通过测试,您可以通过在以下代码中的默认 junit-frames.xsl 中创建新模板来显示消息。

        <xsl:when test="failure">
            <td>Failure</td>
            <td><xsl:apply-templates select="failure"/></td>
        </xsl:when>
        <xsl:when test="error">
            <td>Error</td>
            <td><xsl:apply-templates select="error"/></td>
        </xsl:when>
        <xsl:when test="skipped">
            <td>Skipped</td>
            <td><xsl:apply-templates select="skipped"/></td>
        </xsl:when>
        <xsl:otherwise>
            <td>Success</td>
            <td><xsl:apply-templates select="success"/></td>
        </xsl:otherwise>

就在下面。

<xsl:template match="failure">
     <xsl:call-template name="display-failures"/>
</xsl:template>

<xsl:template match="error">
    <xsl:call-template name="display-failures"/>
</xsl:template>

<xsl:template match="skipped">
    <xsl:call-template name="display-failures"/>
</xsl:template>

<xsl:template match="success">
    <xsl:call-template name="display-failures"/>
</xsl:template>

对显示失败使用相同的模板将打印出通过测试的消息。此外,请确保您的 build.xml 调用自定义的 junit-frames.xsl 而不是库中的那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2011-06-19
    • 1970-01-01
    • 2016-01-16
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多