Skip to content
Merged
37 changes: 37 additions & 0 deletions TUnit.Engine.Tests/HookExecutionOrderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Shouldly;
using TUnit.Core;

namespace TUnit.Engine.Tests;

public sealed class GlobalHookExecutionOrderSetup
{
[BeforeEvery(Test)]
public static void GlobalSetup(TestContext context)
{
HookExecutionOrderTests._executionOrder.Clear(); // Clear before each test
HookExecutionOrderTests._executionOrder.Add("BeforeEvery");
}
}

public class HookExecutionOrderTests
{
internal static readonly List<string> _executionOrder = [];

[Before(Test)]
public void InstanceSetup()
{
_executionOrder.Add("Before");
}

[Test]
public void VerifyExecutionOrder()
{
_executionOrder.Add("Test");

// Verify that BeforeEvery runs before Before
_executionOrder.Count.ShouldBe(3);
_executionOrder[0].ShouldBe("BeforeEvery");
_executionOrder[1].ShouldBe("Before");
_executionOrder[2].ShouldBe("Test");
}
}
31 changes: 31 additions & 0 deletions TUnit.TestProject/HookOrderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using TUnit.Core;

namespace TUnit.TestProject;

public class HookOrderTests
{
[Before(Test)]
public void Setup()
{
Console.WriteLine("Before test setup");
}

[Test]
public void Basic()
{
Console.WriteLine("This is a basic test");
}
}

public sealed class GlobalHooks
{
[BeforeEvery(Test)]
public static void BeforeTest(TestContext testContext)
{
// Only output for our specific test class to avoid polluting other tests
if (testContext.TestDetails.ClassType == typeof(HookOrderTests))
{
Console.WriteLine("Before every test");
}
}
}
Loading