当测试运行器中的测试失败时,如何使OpenCover返回错误?

时间:2016-08-13 18:38:01

标签: c# cakebuild

使用以下Cake Script时:

Task("Test-xUnit")
    .WithCriteria(() => DirectoryExists(parameters.Paths.Directories.PublishedxUnitTests))
    .Does(() =>
{
    EnsureDirectoryExists(parameters.Paths.Directories.xUnitTestResults);

    OpenCover(tool => {
        tool.XUnit2(GetFiles(parameters.Paths.Directories.PublishedxUnitTests + "/**/*.Tests.dll"), new XUnit2Settings {
            OutputDirectory = parameters.Paths.Directories.xUnitTestResults,
            XmlReport = true,
            NoAppDomain = true
        });
    },
    parameters.Paths.Files.TestCoverageOutputFilePath,
    new OpenCoverSettings()
        .WithFilter(testCoverageFilter)
        .ExcludeByAttribute(testCoverageExcludeByAttribute)
        .ExcludeByFile(testCoverageExcludeByFile));
});

即使有一些失败的测试,对OpenCover的调用也不会因预期的异常而失败。

如果实际上有单元测试失败,有没有办法告诉OpenCover失败?

1 个答案:

答案 0 :(得分:7)

OpenCoverSettings类包含ReturnTargetCodeOffset属性。通过将此值设置为0,您告诉OpenCover返回目标进程的返回代码,而不是它自己的返回代码。您可以找到有关此here的更多信息。有了这个,Cake会在测试失败时正确处理非零退出代码,并且由于测试失败而抛出异常。

上面的代码段应更改为以下内容:

Task("Test-xUnit")
    .WithCriteria(() => DirectoryExists(parameters.Paths.Directories.PublishedxUnitTests))
    .Does(() =>
{
    EnsureDirectoryExists(parameters.Paths.Directories.xUnitTestResults);

    OpenCover(tool => {
        tool.XUnit2(GetFiles(parameters.Paths.Directories.PublishedxUnitTests + "/**/*.Tests.dll"), new XUnit2Settings {
            OutputDirectory = parameters.Paths.Directories.xUnitTestResults,
            XmlReport = true,
            NoAppDomain = true
        });
    },
    parameters.Paths.Files.TestCoverageOutputFilePath,
    new OpenCoverSettings { ReturnTargetCodeOffset = 0 }
        .WithFilter(testCoverageFilter)
        .ExcludeByAttribute(testCoverageExcludeByAttribute)
        .ExcludeByFile(testCoverageExcludeByFile));
});