Skip to content

Commit 3a1dc5b

Browse files
thomhurstclaude
andcommitted
fix: Make BeforeEvery/AfterEvery hooks check test class before asserting custom executor
The BeforeEvery(Test) and AfterEvery(Test) hooks are GLOBAL - they run for ALL tests in the assembly. When SetHookExecutorWithStaticHooksTests was added, these global hooks would run for every test, including tests like AfterTestAttributeTests that don't have CustomHookExecutor set, causing assertions to fail. The fix: Check context.TestDetails.ClassType before running assertions in the global hooks, so they only assert custom executor behavior for tests in SetHookExecutorWithStaticHooksTests. This demonstrates that static hooks DO respect CustomHookExecutor when it's set on the TestContext. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 07bdb77 commit 3a1dc5b

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

TUnit.TestProject/SetHookExecutorTests.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,28 @@ public class SetHookExecutorWithStaticHooksTests
8383
[BeforeEvery(Test)]
8484
public static async Task BeforeEveryTest(TestContext context)
8585
{
86-
// This static hook should also execute with the custom executor
87-
await Assert.That(Thread.CurrentThread.Name).IsEqualTo("CrossPlatformTestExecutor");
88-
await Assert.That(CrossPlatformTestExecutor.IsRunningInTestExecutor.Value).IsTrue();
89-
context.ObjectBag["BeforeEveryExecuted"] = true;
86+
// This static hook is GLOBAL and runs for ALL tests in the assembly
87+
// Only run assertions for tests in SetHookExecutorWithStaticHooksTests class
88+
if (context.TestDetails.ClassType == typeof(SetHookExecutorWithStaticHooksTests))
89+
{
90+
// This static hook should execute with the custom executor when CustomHookExecutor is set
91+
await Assert.That(Thread.CurrentThread.Name).IsEqualTo("CrossPlatformTestExecutor");
92+
await Assert.That(CrossPlatformTestExecutor.IsRunningInTestExecutor.Value).IsTrue();
93+
context.ObjectBag["BeforeEveryExecuted"] = true;
94+
}
9095
}
9196

9297
[AfterEvery(Test)]
9398
public static async Task AfterEveryTest(TestContext context)
9499
{
95-
// This static hook should also execute with the custom executor
96-
await Assert.That(Thread.CurrentThread.Name).IsEqualTo("CrossPlatformTestExecutor");
97-
await Assert.That(CrossPlatformTestExecutor.IsRunningInTestExecutor.Value).IsTrue();
100+
// This static hook is GLOBAL and runs for ALL tests in the assembly
101+
// Only run assertions for tests in SetHookExecutorWithStaticHooksTests class
102+
if (context.TestDetails.ClassType == typeof(SetHookExecutorWithStaticHooksTests))
103+
{
104+
// This static hook should execute with the custom executor when CustomHookExecutor is set
105+
await Assert.That(Thread.CurrentThread.Name).IsEqualTo("CrossPlatformTestExecutor");
106+
await Assert.That(CrossPlatformTestExecutor.IsRunningInTestExecutor.Value).IsTrue();
107+
}
98108
}
99109

100110
[Test]

0 commit comments

Comments
 (0)