如何从 DotCover 控制台运行程序获取所覆盖可执行文件的退出代码?

发布于 2024-11-03 13:49:35 字数 625 浏览 0 评论 0原文

我的组织正在致力于集成 DotCover 的控制台运行程序(描述 这里此处)进入我们的 MSBuild通过自定义 MSBuild 任务基于构建过程。

正如您所期望的,我们将介绍 NUnit 在我们的单元测试程序集上的运行。虽然我们对 DotCover 生成的覆盖率结果非常满意,但我们发现我们的测试现在可以失败,而不会导致我们的构建失败。前进一步,后退两步。

DotCover(至少我们运行它的方式)完全隐藏了所覆盖进程的结果,包括控制台输出和退出代码。我希望它在这方面表现得更像 NCover —— 回显所覆盖进程的所有输出和退出代码。

有谁知道如何使用 DotCover 控制台运行程序实现其中任何一个?获取所覆盖进程的退出代码是最重要的,因为我们需要我们的构建在测试失败时失败。

My organization is working on integrating DotCover's console runner (described here and here) into our MSBuild-based build process via a custom MSBuild task.

As you might expect, we are covering NUnit runs over our unit test assemblies. While we are quite happy with the coverage results DotCover is generating, we've discovered that our tests can now fail without causing our build to fail. One step forward, two steps back.

DotCover (at least the way we are running it) completely hides the results of the covered process, both the console output and the exit code. I wish it behaved more like NCover in this regard -- echoing all output and the exit code from the covered process.

Does anyone know how to achieve either of these with the DotCover console runner? Getting the exit code of the covered process is most important, as we need our builds to fail in the event of a test failure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も让我眼熟你 2024-11-10 13:49:35

我们使用 MSBuild 和 MSBuild 社区任务来使构建失败。

您可以使用 MSBuild.CommunityTasks 的 XmlRead 任务分析 dotCover 生成的输出 xml 文件。

<Target Name="DetermineCoverage">
    <Message Text="==================================================" />
    <Message Text="Determine Unit Test Coverage" />

    <XmlRead
        XPath="/Root/@CoveragePercent"
        XmlFileName="dotCoverOutput.xml">
        <Output TaskParameter="Value" PropertyName="CoveragePercent"/>
    </XmlRead>
    <Message Text="==" />
    <Message Text="== Coverage Percentage $(CoveragePercent)" />

    <Error
        Text="Unit Test coverage did not exceed the desired threshold"
        Condition="$(CoveragePercent) < 90" />

    <Message Text="==================================================" />
</Target>

We are using MSBuild and MSBuild Community Tasks to fail the build.

You can analyze the dotCover generated output xml file using the XmlRead task of MSBuild.CommunityTasks.

<Target Name="DetermineCoverage">
    <Message Text="==================================================" />
    <Message Text="Determine Unit Test Coverage" />

    <XmlRead
        XPath="/Root/@CoveragePercent"
        XmlFileName="dotCoverOutput.xml">
        <Output TaskParameter="Value" PropertyName="CoveragePercent"/>
    </XmlRead>
    <Message Text="==" />
    <Message Text="== Coverage Percentage $(CoveragePercent)" />

    <Error
        Text="Unit Test coverage did not exceed the desired threshold"
        Condition="$(CoveragePercent) < 90" />

    <Message Text="==================================================" />
</Target>
执手闯天涯 2024-11-10 13:49:35

我遇到了同样的问题。

另一种解决方法是首先使用 dotCover 之外的测试运行程序运行单元测试。这将公开正确的返回代码。

然后作为第二步,运行 dotCover 来获取覆盖率结果。

希望这对某人有帮助。

I ran into the same issue.

An alternative workaround is to run your unit tests with your test runner outside of dotCover first. This will expose the correct return codes.

And then as a second step, run dotCover to get your coverage results.

Hope this helps somebody.

守护在此方 2024-11-10 13:49:35

由于我需要快速解决方法,因此我添加了一个后处理步骤来抓取 DotCover 运行期间生成的 NUnit xml 文件以应对测试用例失败,如果发现任何错误,我都会使构建失败。我通过一个简单的自定义 MSBuild 任务完成了此操作:

public class CheckNUnitResults : Task
{
    [Required]
    public string ResultFile { get; set; }

    public override bool Execute()
    {
        Log.LogMessageFromText("Analyzing nunit results : " + ResultFile, MessageImportance.Normal);
        var failedCases =
            XDocument
                .Load(ResultFile)
                .Descendants("test-case")
                .Where(xe => xe.Attribute("success").Value.Equals("False"));
        var fail = failedCases.Any();
        if (fail)
        {
            Log.LogError("Found test case failures : " +
                         string.Join(", ", failedCases
                                               .Select(xe => xe.Attribute("name").Value)
                                               .ToArray()));
        }
        return !fail;
    }
}

这是从 MSBuild 调用的:(

<CheckNUnitResults ResultFile="YourNUnitResultFile.xml" />

我在包含结果文件的 ItemGroup 上循环调用它)

Since I needed a workaround quickly, I've added a post-processing step to scrape the NUnit xml file generated during the DotCover run for test case failures, and I fail the build if I find any. I did this via a simple custom MSBuild Task:

public class CheckNUnitResults : Task
{
    [Required]
    public string ResultFile { get; set; }

    public override bool Execute()
    {
        Log.LogMessageFromText("Analyzing nunit results : " + ResultFile, MessageImportance.Normal);
        var failedCases =
            XDocument
                .Load(ResultFile)
                .Descendants("test-case")
                .Where(xe => xe.Attribute("success").Value.Equals("False"));
        var fail = failedCases.Any();
        if (fail)
        {
            Log.LogError("Found test case failures : " +
                         string.Join(", ", failedCases
                                               .Select(xe => xe.Attribute("name").Value)
                                               .ToArray()));
        }
        return !fail;
    }
}

This is called from MSBuild thusly:

<CheckNUnitResults ResultFile="YourNUnitResultFile.xml" />

(I call it in a loop over an ItemGroup containing my result files)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文