【问题标题】:How to get Boost Unit Test to show filename on JUnit testcase attribute?如何让 Boost 单元测试在 JUnit 测试用例属性上显示文件名?
【发布时间】:2023-01-11 18:27:37
【问题描述】:

我知道 Boost 可以看到文件名,因为当您将报告者级别设置为详细并将记录器级别设置为全部时,会显示文件名(甚至是行号)。

例如,如果我调用测试可执行文件

$ ./test --log_level=all --report_level=detailed

我得到

Running 1 test case...
Entering test module "simple_class"
/path/to/test_constructor.cpp(28): Entering test suite "simple_class_constructor"
/path/to/test_constructor.cpp(29): Entering test case "default"
/path/to/test_constructor.cpp(30): info: check 'no exceptions thrown by simple_class()' has passed
/path/to/test_constructor.cpp(29): Leaving test case "default"; testing time: 2909us
Leaving test module "simple_class"; testing time: 2955us

Test module "simple_class" has passed with:
  1 test case out of 1 passed
  1 assertion out of 1 passed

  Test suite "simple_class_constructor" has passed with:
    1 test case out of 1 passed
    1 assertion out of 1 passed

    Test case "bool_card_constructor/default" has passed with:
      1 assertion out of 1 passed

但是如果我像这样调用可执行文件

$ ./test --log_level=all --log_format=JUNIT --report_level=no

我得到

<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="1" skipped="0" errors="0" failures="0" id="0" name="simple_class" time="0.002691">
<testcase assertions="1" classname="simple_class_constructor" name="default" time="0.002217">
<system-out><![CDATA[INFO:
- file   : test_constructor.cpp
- line   : 30
- message: check 'no exceptions thrown by simple_class()' has passed

]]></system-out>
</testcase>
</testsuite>

在最后一种格式中,您可以看到信息位于 system-out 标记内,但我希望它位于 testcase 属性中。

问题

  • 有没有办法在 Boost Unit Test 的 JUnit 记录器的 testcase 属性中获取测试套件/用例所在的文件名?

  • 如果在 Boost 单元测试中不可能,是否可以通过 shell 脚本或类似的方式进行“更正”,如何进行?

