2012-01-04 123 views

回答

0

考慮到一個測試項目只不過是一個普通的類庫:你可以用XML comments來修飾它們來爲你的測試方法創建文檔,這與你在代碼中記錄正常方法的方式是一樣的。 如果您使用像NUnit這樣的測試框架,則可以使用test case factory (http://www.nunit.org/index.php?p=factory&r=2.5)爲您的方法提供輸入參數,並允許xml註釋描述測試的輸入數據。

此外,有看GhostDoc

+0

我已經使用GhostDoc。我的目的是從單元測試中提取文檔,而不是文檔單元測試,文檔應該包含純文本的文檔列表,類似於http://testdoc.codeplex.com/ – slamballx 2012-01-04 13:55:49

+1

您可以更改xml文檔的輸出結果您發佈的項目可能不會處理輸入參數你的方法ds,這是一個限制。如果你正在尋找一種更具描述性的寫作方式,你應該看看BDD。 StoryQ是一個很好的框架(http://storyq.codeplex.com/),你可以在這裏找到一個簡單的示例:http://www.codeproject.com/KB/testing/bddintro.aspx – 2012-01-04 14:57:55

+0

Thanks @ george2giga我會考慮使用BDD。 – slamballx 2012-01-04 17:13:59

1

單元測試應該是你的代碼的文檔。因此,記錄您的文檔似乎有點奇怪,因爲一個要求。

這就是說,單元測試是正常的代碼,所以你可以使用你已經用來記錄正常代碼的相同工具。諸如XML代碼的註釋和Sandcastle來生成文檔是可以想到的工具之一。

+0

現在我在看http://testdoc.codeplex.com/ – slamballx 2012-01-04 13:33:10

+0

我的目的是給測試用例的列表,我的同事在「純文本」,因爲他們不進行單元測試,我認爲這可能是一種合適的方式來接近單元測試世界。 – slamballx 2012-01-04 13:40:55

0

我已經晚了幾年晚會,但我只是在http://blog.aabech.no/archive/generating-documentation-from-nunit-tests/寫了一篇關於此的帖子。

如果上述內容已脫機,則其要點是NUnit Console runner默認輸出XML文件。您可以通過向該工具添加一個或多個--result = SPEC參數來覆蓋該參數。一個規範可以指向一個XSLT轉換,而您可以獲得HTML輸出。我猜XSLT也可以做純文本,所以它幾乎取決於你。 :)

下面是一個示例用法:
nunit3-console.exe My.Tests.dll --result:「TestSummary.htm; transform = TestSummary。XSLT」

和一個簡單的HTML轉換:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet 
    version="1.0" 
    exclude-result-prefixes="msxsl" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    > 
    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="@* | node()"> 
    <html> 
     <head> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"/> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"/> 
     <style type="text/css"> 
      .Passed { color: green; } 
      .Inconclusive { color: #BBAA00; } 
      .Failed { color: red; } 

      ul { 
      margin-left: 0px; 
      list-style-type: none; 
      padding-left: 0; 
      } 

      ul ul { 
      margin-left: 15px; 
      } 

      .counts { 
      font-size: .7em; 
      color: gray; 
      } 
     </style> 

     </head> 
     <body> 
     <div class="container"> 
      <xsl:apply-templates select="/test-run/test-suite"/> 
     </div> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">// Force closing tag</script> 
     <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js">// Force closing tag</script> 
     <script type="text/javascript"> 
      $("td").each(function(i, e) { 
      $(e).text($(e).text().replace(/_/g, " ")); 
      }); 
     </script> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="/test-run/test-suite"> 
    <h1> 
     <xsl:value-of select="./test-run/@name"/> 
    </h1> 
    <table class="table table-striped"> 
     <xsl:apply-templates select="./test-suite"/> 
    </table> 
    </xsl:template> 

    <xsl:template match="test-suite"> 
    <tr> 
     <td> 
     <xsl:attribute name="class"> 
      <xsl:choose> 
      <xsl:when test="./@failed > 0">Failed</xsl:when> 
      <xsl:when test="./@inconclusive > 0">Inconclusive</xsl:when> 
      <xsl:otherwise>Passed</xsl:otherwise> 
      </xsl:choose> 
     </xsl:attribute> 
     <xsl:attribute name="style"> 
      padding-left: <xsl:value-of select="count(ancestor::test-suite)*15"/>px; 
     </xsl:attribute> 
     <xsl:value-of select="./@name"/> 
     </td> 
     <td class="counts"> 
     <xsl:value-of select="./@passed"/> passed, 
     <xsl:value-of select="./@inconclusive"/> inconclusive, 
     <xsl:value-of select="./@failed"/> failed 
     </td> 
    </tr> 
    <xsl:for-each select="./test-suite"> 
     <xsl:apply-templates select="."/> 
    </xsl:for-each> 
    <xsl:for-each select="./test-case"> 
     <xsl:apply-templates select="."/> 
    </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="test-case"> 
    <tr> 
     <td colspan="2"> 
     <xsl:attribute name="style"> 
      padding-left: <xsl:value-of select="count(ancestor::test-suite)*15"/>px; 
     </xsl:attribute> 
     <xsl:attribute name="class"> 
      <xsl:value-of select="./@result"/> 
     </xsl:attribute> 
     <xsl:value-of select="./@name"/> 
     </td> 
    </tr> 

    </xsl:template> 
</xsl:stylesheet>