当测试运行程序中存在失败测试时,如何使 OpenCover return 出错?
How can I make OpenCover return an error when there is a failing test in the Test Runner?
使用以下 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()
.WithFilter(testCoverageFilter)
.ExcludeByAttribute(testCoverageExcludeByAttribute)
.ExcludeByFile(testCoverageExcludeByFile));
});
即使有一些失败的测试,对 OpenCover 的调用也不会像预期的那样失败并出现异常。
如果单元测试实际上失败了,有没有办法告诉 OpenCover 失败?
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。有了这个,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));
});
使用以下 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()
.WithFilter(testCoverageFilter)
.ExcludeByAttribute(testCoverageExcludeByAttribute)
.ExcludeByFile(testCoverageExcludeByFile));
});
即使有一些失败的测试,对 OpenCover 的调用也不会像预期的那样失败并出现异常。
如果单元测试实际上失败了,有没有办法告诉 OpenCover 失败?
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。有了这个,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));
});