Skip to content

Conversation

@thomhurst
Copy link
Owner

Summary

  • Fixed GenerateDependencies in TestMetadataGenerator to also check class-level [DependsOn] attributes
  • Added test cases for class-level [DependsOn] dependencies

Problem

When using [DependsOn] at the class level (rather than method level), the dependency was not being captured in the generated test metadata. This meant that when filtering tests, the dependencies declared at class level were not being pulled in.

For example:

[DependsOn(typeof(MyAsyncTest), nameof(Test))]  // Class-level - was being ignored!
public sealed class DependsOn_AsyncTest
{
    [Test]
    public async Task Test() => ...
}

Root Cause

The GenerateDependencies method only collected [DependsOn] attributes from methodSymbol.GetAttributes(), missing attributes on the containing type.

Fix

Added .Concat(methodSymbol.ContainingType.GetAttributes()) to also capture class-level attributes.

Test plan

  • Existing source generator tests pass (424 tests)
  • Manual test with filter /*/*/*DependsOn_AsyncTest*/* now correctly pulls in MyAsyncTest.Test dependency

@thomhurst
Copy link
Owner Author

test

@thomhurst
Copy link
Owner Author

Summary: Fixes source generator to collect class-level [DependsOn] attributes, bringing it into parity with reflection mode.

Critical Issues: None found ✅

Suggestions:

  1. Verify Snapshot Tests - The PR modifies source generator output (adds class-level attribute collection), which typically requires updating .verified.txt files per Rule 2. Please verify by running: dotnet test TUnit.Core.SourceGenerator.Tests

If snapshot tests fail, review the changes and commit the updated .verified.txt files (never .received.txt).

  1. Consider Adding Source Generator Test - While the manual test cases in DependsOnTests.cs are good, consider adding a specific source generator snapshot test that verifies class-level [DependsOn] attributes generate correct metadata. This would catch regressions more directly.

Analysis Notes:

Dual-Mode Compliance (Rule 1): ✅ Correct - Reflection mode (ReflectionAttributeExtractor.GetAttributes at TUnit.Engine/Discovery/ReflectionAttributeExtractor.cs:107-134) already collects class-level attributes (line 123). This PR fixes source generator mode to match reflection mode behavior. The change brings the two modes into parity rather than creating a disparity.

Performance (Rule 4): ✅ Good - Uses .Concat() which is appropriate for this code path (not a hot path during execution). Test metadata generation happens once during discovery.

Test Coverage: ✅ Good - Added comprehensive test cases covering class-level [DependsOn] with multiple test classes. Tests verify the actual dependency behavior (invocation counts).

Verdict: ✅ APPROVE - No critical issues. The fix correctly addresses the disparity between source generator and reflection modes. Just verify snapshot tests pass before merging.

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.
@thomhurst thomhurst force-pushed the fix/class-level-dependson-attributes branch from efafe21 to 083d106 Compare January 18, 2026 11:22
@thomhurst
Copy link
Owner Author

Summary

Fixes source generator to collect class-level [DependsOn] attributes, achieving parity with reflection mode.

Critical Issues

None found ✅

Suggestions

1. Snapshot Tests Required (Rule 2)
This PR modifies source generator output by changing attribute collection logic. Per TUnit's mandatory rules, source generator changes require updating snapshot tests.

Please run:

dotnet test TUnit.Core.SourceGenerator.Tests

If any .received.txt files are generated, review them and commit as .verified.txt files. The PR currently has no snapshot file updates, which suggests either:

  • Tests haven't been run yet, or
  • The change doesn't affect existing snapshot coverage

2. Consider Adding Explicit Source Generator Test
While DependsOnTests.cs validates runtime behavior, consider adding a source generator snapshot test that explicitly verifies class-level [DependsOn] attributes generate correct metadata. This would prevent regressions more directly.

Analysis Notes

Dual-Mode Compliance (Rule 1): ✅ Correct

  • Reflection mode (TUnit.Engine/Discovery/ReflectionAttributeExtractor.cs:123): Already collects class-level attributes via key.TestClass.GetCustomAttributes(key.AttributeType)
  • Source generator mode (before): Only checked methodSymbol.GetAttributes() - missing class-level
  • This PR: Adds .Concat(methodSymbol.ContainingType.GetAttributes()) to match reflection behavior

The change correctly brings source generator mode into parity with reflection mode.

Performance (Rule 4): ✅ Good
Using .Concat() is appropriate here - this runs during test discovery (not a hot path), and the allocation overhead is negligible.

Test Coverage: ✅ Good
Added comprehensive test cases with multiple classes depending on the same test, verifying actual dependency execution behavior.

Previous Review Status

A previous review comment exists noting similar findings. The PR has been updated but snapshot test verification remains pending.

Verdict

⚠️ REQUEST CHANGES - Please run and verify snapshot tests before merging (Rule 2 requirement).

@thomhurst thomhurst merged commit 996f5fa into main Jan 18, 2026
12 of 13 checks passed
@thomhurst thomhurst deleted the fix/class-level-dependson-attributes branch January 18, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants