Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions TUnit.Engine.Tests/SkipTestExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Shouldly;
using TUnit.Engine.Tests.Enums;

namespace TUnit.Engine.Tests;

public class SkipTestExceptionTests(TestMode testMode) : InvokableTestBase(testMode)
{
[Test]
public async Task ThrowingSkipTestException_ShouldMarkAsSkipped()
{
await RunTestsWithFilter(
"/*/*/SkipExceptionFixTest/*",
[
result => result.ResultSummary.Outcome.ShouldBe("Failed"),
result => result.ResultSummary.Counters.Total.ShouldBe(1),
result => result.ResultSummary.Counters.Passed.ShouldBe(0),
result => result.ResultSummary.Counters.Failed.ShouldBe(0),
result => result.ResultSummary.Counters.NotExecuted.ShouldBe(1)
]);
}
}
21 changes: 21 additions & 0 deletions TUnit.Engine/Services/SingleTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ await PropertyInjectionService.InjectPropertiesAsync(
test.Context.SkipReason = e.Message;
return await HandleSkippedTestInternalAsync(test, cancellationToken).ConfigureAwait(false);
}
catch (SkipTestException e)
{
test.Context.SkipReason = e.Reason;
return await HandleSkippedTestInternalAsync(test, cancellationToken).ConfigureAwait(false);
}
catch (Exception exception) when (_engineCancellationToken.Token.IsCancellationRequested && exception is OperationCanceledException or TaskCanceledException)
{
HandleCancellation(test);
Expand Down Expand Up @@ -318,6 +323,12 @@ private async Task ExecuteTestWithHooksAsync(AbstractExecutableTest test, object
test.State = TestState.Passed;
test.Result = _resultFactory.CreatePassedResult(test.StartTime!.Value);
}
catch (SkipTestException ex)
{
test.Context.SkipReason = ex.Reason;
test.Result = await HandleSkippedTestInternalAsync(test, cancellationToken).ConfigureAwait(false);
testException = ex;
}
catch (Exception ex)
{
HandleTestFailure(test, ex);
Expand All @@ -328,6 +339,16 @@ private async Task ExecuteTestWithHooksAsync(AbstractExecutableTest test, object
{
await ExecuteAfterTestHooksAsync(afterTestHooks, test.Context, cancellationToken).ConfigureAwait(false);
}
catch (SkipTestException afterHookSkipEx)
{
if (testException != null)
{
throw new AggregateException("Test and after hook both failed", testException, afterHookSkipEx);
}

test.Context.SkipReason = afterHookSkipEx.Reason;
test.Result = await HandleSkippedTestInternalAsync(test, cancellationToken).ConfigureAwait(false);
}
catch (Exception afterHookEx)
{
if (testException != null)
Expand Down
12 changes: 12 additions & 0 deletions TUnit.TestProject/SkipExceptionFixTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using TUnit.Core.Exceptions;

namespace TUnit.TestProject;

public class SkipExceptionFixTest
{
[Test]
public void ThrowSkipTestException_ShouldMarkAsSkipped()
{
throw new SkipTestException("This test should be skipped, not failed");
}
}
Loading