6

我尝试使用 cake 脚本运行在 Xunit 中使用 cake 脚本编写的测试用例,我需要知道通过和失败的测试用例计数。

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

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

谁能建议如何获得通过和失败的测试用例的数量

4

2 回答 2

11

您必须使用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 文档示例源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");
于 2016-11-16T15:00:23.373 回答
1

与大多数测试运行程序一样,XUnit 在控制台运行程序的返回代码中返回失败测试的数量。开箱即用,当工具的返回码不为零时,Cake 抛出异常,因此构建失败。

这可以在此处的 XUnit Runner Tests 中看到:

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");
});

这是利用 Cake 中的异常处理功能:

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

发生错误时采取措施。最重要的是,然后我使用ReportUnit 别名将 XML 报告转换为人类可读的 HTML 报告。

于 2016-11-16T21:24:24.730 回答