【问题标题】:How to get passed and fail test case count in xunit using cake(c# make) script如何使用 cake(c# make) 脚本在 xunit 中通过和失败测试用例计数
【发布时间】:2017-03-27 20:58:46
【问题描述】:

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

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

【问题讨论】:

  • 您是指测试报告还是希望将这些值用于其他用途?
  • @Nkosi XUnit2(testAssemblies);此行将运行上述 DLL 中的测试用例
  • @Nkosi 我想获取测试用例运行摘要,例如失败计数、通过测试用例计数,或者甚至只是在代码级别通过或失败
  • XUnit2Aliases​.XUnit2(IEnumerable<string>, ​XUnit2Settings) 允许您使用设置运行测试。其中之一是是否创建包含这些值的测试报告。
  • @Nkosi 谢谢,我会试试的

标签: c# unit-testing continuous-integration xunit cakebuild


【解决方案1】:

您必须使用 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 documentation,the example source,more 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>

那么下面的sn-p应该会给你带来信息:

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

【讨论】:

    【解决方案2】:

    与大多数测试运行程序一样,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 alias 将 XML 报告转换为人类可读的 HTML 报告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      相关资源
      最近更新 更多