diff --git a/src/Cake.Common.Tests/Unit/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporterTests.cs b/src/Cake.Common.Tests/Unit/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporterTests.cs index ca5936cff2..396e9ec10c 100644 --- a/src/Cake.Common.Tests/Unit/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporterTests.cs +++ b/src/Cake.Common.Tests/Unit/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporterTests.cs @@ -179,6 +179,79 @@ public void Should_Append_XsltFile() "/xsltFile:\"/Working/template.xslt\"", result.Args); } + [Fact] + public void Should_Rethrow_Exception_From_Action() + { + // Given + var exception = new CakeException("The exception message"); + var fixture = new SpecFlowTestExecutionReporterFixture(); + fixture.Settings.ThrowOnTestFailure = true; + var intercepting = true; + + fixture.Action = context => + { + context.ProcessRunner.Start( + new FilePath("/Working/tools/MSTest.exe"), + new ProcessSettings() + { + Arguments = "/resultsfile:\"/Working/TestResult.trx\"" + }); + + // Quick fix to avoid throwing exception while intercepting action + if (intercepting) + { + intercepting = false; + } + else + { + throw exception; + } + }; + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + Assert.IsCakeException(result, exception.Message); + } + + [Fact] + public void Should_Not_Rethrow_Exception_From_Action() + { + // Given + var exception = new CakeException("The exception message"); + var fixture = new SpecFlowTestExecutionReporterFixture(); + fixture.Settings.ThrowOnTestFailure = false; + var intercepting = true; + + fixture.Action = context => + { + var process = context.ProcessRunner.Start( + new FilePath("/Working/tools/MSTest.exe"), + new ProcessSettings() + { + Arguments = "/resultsfile:\"/Working/TestResult.trx\"" + }); + + // Quick fix to avoid throwing exception while intercepting action + if (intercepting) + { + intercepting = false; + } + else + { + throw exception; + } + }; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("mstestexecutionreport \"/Working/Tests.csproj\" " + + "/testResult:\"/Working/TestResult.trx\"", result.Args); + } + [Fact] public void Should_Capture_XUnit2() { diff --git a/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReportSettings.cs b/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReportSettings.cs index 8677c764e5..6a53a0ed45 100644 --- a/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReportSettings.cs +++ b/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReportSettings.cs @@ -9,5 +9,10 @@ namespace Cake.Common.Tools.SpecFlow.TestExecutionReport /// public sealed class SpecFlowTestExecutionReportSettings : SpecFlowSettings { + /// + /// Gets or sets a value indicating whether exceptions from the + /// intercepted action should be rethrown after report generation + /// + public bool ThrowOnTestFailure { get; set; } } } \ No newline at end of file diff --git a/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporter.cs b/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporter.cs index 8cf750e643..7e57864b21 100644 --- a/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporter.cs +++ b/src/Cake.Common/Tools/SpecFlow/TestExecutionReport/SpecFlowTestExecutionReporter.cs @@ -69,6 +69,7 @@ public void Run(ICakeContext context, var builder = GetArguments(interceptor, settings, projectFile); // Execute the action + CakeException testException = null; try { action(context); @@ -77,10 +78,16 @@ public void Run(ICakeContext context, { // Write warning to log context.Warning(e.Message); + testException = e; } // Run the tool. Run(settings, builder); + + if (settings.ThrowOnTestFailure && testException != null) + { + throw testException; + } } private static SpecFlowContext InterceptAction(