2016-11-14 54 views
4

我嘗試使用Cake腳本來運行使用Cake腳本編寫的測試用例,我需要知道傳遞的數目和失敗的測試用例數。如何在xunit中使用蛋糕(c#make)腳本獲得通過和失敗測試用例計數

#tool "nuget:?package=xunit.runner.console" 
var testAssemblies = GetFiles("./src/**/bin/Release/*.Tests.dll"); 
XUnit2(testAssemblies); 

參考:http://www.cakebuild.net/dsl/xunit-v2

任何人都可以請建議如何獲得通過的數量和失敗的測試用例?

+0

您是指測試報告還是您希望將這些值用於其他內容? – Nkosi

+0

@Nkosi XUnit2(testAssemblies);這一行將運行所提到的DLL中的測試用例 – Venkat

+0

@Nkosi我想獲得測試用例運行摘要,例如失敗計數,通過測試用例計數,或者甚至只是代碼級別通過或失敗 – Venkat

回答

10

您必須使用XUnit2Aliases​.XUnit2(IEnumerable < FilePath >, ​XUnit2Settings) + XmlPeekAliases來讀取XUnit輸出。

var testAssemblies = GetFiles("./src/**/bin/Release/*.Tests.dll"); 
XUnit2(testAssemblies, 
    new XUnit2Settings { 
     Parallelism = ParallelismOption.All, 
     HtmlReport = false, 
     NoAppDomain = true, 
     XmlReport = true, 
     OutputDirectory = "./build" 
    }); 

XML格式爲:(XUnit documentationthe example sourcemore information in Reflex

<?xml version="1.0" encoding="UTF-8"?> 
<testsuite name="nosetests" tests="1" errors="1" failures="0" skip="0"> 
    <testcase classname="path_to_test_suite.TestSomething" 
       name="test_it" time="0"> 
     <error type="exceptions.TypeError" message="oops, wrong type"> 
     Traceback (most recent call last): 
     ... 
     TypeError: oops, wrong type 
     </error> 
    </testcase> 
</testsuite> 

然後將下面的代碼段應該爲你帶來的信息:

var file = File("./build/report-err.xml"); 
var failuresCount = XmlPeek(file, "/testsuite/@failures"); 
var testsCount = XmlPeek(file, "/testsuite/@tests"); 
var errorsCount = XmlPeek(file, "/testsuite/@errors"); 
var skipCount = XmlPeek(file, "/testsuite/@skip"); 
1

最喜歡的測試運行,返回的xUnit來自控制檯運行程序的返回碼中失敗的測試次數。開箱即用,當工具的返回碼不爲零時,Cake會拋出一個異常,並因此失敗構建。

這可以在的xUnit亞軍可以看到測試的位置:

https://github.com/cake-build/cake/blob/08907d1a5d97b66f58c01ae82506280882dcfacc/src/Cake.Common.Tests/Unit/Tools/XUnit/XUnitRunnerTests.cs#L145

因此,爲了知道是否:

簡單地將它傳遞或代碼級失敗的

這通過構建是否成功而隱含知道。我通常使用一個類似的戰略:

Task("Tests") 
.Does(() => 
{ 
    var testAssemblies = GetFiles("./src/**/bin/Release/*.Tests.dll"); 
    XUnit2(testAssemblies, 
     new XUnit2Settings { 
      Parallelism = ParallelismOption.All, 
      HtmlReport = false, 
      NoAppDomain = true, 
      XmlReport = true, 
      OutputDirectory = "./build" 
    }); 
}) 
.ReportError(exception => 
{ 
    Information("Some Unit Tests failed..."); 
    ReportUnit("./build/report-err.xml", "./build/report-err.html"); 
}); 

這是在製作蛋糕用的異常處理功能:

http://cakebuild.net/docs/fundamentals/error-handling

發生錯誤時採取行動。最重要的是,我使用ReportUnit alias將XML報告轉換爲人類可讀的HTML報告。

相關問題