【问题讨论】:

    标签: c++ unit-testing junit boost


    【解决方案1】:

    您可以与 XML 报告器的输出进行交叉引用:

    build/sotest --log_level=all --log_format=JUNIT --report_level=no
    
    <?xml version="1.0" encoding="UTF-8"?>
    <testsuite tests="1" skipped="0" errors="0" failures="1" id="0" name="sotest" time="0.000131">
    <testcase assertions="1" classname="Demo" name="Foo" time="0.000114">
    <failure message="failure" type="assertion error"><![CDATA[
    ASSERTION FAILURE:
    - file   : test.cpp
    - line   : 8
    - message: check 1==2 has failed
    
    ]]></failure><system-err><![CDATA[Failures detected in:
    - test case: Demo/Foo
    - file: test.cpp
    - line: 7
    ]]></system-err>
    </testcase>
    </testsuite>
    
    build/sotest --log_level=all --log_format=XML --report_level=no
    
    <TestLog><TestSuite name="sotest"><TestSuite name="Demo" file="/home/sehe/Projects/stackoverflow/test.cpp" line="5"><TestCase name="Foo" file="/home/sehe/Projects/stackoverflow/test.cpp" line="7"><Error file="/home/sehe/Projects/stackoverflow/test.cpp" line="8"><![CDATA[check 1==2 has failed]]></Error><TestingTime>104</TestingTime></TestCase></TestSuite></TestSuite></TestLog>
    

    事实上,XML 输出可能正是您所需要的!

    演示

    让我们使用这个简单的测试:

    #define BOOST_TEST_MODULE sotest
    #include <boost/test/unit_test.hpp>
    #include <boost/test/included/unit_test_framework.hpp>
    
    BOOST_AUTO_TEST_SUITE(SuiteA)
    BOOST_AUTO_TEST_CASE(Foo) { BOOST_CHECK(1==2); }
    BOOST_AUTO_TEST_SUITE_END()
    
    BOOST_AUTO_TEST_SUITE(SuiteB)
    BOOST_AUTO_TEST_CASE(Foo) { BOOST_CHECK(2==3); }
    BOOST_AUTO_TEST_SUITE_END()
    

    要使用 XSLT 关联两者,请将两者写入单独的文件,例如:

    sotest --logger=XML,all,b.xml:JUNIT,all,a.xml
    

    现在使用 saxon-he:

    java -jar /home/sehe/custom/Saxon/saxon-he-11.3.jar -xsl:merge.xslt -s:a.xml
    

    merge.xslt 是我想出的一个快速而肮脏的样式表:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="2.0">
      
      <xsl:output method="xml" indent="yes"/>
      
      <xsl:param name="xmlreport_filename" select="'b.xml'"/>
      <xsl:variable name="xmlreport" select="document($xmlreport_filename)"/>
      
      <!--<xsl:key name="k1" match="TestLog/TestSuite/TestSuite/TestCase" use="@name"/>-->
      <xsl:key name="k1" match="TestCase" use="@name"/>
      
      <xsl:template match="testsuite/testcase">
        <xsl:element name="{local-name()}">
          <xsl:variable name="expectedclass" select="@classname"/>
          <xsl:copy-of select="@*"/>
          <!--extra attributes-->
          <xsl:for-each select="key('k1', @name, $xmlreport)">
            <xsl:if test="..[@name=$expectedclass]">
              <xsl:copy-of select="@file|@line"/>
            </xsl:if>
          </xsl:for-each>
          <!--annotating with elements, demonstration-->
          <xsl:for-each select="key('k1', @name, $xmlreport)">
            <annotation>
              <xsl:choose>
                <xsl:when test="..[@name=$expectedclass]">
                  <xsl:copy-of select=".."/>
                </xsl:when>
                <xsl:otherwise>
                  <warning reason="mismatched">
                    <xsl:attribute name='expectedclass'> <xsl:value-of select='$expectedclass'/> </xsl:attribute>
                    <xsl:attribute name='actualclass'> <xsl:value-of select='../@name'/> </xsl:attribute>
                    <!--<xsl:copy-of select=".."/>-->
                  </warning>
                </xsl:otherwise>
              </xsl:choose>
            </annotation>
          </xsl:for-each>
          <xsl:copy-of select="*"/>
        </xsl:element>
      </xsl:template>
      
      <xsl:template match="testsuite">
          <xsl:element name="{local-name()}">
              <xsl:apply-templates/>
          </xsl:element>
      </xsl:template>
    
    </xsl:stylesheet>
    

    这有输出

    <?xml version="1.0" encoding="UTF-8"?>
    <testsuite>
       <testcase assertions="1"
                  classname="SuiteA"
                  name="Foo"
                  time="0.000107"
                  file="/home/sehe/Projects/stackoverflow/test.cpp"
                  line="6">
          <annotation>
             <TestSuite name="SuiteA"
                         file="/home/sehe/Projects/stackoverflow/test.cpp"
                         line="5">
                <TestCase name="Foo"
                           file="/home/sehe/Projects/stackoverflow/test.cpp"
                           line="6">
                   <Error file="/home/sehe/Projects/stackoverflow/test.cpp" line="6">check 1==2 has failed</Error>
                   <TestingTime>107</TestingTime>
                </TestCase>
             </TestSuite>
          </annotation>
          <annotation>
             <warning reason="mismatched" expectedclass="SuiteA" actualclass="SuiteB"/>
          </annotation>
          <failure message="failure" type="assertion error">
    ASSERTION FAILURE:
    - file   : test.cpp
    - line   : 6
    - message: check 1==2 has failed
    
    </failure>
          <system-err>Failures detected in:
    - test case: SuiteA/Foo
    - file: test.cpp
    - line: 6
    </system-err>
       </testcase>
       <testcase assertions="1"
                  classname="SuiteB"
                  name="Foo"
                  time="9.2e-05"
                  file="/home/sehe/Projects/stackoverflow/test.cpp"
                  line="10">
          <annotation>
             <warning reason="mismatched" expectedclass="SuiteB" actualclass="SuiteA"/>
          </annotation>
          <annotation>
             <TestSuite name="SuiteB"
                         file="/home/sehe/Projects/stackoverflow/test.cpp"
                         line="9">
                <TestCase name="Foo"
                           file="/home/sehe/Projects/stackoverflow/test.cpp"
                           line="10">
                   <Error file="/home/sehe/Projects/stackoverflow/test.cpp" line="10">check 2==3 has failed</Error>
                   <TestingTime>92</TestingTime>
                </TestCase>
             </TestSuite>
          </annotation>
          <failure message="failure" type="assertion error">
    ASSERTION FAILURE:
    - file   : test.cpp
    - line   : 10
    - message: check 2==3 has failed
    
    </failure>
          <system-err>Failures detected in:
    - test case: SuiteB/Foo
    - file: test.cpp
    - line: 10
    </system-err>
       </testcase>
    </testsuite>
    

    警告

    请注意,这恰好适用于简单示例,您将必须测试/适应您的真实测试输出。

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 2022-07-14
      • 1970-01-01
      • 2012-04-29
      • 2019-04-24
      • 1970-01-01
      • 2012-01-08
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多