如何展示XML,利用XSLT展示CATCH2单元测试报告的一次实践

1 篇文章 0 订阅
1 篇文章 0 订阅

在网上搜索只能找到将gtest单元测试的报告转换为HTML,没有catch2的模板。

所以只能参考gtest的xslt模板。参考链接:https://blog.csdn.net/Neil4/article/details/104484792

整理后相应的catch2的xslt模板如下:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
        <xsl:template match="/">
        <html>
        <head>
                <title>Unit Test Results</title>
                <style type="text/css">
                        td#passed {
                                color: green;
                                font-weight: bold;
                        }
                        td#failed {
                                color: red;
                                font-weight: bold;
                        }
                        <!-- borrowod from here http://red-team-design.com/practical-css3-tables-with-rounded-corners/ -->
                        body {
                                width: 80%;
                                margin: 40px auto;
                                font-family: 'trebuchet MS', 'Lucida sans', Arial;
                                font-size: 14px;
                                color: #444;
                        }
                        table {
                                *border-collapse: collapse; /* IE7 and lower */
                                border-spacing: 0;
                                width: 100%;
                        }
                        .bordered {
                                border: solid #ccc 1px;
                                -moz-border-radius: 6px;
                                -webkit-border-radius: 6px;
                                border-radius: 6px;
                                -webkit-box-shadow: 0 1px 1px #ccc; 
                                -moz-box-shadow: 0 1px 1px #ccc; 
                                box-shadow: 0 1px 1px #ccc;         
                        }
                        .bordered tr:hover {
                                background: #fbf8e9;
                                -o-transition: all 0.1s ease-in-out;
                                -webkit-transition: all 0.1s ease-in-out;
                                -moz-transition: all 0.1s ease-in-out;
                                -ms-transition: all 0.1s ease-in-out;
                                transition: all 0.1s ease-in-out;     
                        }    
                        .bordered td, .bordered th {
                                border-left: 1px solid #ccc;
                                border-top: 1px solid #ccc;
                                padding: 10px;
                                text-align: left;    
                        }
                        .bordered th {
                                background-color: #dce9f9;
                                background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
                                background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
                                background-image:    -moz-linear-gradient(top, #ebf3fc, #dce9f9);
                                background-image:     -ms-linear-gradient(top, #ebf3fc, #dce9f9);
                                background-image:      -o-linear-gradient(top, #ebf3fc, #dce9f9);
                                background-image:         linear-gradient(top, #ebf3fc, #dce9f9);
                                -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset; 
                                -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;  
                                box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;        
                                border-top: none;
                                text-shadow: 0 1px 0 rgba(255,255,255,.5); 
                        }
                        .bordered td:first-child, .bordered th:first-child {
                                border-left: none;
                        }
                        .bordered th:first-child {
                                -moz-border-radius: 6px 0 0 0;
                                -webkit-border-radius: 6px 0 0 0;
                                border-radius: 6px 0 0 0;
                        }
                        .bordered th:last-child {
                                -moz-border-radius: 0 6px 0 0;
                                -webkit-border-radius: 0 6px 0 0;
                                border-radius: 0 6px 0 0;
                        }
                        .bordered th:only-child{
                                -moz-border-radius: 6px 6px 0 0;
                                -webkit-border-radius: 6px 6px 0 0;
                                border-radius: 6px 6px 0 0;
                        }
                        .bordered tr:last-child td:first-child {
                                -moz-border-radius: 0 0 0 6px;
                                -webkit-border-radius: 0 0 0 6px;
                                border-radius: 0 0 0 6px;
                        }
                        .bordered tr:last-child td:last-child {
                                -moz-border-radius: 0 0 6px 0;
                                -webkit-border-radius: 0 0 6px 0;
                                border-radius: 0 0 6px 0;
                        }
                </style>
        </head>
        <body>
                <xsl:apply-templates/>
        </body>
        </html>

</xsl:template>

<xsl:template match="Group">
        <h1>Unit Test Run <xsl:value-of select="@timestamp"/></h1>
        <p>
                Executed <b><xsl:value-of select="count(TestCase)"/></b> test cases
                <b><xsl:value-of select="OverallResults/@successes"/></b> test cases success.
                <b><xsl:value-of select="OverallResults/@failures"/></b> test cases failed.
        </p>
        <xsl:apply-templates/>
</xsl:template>

<xsl:template match="TestCase">
        <h2><xsl:value-of select="@name"/></h2>

        <table class="bordered">
                <tr><th style="width:30%">SectionName(result:<xsl:value-of select="OverallResult/@success"/>) </th>
                        <th>Successes</th>
                        <th>Failures</th>
                </tr>
                <xsl:for-each select="Section">
                <tr>
                        <td><xsl:value-of select="@name"/></td>
                        <td><xsl:value-of select="OverallResults/@successes"/></td>
                        <td><xsl:value-of select="OverallResults/@failures"/></td>
                </tr>
                </xsl:for-each>
        </table>
</xsl:template>
</xsl:stylesheet>

首先执行命令: catch2UnitTest -r xml -o catch2.xml

然后执行:xsltproc catch2html.xslt catch2.xml > catch2.html

最后利用jenkins的publish html report插件,展示效果如下:

 

catch2.html

附上catch2.xml供大家参考:

<?xml version="1.0" encoding="UTF-8"?>
<Catch name="CsMainUnitTest">
  <Group name="CsMainUnitTest">
    <TestCase name="AES256Test" tags="[AES_256_DECRYPT]" filename="AES256Test.cpp" line="11">
      <Section name="ParseReqArmingMsgFromDev" filename="AES256Test.cpp" line="25">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="ParseError" filename="AES256Test.cpp" line="50">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="TransferToSocketMsg" filename="AES256Test.cpp" line="81">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="ForceTransfer" filename="AES256Test.cpp" line="110">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <OverallResult success="true"/>
    </TestCase>
    <TestCase name="CHECK_BUFFER_OVER_FLOW" tags="[check]" filename="CheckBuffOverFlowTest.cpp" line="9">
      <Section name="Overflow" filename="CheckBuffOverFlowTest.cpp" line="13">
        <OverallResults successes="2" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="Not overflow" filename="CheckBuffOverFlowTest.cpp" line="25">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <OverallResult success="true"/>
    </TestCase>
    <TestCase name="MsgHandleTest" tags="[BuildCommonMsg]" filename="MsgHandleTest.cpp" line="10">
      <Section name="Only type" filename="MsgHandleTest.cpp" line="16">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="RegisterFace" filename="MsgHandleTest.cpp" line="26">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="ModifyFace" filename="MsgHandleTest.cpp" line="44">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="DeleteFace" filename="MsgHandleTest.cpp" line="62">
        <OverallResults successes="1" failures="0" expectedFailures="0"/>
      </Section>
      <OverallResult success="true"/>
    </TestCase>
    <TestCase name="ThirdPartyTest" tags="[DaoTest]" filename="ThirdPartyTest.cpp" line="42">
      <Section name="DaoGetThirdPartyInfo noresult" filename="ThirdPartyTest.cpp" line="49">
        <OverallResults successes="2" failures="0" expectedFailures="0"/>
      </Section>
      <Section name="DaoGetThirdPartyInfo exist" filename="ThirdPartyTest.cpp" line="56">
        <OverallResults successes="2" failures="0" expectedFailures="0"/>
      </Section>
      <OverallResult success="true"/>
    </TestCase>
    <OverallResults successes="15" failures="0" expectedFailures="0"/>
  </Group>
  <OverallResults successes="15" failures="0" expectedFailures="0"/>
</Catch>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值