如何在xunit中使用蛋糕(c#make)脚本获得通过和失败测试用例计数

问题描述:

我尝试使用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

您必须使用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"); 

最喜欢的测试运行,返回的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报告。