JUnit测试

利用Ant中的junit和junitreport进行测试:
Ant中junit标签对以Test结尾的class进行测试,生成xml测试结果,然后junitreport根据 ${test.style.dir}中的样式文件junit-noframes.xsl生成html测试报告。
build.xml文件:
  1. <target name="test" depends="compile,compile-test">
  2.         <mkdir dir="test-report" />
  3.         <junit>
  4.             <classpath refid="project.classpath" />
  5.             <formatter type="xml" />
  6.             <batchtest todir="test-report">
  7.                 <fileset dir="test/classes">
  8.                     <include name="**/*Test.class" />
  9.                 </fileset>
  10.             </batchtest>
  11.         </junit>
  12.         <junitreport todir="test-report">
  13.             <fileset dir="test-report">
  14.                 <include name="TEST-*.xml" />
  15.             </fileset>
  16.             <report format="noframes" styledir="${test.style.dir}" todir="test-report" />
  17.         </junitreport>
  18.     </target>
样式文件:
  1. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  2. <xsl:output method="html" indent="yes" encoding="US-ASCII"
  3.   doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
  4. <xsl:decimal-format decimal-separator="." grouping-separator="," />
  5. <!--
  6.  The Apache Software License, Version 1.1
  7.  Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
  8.  reserved.
  9.  Redistribution and use in source and binary forms, with or without
  10.  modification, are permitted provided that the following conditions
  11.  are met:
  12.  1. Redistributions of source code must retain the above copyright
  13.     notice, this list of conditions and the following disclaimer.
  14.  2. Redistributions in binary form must reproduce the above copyright
  15.     notice, this list of conditions and the following disclaimer in
  16.     the documentation and/or other materials provided with the
  17.     distribution.
  18.  3. The end-user documentation included with the redistribution, if
  19.     any, must include the following acknowlegement:
  20.        "This product includes software developed by the
  21.         Apache Software Foundation (http://www.apache.org/)."
  22.     Alternately, this acknowlegement may appear in the software itself,
  23.     if and wherever such third-party acknowlegements normally appear.
  24.  4. The names "The Jakarta Project""Ant", and "Apache Software
  25.     Foundation" must not be used to endorse or promote products derived
  26.     from this software without prior written permission. For written
  27.     permission, please contact apache@apache.org.
  28.  5. Products derived from this software may not be called "Apache"
  29.     nor may "Apache" appear in their names without prior written
  30.     permission of the Apache Group.
  31.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  32.  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33.  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  34.  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  35.  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37.  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  38.  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  39.  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  40.  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  41.  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42.  SUCH DAMAGE.
  43.  ====================================================================
  44.  This software consists of voluntary contributions made by many
  45.  individuals on behalf of the Apache Software Foundation.  For more
  46.  information on the Apache Software Foundation, please see
  47.  <http://www.apache.org/>.
  48.  -->
  49.  
  50. <!--
  51.  
  52.  Sample stylesheet to be used with An JUnitReport output.
  53.  
  54.  It creates a non-framed report that can be useful to send via
  55.  e-mail or such.
  56.  
  57.  @author Stephane Bailliez <a href="mailto:sbailliez@apache.org"/>
  58.  @author Erik Hatcher <a href="mailto:ehatcher@apache.org"/>
  59.  
  60. -->
  61. <xsl:template match="testsuites">
  62.     <html>
  63.         <head>
  64.             <title>Unit Test Results</title>
  65.     <style type="text/css">
  66.       body {
  67.         font:normal 68% verdana,arial,helvetica;
  68.         color:#000000;
  69.       }
  70.       table tr td, table tr th {
  71.           font-size: 68%;
  72.       }
  73.       table.details tr th{
  74.         font-weight: bold;
  75.         text-align:left;
  76.         background:#a6caf0;
  77.       }
  78.       table.details tr td{
  79.         background:#eeeee0;
  80.       }
  81.       
  82.       p {
  83.         line-height:1.5em;
  84.         margin-top:0.5em; margin-bottom:1.0em;
  85.       }
  86.       h1 {
  87.         margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
  88.       }
  89.       h2 {
  90.         margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
  91.       }
  92.       h3 {
  93.         margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
  94.       }
  95.       h4 {
  96.         margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
  97.       }
  98.       h5 {
  99.         margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
  100.       }
  101.       h6 {
  102.         margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
  103.       }
  104.       .Error {
  105.         font-weight:bold; color:red;
  106.       }
  107.       .Failure {
  108.         font-weight:bold; color:purple;
  109.       }
  110.       .Properties {
  111.         text-align:right;
  112.       }
  113.       </style>
  114.       <script type="text/javascript" language="JavaScript">
  115.         var TestCases = new Array();
  116.         var cur;
  117.         <xsl:for-each select="./testsuite">      
  118.             <xsl:apply-templates select="properties"/>
  119.         </xsl:for-each>
  120.        </script>
  121.        <script type="text/javascript" language="JavaScript"><![CDATA[
  122.         function displayProperties (name) {
  123.           var win = window.open('','JUnitSystemProperties','scrollbars=1,resizable=1');
  124.           var doc = win.document.open();
  125.           doc.write("<html><head><title>Properties of " + name + "</title>");
  126.           doc.write("<style>")
  127.           doc.write("body {font:normal 68% verdana,arial,helvetica; color:#000000; }");
  128.           doc.write("table tr td, table tr th { font-size: 68%; }");
  129.           doc.write("table.properties { border-collapse:collapse; border-left:solid 1 #cccccc; border-top:solid 1 #cccccc; padding:5px; }");
  130.           doc.write("table.properties th { text-align:left; border-right:solid 1 #cccccc; border-bottom:solid 1 #cccccc; background-color:#eeeeee; }");
  131.           doc.write("table.properties td { font:normal; text-align:left; border-right:solid 1 #cccccc; border-bottom:solid 1 #cccccc; background-color:#fffffff; }");
  132.           doc.write("h3 { margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica }");
  133.           doc.write("</style>");
  134.           doc.write("</head><body>");
  135.           doc.write("<h3>Properties of " + name + "</h3>");
  136.           doc.write("<div align=/"right/"><a href=/"javascript:window.close();/">Close</a></div>");
  137.           doc.write("<table class='properties'>");
  138.           doc.write("<tr><th>Name</th><th>Value</th></tr>");
  139.           for (prop in TestCases[name]) {
  140.             doc.write("<tr><th>" + prop + "</th><td>" + TestCases[name][prop] + "</td></tr>");
  141.           }
  142.           doc.write("</table>");
  143.           doc.write("</body></html>");
  144.           doc.close();
  145.           win.focus();
  146.         }
  147.       ]]>  
  148.       </script>
  149.         </head>
  150.         <body>
  151.             <a name="top"></a>
  152.             <xsl:call-template name="pageHeader"/>  
  153.             
  154.             <!-- Summary part -->
  155.             <xsl:call-template name="summary"/>
  156.             <hr size="1" width="95%" align="left"/>
  157.             
  158.             <!-- Package List part -->
  159.             <xsl:call-template name="packagelist"/>
  160.             <hr size="1" width="95%" align="left"/>
  161.             
  162.             <!-- For each package create its part -->
  163.             <xsl:call-template name="packages"/>
  164.             <hr size="1" width="95%" align="left"/>
  165.             
  166.             <!-- For each class create the  part -->
  167.             <xsl:call-template name="classes"/>
  168.             
  169.         </body>
  170.     </html>
  171. </xsl:template>
  172.     
  173.     
  174.     
  175.     <!-- ================================================================== -->
  176.     <!-- Write a list of all packages with an hyperlink to the anchor of    -->
  177.     <!-- of the package name.                                               -->
  178.     <!-- ================================================================== -->
  179.     <xsl:template name="packagelist">   
  180.         <h2>Packages</h2>
  181.         Note: package statistics are not computed recursively, they only sum up all of its testsuites numbers.
  182.         <table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
  183.             <xsl:call-template name="testsuite.test.header"/>
  184.             <!-- list all packages recursively -->
  185.             <xsl:for-each select="./testsuite[not(./@package = preceding-sibling::testsuite/@package)]">
  186.                 <xsl:sort select="@package"/>
  187.                 <xsl:variable name="testsuites-in-package" select="/testsuites/testsuite[./@package = current()/@package]"/>
  188.                 <xsl:variable name="testCount" select="sum($testsuites-in-package/@tests)"/>
  189.                 <xsl:variable name="errorCount" select="sum($testsuites-in-package/@errors)"/>
  190.                 <xsl:variable name="failureCount" select="sum($testsuites-in-package/@failures)"/>
  191.                 <xsl:variable name="timeCount" select="sum($testsuites-in-package/@time)"/>
  192.                 
  193.                 <!-- write a summary for the package -->
  194.                 <tr valign="top">
  195.                     <!-- set a nice color depending if there is an error/failure -->
  196.                     <xsl:attribute name="class">
  197.                         <xsl:choose>
  198.                             <xsl:when test="$failureCount > 0">Failure</xsl:when>
  199.                             <xsl:when test="$errorCount > 0">Error</xsl:when>
  200.                         </xsl:choose>
  201.                     </xsl:attribute>
  202.                     <td><a href="#{@package}"><xsl:value-of select="@package"/></a></td>
  203.                     <td><xsl:value-of select="$testCount"/></td>
  204.                     <td><xsl:value-of select="$errorCount"/></td>
  205.                     <td><xsl:value-of select="$failureCount"/></td>
  206.                     <td>
  207.                     <xsl:call-template name="display-time">
  208.                         <xsl:with-param name="value" select="$timeCount"/>
  209.                     </xsl:call-template>
  210.                     </td>
  211.                 </tr>
  212.             </xsl:for-each>
  213.         </table>        
  214.     </xsl:template>
  215.     
  216.     
  217.     <!-- ================================================================== -->
  218.     <!-- Write a package level report                                       -->
  219.     <!-- It creates a table with values from the document:                  -->
  220.     <!-- Name | Tests | Errors | Failures | Time                            -->
  221.     <!-- ================================================================== -->
  222.     <xsl:template name="packages">
  223.         <!-- create an anchor to this package name -->
  224.         <xsl:for-each select="/testsuites/testsuite[not(./@package = preceding-sibling::testsuite/@package)]">
  225.             <xsl:sort select="@package"/>
  226.                 <a name="{@package}"></a>
  227.                 <h3>Package <xsl:value-of select="@package"/></h3>
  228.                 
  229.                 <table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
  230.                     <xsl:call-template name="testsuite.test.header"/>
  231.             
  232.                     <!-- match the testsuites of this package -->
  233.                     <xsl:apply-templates select="/testsuites/testsuite[./@package = current()/@package]" mode="print.test"/>
  234.                 </table>
  235.                 <a href="#top">Back to top</a>
  236.                 <p/>
  237.                 <p/>
  238.         </xsl:for-each>
  239.     </xsl:template>
  240.     
  241.     <xsl:template name="classes">
  242.         <xsl:for-each select="testsuite">
  243.             <xsl:sort select="@name"/>
  244.             <!-- create an anchor to this class name -->
  245.             <a name="{@name}"></a>
  246.             <h3>TestCase <xsl:value-of select="@name"/></h3>
  247.             
  248.             <table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
  249.               <xsl:call-template name="testcase.test.header"/>
  250.               <!--
  251.               test can even not be started at all (failure to load the class)
  252.               so report the error directly
  253.               -->
  254.                 <xsl:if test="./error">
  255.                     <tr class="Error">
  256.                         <td colspan="4"><xsl:apply-templates select="./error"/></td>
  257.                     </tr>
  258.                 </xsl:if>
  259.                 <xsl:apply-templates select="./testcase" mode="print.test"/>
  260.             </table>
  261.             <div class="Properties">
  262.                 <a>
  263.                     <xsl:attribute name="href">javascript:displayProperties('<xsl:value-of select="@package"/>.<xsl:value-of select="@name"/>');</xsl:attribute>
  264.                     Properties »
  265.                 </a>
  266.             </div>
  267.             <p/>
  268.             
  269.             <a href="#top">Back to top</a>
  270.         </xsl:for-each>
  271.     </xsl:template>
  272.     
  273.     <xsl:template name="summary">
  274.         <h2>Summary</h2>
  275.         <xsl:variable name="testCount" select="sum(testsuite/@tests)"/>
  276.         <xsl:variable name="errorCount" select="sum(testsuite/@errors)"/>
  277.         <xsl:variable name="failureCount" select="sum(testsuite/@failures)"/>
  278.         <xsl:variable name="timeCount" select="sum(testsuite/@time)"/>
  279.         <xsl:variable name="successRate" select="($testCount - $failureCount - $errorCount) div $testCount"/>
  280.         <table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
  281.         <tr valign="top">
  282.             <th>Tests</th>
  283.             <th>Failures</th>
  284.             <th>Errors</th>
  285.             <th>Success rate</th>
  286.             <th>Time</th>
  287.         </tr>
  288.         <tr valign="top">
  289.             <xsl:attribute name="class">
  290.                 <xsl:choose>
  291.                     <xsl:when test="$failureCount > 0">Failure</xsl:when>
  292.                     <xsl:when test="$errorCount > 0">Error</xsl:when>
  293.                 </xsl:choose>
  294.             </xsl:attribute>
  295.             <td><xsl:value-of select="$testCount"/></td>
  296.             <td><xsl:value-of select="$failureCount"/></td>
  297.             <td><xsl:value-of select="$errorCount"/></td>
  298.             <td>
  299.                 <xsl:call-template name="display-percent">
  300.                     <xsl:with-param name="value" select="$successRate"/>
  301.                 </xsl:call-template>
  302.             </td>
  303.             <td>
  304.                 <xsl:call-template name="display-time">
  305.                     <xsl:with-param name="value" select="$timeCount"/>
  306.                 </xsl:call-template>
  307.             </td>
  308.         </tr>
  309.         </table>
  310.         <table border="0" width="95%">
  311.         <tr>
  312.         <td style="text-align: justify;">
  313.         Note: <i>failures</i> are anticipated and checked for with assertions while <i>errors</i> are unanticipated.
  314.         </td>
  315.         </tr>
  316.         </table>
  317.     </xsl:template>
  318.     
  319.   <!--
  320.    Write properties into a JavaScript data structure.
  321.    This is based on the original idea by Erik Hatcher (ehatcher@apache.org)
  322.    -->
  323.   <xsl:template match="properties">
  324.     cur = TestCases['<xsl:value-of select="../@package"/>.<xsl:value-of select="../@name"/>'] = new Array();
  325.     <xsl:for-each select="property">
  326.     <xsl:sort select="@name"/>
  327.         cur['<xsl:value-of select="@name"/>'] = '<xsl:call-template name="JS-escape"><xsl:with-param name="string" select="@value"/></xsl:call-template>';
  328.     </xsl:for-each>
  329.   </xsl:template>
  330.     
  331. <!-- Page HEADER -->
  332. <xsl:template name="pageHeader">
  333.     <h1>Unit Test Results</h1>
  334.     <table width="100%">
  335.     <tr>
  336.         <td align="left"></td>
  337.         <td align="right">Designed for use with <a href='http://www.junit.org'>JUnit</a> and <a href='http://jakarta.apache.org/ant'>Ant</a>.</td>
  338.     </tr>
  339.     </table>
  340.     <hr size="1"/>
  341. </xsl:template>
  342. <xsl:template match="testsuite" mode="header">
  343.     <tr valign="top">
  344.         <th width="80%">Name</th>
  345.         <th>Tests</th>
  346.         <th>Errors</th>
  347.         <th>Failures</th>
  348.         <th nowrap="nowrap">Time(s)</th>
  349.     </tr>
  350. </xsl:template>
  351. <!-- class header -->
  352. <xsl:template name="testsuite.test.header">
  353.     <tr valign="top">
  354.         <th width="80%">Name</th>
  355.         <th>Tests</th>
  356.         <th>Errors</th>
  357.         <th>Failures</th>
  358.         <th nowrap="nowrap">Time(s)</th>
  359.     </tr>
  360. </xsl:template>
  361. <!-- method header -->
  362. <xsl:template name="testcase.test.header">
  363.     <tr valign="top">
  364.         <th>Name</th>
  365.         <th>Status</th>
  366.         <th width="80%">Type</th>
  367.         <th nowrap="nowrap">Time(s)</th>
  368.     </tr>
  369. </xsl:template>
  370. <!-- class information -->
  371. <xsl:template match="testsuite" mode="print.test">
  372.     <tr valign="top">
  373.         <!-- set a nice color depending if there is an error/failure -->
  374.         <xsl:attribute name="class">
  375.             <xsl:choose>
  376.                 <xsl:when test="@failures[.> 0]">Failure</xsl:when>
  377.                 <xsl:when test="@errors[.> 0]">Error</xsl:when>
  378.             </xsl:choose>
  379.         </xsl:attribute>
  380.     
  381.         <!-- print testsuite information -->
  382.         <td><a href="#{@name}"><xsl:value-of select="@name"/></a></td>
  383.         <td><xsl:value-of select="@tests"/></td>
  384.         <td><xsl:value-of select="@errors"/></td>
  385.         <td><xsl:value-of select="@failures"/></td>
  386.         <td>
  387.             <xsl:call-template name="display-time">
  388.                 <xsl:with-param name="value" select="@time"/>
  389.             </xsl:call-template>
  390.         </td>
  391.     </tr>
  392. </xsl:template>
  393. <xsl:template match="testcase" mode="print.test">
  394.     <tr valign="top">
  395.         <xsl:attribute name="class">
  396.             <xsl:choose>
  397.                 <xsl:when test="failure | error">Error</xsl:when>
  398.             </xsl:choose>
  399.         </xsl:attribute>
  400.         <td><xsl:value-of select="@name"/></td>
  401.         <xsl:choose>
  402.             <xsl:when test="failure">
  403.                 <td>Failure</td>
  404.                 <td><xsl:apply-templates select="failure"/></td>
  405.             </xsl:when>
  406.             <xsl:when test="error">
  407.                 <td>Error</td>
  408.                 <td><xsl:apply-templates select="error"/></td>
  409.             </xsl:when>
  410.             <xsl:otherwise>
  411.                 <td>Success</td>
  412.                 <td></td>
  413.             </xsl:otherwise>
  414.         </xsl:choose>
  415.         <td>
  416.             <xsl:call-template name="display-time">
  417.                 <xsl:with-param name="value" select="@time"/>
  418.             </xsl:call-template>
  419.         </td>
  420.     </tr>
  421. </xsl:template>
  422. <xsl:template match="failure">
  423.     <xsl:call-template name="display-failures"/>
  424. </xsl:template>
  425. <xsl:template match="error">
  426.     <xsl:call-template name="display-failures"/>
  427. </xsl:template>
  428. <!-- Style for the error and failure in the tescase template -->
  429. <xsl:template name="display-failures">
  430.     <xsl:choose>
  431.         <xsl:when test="not(@message)">N/A</xsl:when>
  432.         <xsl:otherwise>
  433.             <xsl:value-of select="@message"/>
  434.         </xsl:otherwise>
  435.     </xsl:choose>
  436.     <!-- display the stacktrace -->
  437.     <code>
  438.         <br/><br/>
  439.         <xsl:call-template name="br-replace">
  440.             <xsl:with-param name="word" select="."/>
  441.         </xsl:call-template>
  442.     </code>
  443.     <!-- the later is better but might be problematic for non-21" monitors... -->
  444.     <!--pre><xsl:value-of select="."/></pre-->
  445. </xsl:template>
  446. <xsl:template name="JS-escape">
  447.     <xsl:param name="string"/>
  448.     <xsl:choose>
  449.         <xsl:when test="contains($string,"'")">
  450.             <xsl:value-of select="substring-before($string,"'")"/>/'<xsl:call-template name="JS-escape">
  451.                 <xsl:with-param name="string" select="substring-after($string,"'")"/>
  452.             </xsl:call-template>
  453.         </xsl:when> 
  454.         <xsl:when test="contains($string,'/')">
  455.             <xsl:value-of select="substring-before($string,'/')"/>//<xsl:call-template name="JS-escape">
  456.                 <xsl:with-param name="string" select="substring-after($string,'/')"/>
  457.             </xsl:call-template>
  458.         </xsl:when>
  459.         <xsl:otherwise>
  460.             <xsl:value-of select="$string"/>
  461.         </xsl:otherwise>
  462.     </xsl:choose>
  463. </xsl:template>
  464. <!--
  465.     template that will convert a carriage return into a br tag
  466.     @param word the text from which to convert CR to BR tag
  467. -->
  468. <xsl:template name="br-replace">
  469.     <xsl:param name="word"/>
  470.     <xsl:choose>
  471.         <xsl:when test="contains($word,'
  472. ')">
  473.             <xsl:value-of select="substring-before($word,'
  474. ')"/>
  475.             <br/>
  476.             <xsl:call-template name="br-replace">
  477.                 <xsl:with-param name="word" select="substring-after($word,'
  478. ')"/>
  479.             </xsl:call-template>
  480.         </xsl:when>
  481.         <xsl:otherwise>
  482.             <xsl:value-of select="$word"/>
  483.         </xsl:otherwise>
  484.     </xsl:choose>
  485. </xsl:template>
  486. <xsl:template name="display-time">
  487.     <xsl:param name="value"/>
  488.     <xsl:value-of select="format-number($value,'0.000')"/>
  489. </xsl:template>
  490. <xsl:template name="display-percent">
  491.     <xsl:param name="value"/>
  492.     <xsl:value-of select="format-number($value,'0.00%')"/>
  493. </xsl:template>
  494. </xsl:stylesheet>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值