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
35 changes: 35 additions & 0 deletions TUnit.Engine.Tests/SkipInHooksTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Shouldly;
using TUnit.Engine.Tests.Enums;

namespace TUnit.Engine.Tests;

public class SkipInHooksTests(TestMode testMode) : InvokableTestBase(testMode)
{
[Test]
public async Task SkipInBeforeClassHook_ShouldMarkTestAsSkipped()
{
await RunTestsWithFilter(
"/*/*/SkipInBeforeClassHookTests/*",
[
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)
]);
}

[Test]
public async Task SkipInBeforeTestHook_ShouldMarkAllTestsAsSkipped()
{
await RunTestsWithFilter(
"/*/*/SkipInBeforeTestHookTests/*",
[
result => result.ResultSummary.Outcome.ShouldBe("Failed"),
result => result.ResultSummary.Counters.Total.ShouldBe(2),
result => result.ResultSummary.Counters.Passed.ShouldBe(0),
result => result.ResultSummary.Counters.Failed.ShouldBe(0),
result => result.ResultSummary.Counters.NotExecuted.ShouldBe(2)
]);
}
}
53 changes: 51 additions & 2 deletions TUnit.Engine/Services/HookExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.ExceptionServices;
using TUnit.Core;
using TUnit.Core.Exceptions;
using TUnit.Core.Services;
Expand Down Expand Up @@ -46,8 +47,16 @@ public async ValueTask ExecuteBeforeTestSessionHooksAsync(CancellationToken canc
}
catch (Exception ex)
{
// Wrap hook exceptions in specific exception types
// This allows the test runner to handle hook failures appropriately
if (ex is SkipTestException)
{
throw;
}

if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}

throw new BeforeTestSessionException("BeforeTestSession hook failed", ex);
}
}
Expand Down Expand Up @@ -100,6 +109,16 @@ public async ValueTask ExecuteBeforeAssemblyHooksAsync(Assembly assembly, Cancel
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}

if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}

throw new BeforeAssemblyException("BeforeAssembly hook failed", ex);
}
}
Expand Down Expand Up @@ -155,6 +174,16 @@ public async ValueTask ExecuteBeforeClassHooksAsync(
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}

if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}

throw new BeforeClassException("BeforeClass hook failed", ex);
}
}
Expand Down Expand Up @@ -209,6 +238,16 @@ public async ValueTask ExecuteBeforeTestHooksAsync(AbstractExecutableTest test,
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}

if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}

throw new BeforeTestException("BeforeEveryTest hook failed", ex);
}
}
Expand All @@ -228,6 +267,16 @@ public async ValueTask ExecuteBeforeTestHooksAsync(AbstractExecutableTest test,
}
catch (Exception ex)
{
if (ex is SkipTestException)
{
throw;
}

if (ex.InnerException is SkipTestException skipEx)
{
ExceptionDispatchInfo.Capture(skipEx).Throw();
}

throw new BeforeTestException("BeforeTest hook failed", ex);
}
}
Expand Down
40 changes: 40 additions & 0 deletions TUnit.TestProject/SkipInHooksTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using TUnit.Core;

namespace TUnit.TestProject;

public class SkipInBeforeClassHookTests
{
[Before(Class)]
public static void BeforeClass_Skip()
{
Skip.Test("Skipping from BeforeClass hook");
}

[Test]
public void Test_ShouldBeSkipped()
{
// This test should never execute
}
}

public class SkipInBeforeTestHookTests
{
[Before(Test)]
public void BeforeTest_Skip()
{
Skip.Test("Skipping from Before(Test) hook");
}

[Test]
public void Test1_ShouldBeSkipped()
{
// This test should never execute
}

[Test]
public void Test2_ShouldBeSkipped()
{
// This test should never execute
}
}

Loading