Skip to content

Commit efafe21

Browse files
committed
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.
1 parent 16c3457 commit efafe21

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,7 @@ private static void GenerateReturnHandling(
24782478
private static void GenerateDependencies(CodeWriter writer, Compilation compilation, IMethodSymbol methodSymbol)
24792479
{
24802480
var dependsOnAttributes = methodSymbol.GetAttributes()
2481+
.Concat(methodSymbol.ContainingType.GetAttributes())
24812482
.Where(attr => attr.AttributeClass?.Name == "DependsOnAttribute" &&
24822483
attr.AttributeClass.ContainingNamespace?.ToDisplayString() == "TUnit.Core")
24832484
.ToList();

TUnit.TestProject/DependsOnTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,31 @@ public static async Task AssertStartTimes()
2828
await Assert.That(_test2Start).IsAfterOrEqualTo(_test1Start.AddSeconds(4.9));
2929
}
3030
}
31+
32+
public sealed class MyAsyncTest
33+
{
34+
public static int NumberOfInvocations = 0;
35+
36+
[Test]
37+
public async Task Test()
38+
{
39+
NumberOfInvocations += 1;
40+
await Assert.That(NumberOfInvocations).IsEqualTo(1);
41+
}
42+
}
43+
44+
[EngineTest(ExpectedResult.Pass)]
45+
[DependsOn(typeof(MyAsyncTest), nameof(Test))]
46+
public sealed class DependsOn_AsyncTest
47+
{
48+
[Test]
49+
public async Task Test() => await Assert.That(MyAsyncTest.NumberOfInvocations).IsEqualTo(1);
50+
}
51+
52+
[EngineTest(ExpectedResult.Pass)]
53+
[DependsOn(typeof(MyAsyncTest), nameof(Test))]
54+
public sealed class DependsOn_AsyncTest_Two
55+
{
56+
[Test]
57+
public async Task Test() => await Assert.That(MyAsyncTest.NumberOfInvocations).IsEqualTo(1);
58+
}

0 commit comments

Comments
 (0)