From 083d10610e6dee645b5f73ad9ad43ecdbc60a0f0 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Sun, 18 Jan 2026 03:22:03 +0000 Subject: [PATCH] fix: Include class-level DependsOn attributes in generated test metadata The GenerateDependencies method in TestMetadataGenerator was only collecting [DependsOn] attributes from the test method. This caused class-level [DependsOn] attributes to be ignored, meaning dependencies declared at the class level were not being pulled in when filtering tests. This fix adds methodSymbol.ContainingType.GetAttributes() to also capture class-level DependsOn attributes, ensuring tests with class-level dependencies are properly resolved and included when filtered. --- .../Generators/TestMetadataGenerator.cs | 1 + TUnit.TestProject/DependsOnTests.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs b/TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs index ff515d62e6..6cb4b98438 100644 --- a/TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs +++ b/TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs @@ -2478,6 +2478,7 @@ private static void GenerateReturnHandling( private static void GenerateDependencies(CodeWriter writer, Compilation compilation, IMethodSymbol methodSymbol) { var dependsOnAttributes = methodSymbol.GetAttributes() + .Concat(methodSymbol.ContainingType.GetAttributes()) .Where(attr => attr.AttributeClass?.Name == "DependsOnAttribute" && attr.AttributeClass.ContainingNamespace?.ToDisplayString() == "TUnit.Core") .ToList(); diff --git a/TUnit.TestProject/DependsOnTests.cs b/TUnit.TestProject/DependsOnTests.cs index 7600ac9854..359ef05e4d 100644 --- a/TUnit.TestProject/DependsOnTests.cs +++ b/TUnit.TestProject/DependsOnTests.cs @@ -28,3 +28,31 @@ public static async Task AssertStartTimes() await Assert.That(_test2Start).IsAfterOrEqualTo(_test1Start.AddSeconds(4.9)); } } + +public sealed class MyAsyncTest +{ + public static int NumberOfInvocations = 0; + + [Test] + public async Task Test() + { + NumberOfInvocations += 1; + await Assert.That(NumberOfInvocations).IsEqualTo(1); + } +} + +[EngineTest(ExpectedResult.Pass)] +[DependsOn(typeof(MyAsyncTest), nameof(Test))] +public sealed class DependsOn_AsyncTest +{ + [Test] + public async Task Test() => await Assert.That(MyAsyncTest.NumberOfInvocations).IsEqualTo(1); +} + +[EngineTest(ExpectedResult.Pass)] +[DependsOn(typeof(MyAsyncTest), nameof(Test))] +public sealed class DependsOn_AsyncTest_Two +{ + [Test] + public async Task Test() => await Assert.That(MyAsyncTest.NumberOfInvocations).IsEqualTo(1); +}