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?
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));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With