question about letting cake script to keep going #3027
-
How do i make my cake script keep going if a step has an exit code other than zero? I see there is a ToolSetting class that has HandleExitCode in it. In my cake script, I am using xunit2 alias and settings. But I want to know if there is a way to get a count of the failed tests and respond based on that number. Or read in content from console? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
We currently don't return the number of failing tests through the Cake API. Easiest option is probably to parse the output file yourself (similar to what has been done here for NUnit #2772 (comment)). |
Beta Was this translation helpful? Give feedback.
-
As @pascalberger writes, specifying that a test report should be created and parsing it is probably the best option.
It could look something like below Task("Run-Unit-Tests")
.Does(context => {
FilePath xmlReportPath = context.MakeAbsolute(context.File("./artifacts/TestReport.xml"));
XUnit2(
"MyTests/bin/Debug/net472/MyTests.dll",
new XUnit2Settings {
XmlReport = true,
ReportName = xmlReportPath.GetFilenameWithoutExtension().FullPath,
OutputDirectory = xmlReportPath.GetDirectory().FullPath,
HandleExitCode = code => context.HandleXunitXmlResult(code, xmlReportPath)
}
);
}); This will create a report in the artifacts folder called
public static bool HandleXunitXmlResult(this ICakeContext context, int exitCode, FilePath testResultsPath)
{
// Exit code indicates success
if (exitCode == 0)
{
return true;
}
if (!context.FileExists(testResultsPath))
{
throw new FileNotFoundException("Test report not found.", testResultsPath.FullPath);
}
var xml = System.Xml.Linq.XDocument.Load(testResultsPath.FullPath);
var testCounters = (
from testRun in xml.Elements("assemblies")
from resultSummary in testRun.Elements("assembly")
from counters in resultSummary.Elements("collection")
select new {
Total = counters.GetElementAttributeLongValue("total"),
Passed = counters.GetElementAttributeLongValue("passed"),
Failed = counters.GetElementAttributeLongValue("failed"),
Skipped = counters.GetElementAttributeLongValue("skipped")
}
).FirstOrDefault();
if (testCounters == null)
{
throw new Exception($"Failed to parse test xml file {testResultsPath}.");
}
// allow one error
if (testCounters.Failed < 2)
{
context.Information("Less than 2 ({0}) failures \"ok\"", testCounters.Failed);
return true;
}
return false;
} it parses the XML and the counters within it, public static long GetElementAttributeLongValue(this System.Xml.Linq.XElement element, string key)
=> element
.Attributes(key)
.Select(
value=>long.TryParse(
value.Value,
System.Globalization.NumberStyles.Integer,
System.Globalization.CultureInfo.InvariantCulture,
out var longValue
) ? longValue : null as Nullable<long>
)
.FirstOrDefault() ?? -1L; resulting in that the build will continue if there are in this case less than 2 errors So something similar to the above would let you make decisions on something different from XUnit default behavior for failed tests. |
Beta Was this translation helpful? Give feedback.
As @pascalberger writes, specifying that a test report should be created and parsing it is probably the best option.
Func<bool> HandleExitCode
property could indeed be used to handle the result instead of the default exit code handling by creating a helper method.It could look something like below