-
-
Notifications
You must be signed in to change notification settings - Fork 105
feat: add IAsyncDiscoveryInitializer for asynchronous initialization during test discovery #4000
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
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| namespace TUnit.Core.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Defines a contract for types that require asynchronous initialization during test discovery. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Unlike <see cref="IAsyncInitializer"/> which runs during test execution, | ||
| /// implementations of this interface are initialized during the test discovery phase. | ||
| /// This enables data sources (such as <c>InstanceMethodDataSource</c>) to access | ||
| /// fully-initialized objects when generating test cases. | ||
| /// </para> | ||
| /// <para> | ||
| /// Common use cases include: | ||
| /// <list type="bullet"> | ||
| /// <item><description>Starting Docker containers before test case enumeration</description></item> | ||
| /// <item><description>Connecting to databases to discover parameterized test data</description></item> | ||
| /// <item><description>Initializing fixtures that provide data for test case generation</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <para> | ||
| /// This interface extends <see cref="IAsyncInitializer"/>, meaning the same | ||
| /// <see cref="IAsyncInitializer.InitializeAsync"/> method is used. The framework | ||
| /// guarantees exactly-once initialization semantics - objects will not be | ||
| /// re-initialized during test execution. | ||
| /// </para> | ||
| /// </remarks> | ||
| public interface IAsyncDiscoveryInitializer : IAsyncInitializer; |
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
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,58 @@ | ||
| using TUnit.Core; | ||
| using TUnit.Core.Interfaces; | ||
| using TUnit.TestProject.Attributes; | ||
|
|
||
| namespace TUnit.TestProject.Bugs._3997; | ||
|
|
||
| /// <summary> | ||
| /// Simulates a data source (like a Docker container) that needs initialization during test discovery | ||
| /// so that InstanceMethodDataSource can access its data when generating test cases. | ||
| /// </summary> | ||
| public class SimulatedContainer : IAsyncDiscoveryInitializer | ||
| { | ||
| private readonly List<string> _data = []; | ||
| public bool IsInitialized { get; private set; } | ||
|
|
||
| public IReadOnlyList<string> Data => _data; | ||
|
|
||
| public Task InitializeAsync() | ||
| { | ||
| if (IsInitialized) | ||
| { | ||
| throw new InvalidOperationException("Container already initialized! InitializeAsync should only be called once."); | ||
| } | ||
|
|
||
| // Simulate container startup and data population | ||
| _data.AddRange(["TestCase1", "TestCase2", "TestCase3"]); | ||
| IsInitialized = true; | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that IAsyncDiscoveryInitializer is called during test discovery, | ||
| /// allowing InstanceMethodDataSource to access initialized data. | ||
| /// </summary> | ||
| [EngineTest(ExpectedResult.Pass)] | ||
| public class DiscoveryInitializerTests | ||
| { | ||
| [ClassDataSource<SimulatedContainer>(Shared = SharedType.PerClass)] | ||
| public required SimulatedContainer Container { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// This property provides test data from the initialized container. | ||
| /// The container MUST be initialized during discovery before this is evaluated. | ||
| /// </summary> | ||
| public IEnumerable<string> TestCases => Container.Data; | ||
|
|
||
| [Test] | ||
| [InstanceMethodDataSource(nameof(TestCases))] | ||
| public async Task TestWithContainerData(string testCase) | ||
| { | ||
| // Container should be initialized | ||
| await Assert.That(Container.IsInitialized).IsTrue(); | ||
|
|
||
| // testCase should be one of the container's data items | ||
| await Assert.That(Container.Data).Contains(testCase); | ||
| } | ||
| } |
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 foreach loop implicitly filters its target sequence - consider filtering the sequence explicitly using '.Where(...)'.