Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make OpenCover return an error when there is a failing test in the Test Runner?

Tags:

c#

cakebuild

When using the following 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));
});

Even though there are some failing tests, the call to OpenCover doesn't fail with an exception as expected.

Is there a way to tell OpenCover to fail, if there are actually failing unit tests?

like image 473
Gary Ewan Park Avatar asked Mar 11 '23 10:03

Gary Ewan Park


1 Answers

The OpenCoverSettings class contains the ReturnTargetCodeOffset property. By setting this to 0, you are telling OpenCover to return the return code of the target process, rather than it's own return code. You can find more information about this here. With this in place, Cake will correctly handle the non zero exit code when there are failing tests, and throw an exception due to the failing tests.

The above code snippet should be changed to the following:

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));
});
like image 194
Gary Ewan Park Avatar answered May 05 '23 04:05

Gary Ewan Park