-
-
Notifications
You must be signed in to change notification settings - Fork 111
feat: add DisplayName property for dynamic tests #4058
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace TUnit.Core; | ||
|
|
||
| /// <summary> | ||
| /// Unified metadata class for dynamic tests. | ||
| /// Used by both AOT/source-generated mode and reflection mode for tests created via | ||
| /// DynamicTestBuilderAttribute or runtime test variant creation. | ||
| /// </summary> | ||
| public sealed class DynamicTestMetadata : TestMetadata, IDynamicTestMetadata | ||
| { | ||
| private readonly DynamicDiscoveryResult _dynamicResult; | ||
|
|
||
| public DynamicTestMetadata(DynamicDiscoveryResult dynamicResult) | ||
| { | ||
| _dynamicResult = dynamicResult; | ||
| } | ||
|
|
||
| public int DynamicTestIndex => _dynamicResult.DynamicTestIndex; | ||
|
|
||
| public string? DisplayName => _dynamicResult.DisplayName; | ||
|
|
||
| /// <summary> | ||
| /// Parent test ID for test variants created at runtime. | ||
| /// </summary> | ||
| public string? ParentTestId => _dynamicResult.ParentTestId; | ||
|
|
||
| /// <summary> | ||
| /// Relationship to parent test for test variants. | ||
| /// </summary> | ||
| public Enums.TestRelationship? Relationship => _dynamicResult.Relationship; | ||
|
|
||
| /// <summary> | ||
| /// Custom properties for test variants. | ||
| /// </summary> | ||
| public Dictionary<string, object?>? Properties => _dynamicResult.Properties; | ||
|
|
||
| [field: AllowNull, MaybeNull] | ||
| public override Func<ExecutableTestCreationContext, TestMetadata, AbstractExecutableTest> CreateExecutableTestFactory | ||
| { | ||
| get => field ??= CreateExecutableTest; | ||
| } | ||
|
|
||
| private AbstractExecutableTest CreateExecutableTest(ExecutableTestCreationContext context, TestMetadata metadata) | ||
| { | ||
| var modifiedContext = new ExecutableTestCreationContext | ||
| { | ||
| TestId = context.TestId, | ||
| DisplayName = _dynamicResult.DisplayName ?? context.DisplayName, | ||
| Arguments = _dynamicResult.TestMethodArguments ?? context.Arguments, | ||
| ClassArguments = _dynamicResult.TestClassArguments ?? context.ClassArguments, | ||
| Context = context.Context, | ||
| TestClassInstanceFactory = context.TestClassInstanceFactory | ||
| }; | ||
|
|
||
| // Apply runtime test variant properties | ||
| if (_dynamicResult.ParentTestId != null) | ||
| { | ||
| modifiedContext.Context.ParentTestId = _dynamicResult.ParentTestId; | ||
| } | ||
|
|
||
| if (_dynamicResult.Relationship.HasValue) | ||
| { | ||
| modifiedContext.Context.Relationship = _dynamicResult.Relationship.Value; | ||
| } | ||
|
|
||
| if (_dynamicResult.Properties != null) | ||
| { | ||
| foreach (var kvp in _dynamicResult.Properties) | ||
| { | ||
| modifiedContext.Context.StateBag.Items[kvp.Key] = kvp.Value; | ||
| } | ||
| } | ||
|
|
||
| // Create instance factory | ||
| var createInstance = async (TestContext testContext) => | ||
| { | ||
| // If we have a factory from discovery, use it | ||
| if (modifiedContext.TestClassInstanceFactory != null) | ||
| { | ||
| return await modifiedContext.TestClassInstanceFactory(); | ||
| } | ||
|
|
||
| // Check if there's a ClassConstructor to use | ||
| if (testContext.ClassConstructor != null) | ||
| { | ||
| var testBuilderContext = TestBuilderContext.FromTestContext(testContext, null); | ||
| var classConstructorMetadata = new ClassConstructorMetadata | ||
| { | ||
| TestSessionId = metadata.TestSessionId, | ||
| TestBuilderContext = testBuilderContext | ||
| }; | ||
|
|
||
| return await testContext.ClassConstructor.Create(metadata.TestClassType, classConstructorMetadata); | ||
| } | ||
|
|
||
| // Fall back to default instance factory | ||
| var instance = metadata.InstanceFactory(Type.EmptyTypes, modifiedContext.ClassArguments); | ||
|
|
||
| // Handle property injections | ||
| foreach (var propertyInjection in metadata.PropertyInjections) | ||
| { | ||
| var value = propertyInjection.ValueFactory(); | ||
| propertyInjection.Setter(instance, value); | ||
| } | ||
|
|
||
| return instance; | ||
| }; | ||
|
|
||
| var invokeTest = metadata.TestInvoker ?? throw new InvalidOperationException("Test invoker is null"); | ||
|
|
||
| return new ExecutableTest(createInstance, | ||
| async (instance, args, ctx, ct) => | ||
| { | ||
| await invokeTest(instance, args); | ||
| }) | ||
| { | ||
| TestId = modifiedContext.TestId, | ||
| Metadata = metadata, | ||
| Arguments = modifiedContext.Arguments, | ||
| ClassArguments = modifiedContext.ClassArguments, | ||
| Context = modifiedContext.Context | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This code block sets the DisplayName on the context.Metadata, but this seems redundant because the same DisplayName value is already being passed to CreateExecutableTestFactory through the ExecutableTestCreationContext on line 343. The DynamicTestMetadata.CreateExecutableTest method already handles setting the display name via the DisplayName property passed in the context parameter (line 49 in DynamicTestMetadata.cs).
Consider removing this redundant assignment since the DisplayName is already properly propagated through the ExecutableTestCreationContext. This would simplify the code and avoid setting the same value twice through different paths.