-
-
Notifications
You must be signed in to change notification settings - Fork 105
feat: add DynamicTestIndex property for unique test ID generation in dynamic tests #4056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Code Review - PR #4056SummaryThis PR successfully addresses issue #4052 by adding a DynamicTestIndex property to ensure unique test IDs when multiple dynamic tests target the same method. The implementation is clean and follows TUnit architecture patterns. Strengths
Observations and Recommendations1. Missing Test Coverage for the Bug FixIssue: There is no test that explicitly validates the bug fix - that multiple dynamic tests targeting the same method now generate unique IDs. Recommendation: Add a test case to TUnit.TestProject that creates multiple dynamic tests for the same method with different arguments, then verify all tests execute with unique IDs. This would prevent regression. 2. Consider Thread SafetyObservation: DynamicTestBuilderContext._nextIndex uses a simple increment (_nextIndex++). Question: Is AddTest() ever called concurrently? The [RequiresUnreferencedCode] attribute suggests it is called during test discovery, which is typically single-threaded, but worth confirming. If concurrent access is possible: Consider using Interlocked.Increment 3. Interface DesignObservation: IDynamicTestMetadata changed from a marker interface to having a property. Comment: This is good design - the property belongs on the interface since all implementations need it. Well done. Performance ConsiderationsNo performance concerns:
Security ConsiderationsNo security concerns:
Pre-Commit ChecklistBased on CLAUDE.md requirements:
Final VerdictAPPROVE with minor recommendation This is a solid fix that correctly addresses the root cause. The only improvement would be adding explicit test coverage to prevent regression. The implementation is clean, follows TUnit conventions, and properly handles both execution modes. Great work solving this issue! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a DynamicTestIndex property to support unique test ID generation for dynamic tests. This addresses issue #4052 where multiple dynamic tests targeting the same method would generate identical test IDs, causing collisions.
Key changes:
- Added
DynamicTestIndexproperty toIDynamicTestMetadatainterface and related classes to provide unique indexing for dynamic tests - Modified
DynamicTestBuilderContextto automatically assign sequential indices (starting at 1) when tests are added - Updated all three execution paths (source-generated/AOT, reflection, and runtime) to use
DynamicTestIndexin test ID generation viaMethodDataLoopIndex
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| TUnit.PublicAPI/*.verified.txt | Updated public API snapshots across all target frameworks (.NET 4.7, 8.0, 9.0, 10.0) to reflect new DynamicTestIndex property |
| TUnit.Core/IDynamicTestMetadata.cs | Changed from marker interface to include DynamicTestIndex property; added documentation |
| TUnit.Core/AbstractDynamicTest.cs | Added DynamicTestIndex property to both AbstractDynamicTest and DynamicDiscoveryResult classes |
| TUnit.Core/DynamicTestBuilderContext.cs | Implemented auto-incrementing index assignment (starting at 1) when tests are added |
| TUnit.Engine/Building/TestBuilderPipeline.cs | Updated both dynamic test generation paths to use DynamicTestIndex for MethodDataLoopIndex |
| TUnit.Engine/Services/TestRegistry.cs | Implemented IDynamicTestMetadata.DynamicTestIndex in RuntimeDynamicTestMetadata |
| TUnit.Engine/Discovery/ReflectionTestDataCollector.cs | Implemented IDynamicTestMetadata.DynamicTestIndex in DynamicReflectionTestMetadata |
| TUnit.Engine/Building/Collectors/AotTestDataCollector.cs | Implemented IDynamicTestMetadata.DynamicTestIndex in AotDynamicTestMetadata |
…ration for dynamic tests
Pull Request Review: DynamicTestIndex for Unique Test ID GenerationOverviewThis PR successfully fixes issue #4052 by introducing a ✅ Strengths1. Excellent Dual-Mode ImplementationThe fix correctly works in both execution modes:
This is a textbook example of using shared abstractions to achieve dual-mode parity. ✨ 2. Clean Architecture
3. Comprehensive Testing
4. Proper Documentation
5. Snapshot Tests UpdatedAll four public API snapshot files were correctly updated to reflect the new properties. This follows the mandatory Rule 2 from CLAUDE.md. ✅ 🔍 Code Quality ObservationsPerformance Considerations
Thread Safety
Recommendation: Consider documenting that 📝 Suggestions for Improvement1. Test Coverage Enhancement (Minor)The current test validates that 5 tests execute, but could be more specific: // Current test only checks counts
result.ResultSummary.Counters.Total.ShouldBe(5)
// Could also validate unique test IDs
result.Tests.Select(t => t.TestId).Should().OnlyHaveUniqueItems()
// And verify all arguments were used
result.Tests.Select(t => t.Arguments[0]).Should().BeEquivalentTo([1, 2, 3, 4, 5])This would more explicitly validate the bug fix (unique IDs) rather than just successful execution. 2. Edge Case: RepeatCount Interaction (Question)In
Question: Have you verified that a dynamic test with Example test case: // Add to TUnit.TestProject/DynamicTests/DynamicTestIndexTests.cs
[Repeat(2)]
public void RepeatedTestMethod(int value) { }
// In builder: add 2 dynamic tests targeting RepeatedTestMethod
// Expected: 4 total tests (2 dynamic × 2 repeats) with unique IDs3. Minor: Consistent Commenting StyleIn // Line 155: Use DynamicTestIndex from the metadata to ensure unique test IDs for multiple dynamic tests
// Line 276: Use DynamicTestIndex from the metadata to ensure unique test IDs for multiple dynamic testsThis is fine, but could be slightly more concise since it's an internal implementation detail. 🔒 Security & Safety
📋 CLAUDE.md Compliance ChecklistBased on TUnit's development guidelines:
🎯 Final VerdictLGTM with minor suggestions ✅ This is a solid fix that:
The suggestions above are minor enhancements rather than blocking issues. The PR is ready to merge as-is, though the additional test case for 💡 Additional NotesThe issue reporter also mentioned wanting custom test names (similar to NUnit's
Example for future documentation: context.AddTest(new DynamicTest<MyClass>
{
TestMethod = c => c.MyTest(type),
TestMethodArguments = [type],
DisplayName = $"Smoke test for {type.Name}" // Custom name here
});Great work! 🚀 |
Fixes #4052