From 5d52f03928c5eec9b3c63087258f58a2307b19fe Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:19:44 +0000 Subject: [PATCH] Last in Class, Assembly & Session EventReceiver tidy up + fixes (#1413) --- .../TestData/ClassDataSourceAttribute.cs | 20 +---- .../TestData/ClassDataSourceAttribute_2.cs | 32 +------- .../TestData/ClassDataSourceAttribute_3.cs | 50 +----------- .../TestData/ClassDataSourceAttribute_4.cs | 62 +-------------- .../TestData/ClassDataSourceAttribute_5.cs | 78 ++----------------- .../Attributes/TestData/ClassDataSources.cs | 64 +++++++-------- TUnit.Core/TestDataContainer.cs | 31 ++++++-- TUnit.Engine/Hooks/ClassHookOrchestrator.cs | 12 +-- TUnit.Engine/Services/SingleTestExecutor.cs | 6 +- 9 files changed, 86 insertions(+), 269 deletions(-) diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs index 9e04ee7391..4457cf650f 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs @@ -12,7 +12,7 @@ public override IEnumerable> GenerateDataSources(DataGeneratorMetadata d yield return () => { var item = ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .Get(Shared, dataGeneratorMetadata.TestClassType, Key); + .Get(Shared, dataGeneratorMetadata.TestClassType, Key, dataGeneratorMetadata); dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) => { @@ -43,23 +43,7 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( { await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item); }; - - if (Shared is SharedType.PerClass) - { - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass(context.Item2); - }; - } - - if (Shared is SharedType.PerAssembly) - { - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInAssembly(context.Item2); - }; - } - + return item; }; } diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs index 8bce612144..d5b62cfd5e 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs @@ -22,9 +22,9 @@ public sealed class ClassDataSourceAttribute< (T2 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys) + .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata) ); dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) => @@ -89,34 +89,6 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.T); }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass(context.Item2); - } - }; - - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - }; - return ( itemsWithMetadata.Item1.T, itemsWithMetadata.Item2.T diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs index 09653866cf..cbec0cfe59 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs @@ -25,11 +25,11 @@ public sealed class ClassDataSourceAttribute< (T3 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys) + .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata) ); dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) => @@ -117,49 +117,7 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); }; - - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(2) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - }; - - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(2) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - }; - + return ( itemsWithMetadata.Item1.T, itemsWithMetadata.Item2.T, diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs index b89b56a140..6048653197 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs @@ -28,13 +28,13 @@ public sealed class ClassDataSourceAttribute< (T4 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(3, dataGeneratorMetadata.TestClassType, Shared, Keys) + .GetItemForIndex(3, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata) ); dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) => @@ -147,60 +147,6 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item4.T); }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(2) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(3) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - }; - - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(2) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(3) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - }; - return ( itemsWithMetadata.Item1.T, itemsWithMetadata.Item2.T, diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs index a61bd92455..22b7b42a75 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs @@ -31,15 +31,15 @@ public sealed class ClassDataSourceAttribute< (T5 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(3, dataGeneratorMetadata.TestClassType, Shared, Keys), + .GetItemForIndex(3, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata), ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .GetItemForIndex(4, dataGeneratorMetadata.TestClassType, Shared, Keys) + .GetItemForIndex(4, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata) ); dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) => @@ -175,73 +175,7 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item5.Key, itemsWithMetadata.Item5.T); }; - - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(2) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(3) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - - if (Shared.ElementAtOrDefault(4) is SharedType.PerClass) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInClass(context.Item2); - } - }; - - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => - { - if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(2) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(3) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - - if (Shared.ElementAtOrDefault(4) is SharedType.PerAssembly) - { - await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) - .OnLastTestInAssembly(context.Item2); - } - }; - + return ( itemsWithMetadata.Item1.T, itemsWithMetadata.Item2.T, diff --git a/TUnit.Core/Attributes/TestData/ClassDataSources.cs b/TUnit.Core/Attributes/TestData/ClassDataSources.cs index 24a8fe4a74..0c2107c772 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSources.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSources.cs @@ -22,14 +22,14 @@ private ClassDataSources() public static ClassDataSources Get(string sessionId) => SourcesPerSession.GetOrAdd(sessionId, _ => new()); - public (T, SharedType, string) GetItemForIndex(int index, Type testClassType, SharedType[] sharedTypes, string[] keys) where T : new() + public (T, SharedType, string) GetItemForIndex(int index, Type testClassType, SharedType[] sharedTypes, string[] keys, DataGeneratorMetadata dataGeneratorMetadata) where T : new() { var shared = sharedTypes.ElementAtOrDefault(index); var key = shared == SharedType.Keyed ? GetKey(index, sharedTypes, keys) : string.Empty; return ( - Get(shared, testClassType, key), + Get(shared, testClassType, key, dataGeneratorMetadata), shared, key ); @@ -42,17 +42,38 @@ private string GetKey(int index, SharedType[] sharedTypes, string[] keys) return keys.ElementAtOrDefault(keyedIndex) ?? throw new ArgumentException($"Key at index {keyedIndex} not found"); } - public T Get(SharedType sharedType, Type testClassType, string key) where T : new() + public T Get(SharedType sharedType, Type testClassType, string key, DataGeneratorMetadata dataGeneratorMetadata) where T : new() { - return sharedType switch - { - SharedType.None => Create(), - SharedType.PerTestSession => TestDataContainer.GetGlobalInstance(Create), - SharedType.PerClass => TestDataContainer.GetInstanceForType(testClassType, Create), - SharedType.Keyed => TestDataContainer.GetInstanceForKey(key, Create), - SharedType.PerAssembly => TestDataContainer.GetInstanceForAssembly(testClassType.Assembly, Create), - _ => throw new ArgumentOutOfRangeException() - }; + if (sharedType == SharedType.None) + { + var t = Create(); + + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) => await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(t); + + return t; + } + + if (sharedType == SharedType.PerTestSession) + { + return TestDataContainer.GetGlobalInstance(Create, dataGeneratorMetadata); + } + + if (sharedType == SharedType.PerClass) + { + return TestDataContainer.GetInstanceForClass(testClassType, Create, dataGeneratorMetadata); + } + + if (sharedType == SharedType.Keyed) + { + return TestDataContainer.GetInstanceForKey(key, Create); + } + + if (sharedType == SharedType.PerAssembly) + { + return TestDataContainer.GetInstanceForAssembly(testClassType.Assembly, Create, dataGeneratorMetadata); + } + + throw new ArgumentOutOfRangeException(); } public Task InitializeObject(object? item) @@ -145,11 +166,6 @@ public Task Initialize(TestContext testContext, SharedType shared, string key public async ValueTask OnTestEnd(SharedType shared, string key, T? item) { - if (shared == SharedType.None) - { - await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(item); - } - if (shared == SharedType.Keyed) { await TestDataContainer.ConsumeKey(key, typeof(T)); @@ -161,20 +177,6 @@ public async ValueTask OnTestEnd(SharedType shared, string key, T? item) } } - public async ValueTask OnLastTestInClass(TestContext testContext) - { - var instance = TestDataContainer.GetInstanceForType(testContext.TestDetails.ClassType, () => default(T)!); - - await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(instance); - } - - public async ValueTask OnLastTestInAssembly(TestContext testContext) - { - var instance = TestDataContainer.GetInstanceForAssembly(testContext.TestDetails.ClassType.Assembly, () => default(T)!); - - await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(instance); - } - private static T Create() where T : new() { try diff --git a/TUnit.Core/TestDataContainer.cs b/TUnit.Core/TestDataContainer.cs index dbc9189281..382b4133b5 100644 --- a/TUnit.Core/TestDataContainer.cs +++ b/TUnit.Core/TestDataContainer.cs @@ -21,16 +21,33 @@ public static class TestDataContainer private static Disposer Disposer => new(GlobalContext.Current.GlobalLogger); - public static T GetInstanceForType(Type key, Func func) + public static T GetInstanceForClass(Type testClass, Func func, DataGeneratorMetadata dataGeneratorMetadata) { - var objectsForClass = InjectedSharedPerClassType.GetOrAdd(key, _ => new GetOnlyDictionary()); - return (T)objectsForClass.GetOrAdd(typeof(T), _ => func()!); + var objectsForClass = InjectedSharedPerClassType.GetOrAdd(testClass, _ => new GetOnlyDictionary()); + return (T)objectsForClass.GetOrAdd(typeof(T), _ => + { + var t = func()!; + + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, _) => + await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(t); + + return t; + }); } - public static T GetInstanceForAssembly(Assembly assembly, Func func) + public static T GetInstanceForAssembly(Assembly assembly, Func func, + DataGeneratorMetadata dataGeneratorMetadata) { var objectsForClass = InjectedSharedPerAssembly.GetOrAdd(assembly, _ => new GetOnlyDictionary()); - return (T)objectsForClass.GetOrAdd(typeof(T), _ => func()!); + return (T)objectsForClass.GetOrAdd(typeof(T), _ => + { + var t = func()!; + + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, _) => + await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(t); + + return t; + }); } public static void IncrementGlobalUsage(Type type) @@ -40,7 +57,7 @@ public static void IncrementGlobalUsage(Type type) CountsPerGlobalType[type] = count + 1; } - public static T GetGlobalInstance(Func func) + public static T GetGlobalInstance(Func func, DataGeneratorMetadata dataGeneratorMetadata) { return (T)InjectedSharedGlobally.GetOrAdd(typeof(T), _ => func()!); } @@ -58,7 +75,7 @@ public static T GetInstanceForKey(string key, Func func) { var instancesForType = InjectedSharedPerKey.GetOrAdd(typeof(T), _ => new GetOnlyDictionary()); - return (T)instancesForType.GetOrAdd(key, _ => func()!); + return (T)instancesForType.GetOrAdd(key, _ => func()!); } internal static async ValueTask ConsumeKey(string key, Type type) diff --git a/TUnit.Engine/Hooks/ClassHookOrchestrator.cs b/TUnit.Engine/Hooks/ClassHookOrchestrator.cs index 73aa5d623c..d093da2353 100644 --- a/TUnit.Engine/Hooks/ClassHookOrchestrator.cs +++ b/TUnit.Engine/Hooks/ClassHookOrchestrator.cs @@ -56,15 +56,17 @@ public IEnumerable> CollectAfterHooks( yield break; } + var allTestContextsInClass = GetContext(testClassType).Tests; + + foreach (var testEndEventsObject in allTestContextsInClass.SelectMany(tc => tc.GetLastTestInClassEventObjects())) + { + yield return new LastTestInClassAdapter(testEndEventsObject, testContext); + } + var typesIncludingBase = GetTypesIncludingBase(testClassType); foreach (var type in typesIncludingBase) { - foreach (var testEndEventsObject in testContext.GetLastTestInClassEventObjects()) - { - yield return new LastTestInClassAdapter(testEndEventsObject, testContext); - } - var list = hooksCollector.AfterClassHooks .GetOrAdd(type, _ => []).OrderBy(x => x.Order); diff --git a/TUnit.Engine/Services/SingleTestExecutor.cs b/TUnit.Engine/Services/SingleTestExecutor.cs index ab1ddc6603..f39988ff92 100644 --- a/TUnit.Engine/Services/SingleTestExecutor.cs +++ b/TUnit.Engine/Services/SingleTestExecutor.cs @@ -303,10 +303,12 @@ private async Task ExecuteStaticAfterHooks(DiscoveredTest test, TestContext test if (instanceTracker.IsLastTest()) { - foreach (var testEndEventsObject in testContext.GetLastTestInTestSessionEventObjects()) + var testSessionContext = TestSessionContext.Current!; + var allTests = testSessionContext.AllTests; + foreach (var testEndEventsObject in allTests.SelectMany(tc => tc.GetLastTestInTestSessionEventObjects())) { await RunHelpers.RunValueTaskSafelyAsync( - () => testEndEventsObject.OnLastTestInTestSession(TestSessionContext.Current!, testContext), + () => testEndEventsObject.OnLastTestInTestSession(testSessionContext, testContext), cleanUpExceptions); } }