Fix dependency injection scope sharing between test executions#2931
Merged
Fix dependency injection scope sharing between test executions#2931
Conversation
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Dependency Injection Scope shared between tests
Fix dependency injection scope sharing between test executions
Aug 16, 2025
…nflicts Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Owner
|
@copilot Add a test for this, and decorate it with [EngineTest(ExpectedResult.Pass)] so it is run as part of the regression |
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
auto-merge was automatically disabled
August 16, 2025 22:38
Head branch was pushed to by a user without write access
Contributor
Author
Added regression test |
This was referenced Sep 29, 2025
This was referenced Oct 8, 2025
This was referenced Oct 16, 2025
This was referenced Oct 24, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When using
DependencyInjectionDataSourceAttribute<TScope>with multiple test arguments, the same dependency injection scope was incorrectly shared across all test invocations, causing scoped services to be reused when they should be isolated per test.Problem
Consider this test class using dependency injection:
Before this fix: Both test executions would output
1because they shared the same DI scope and got the sameDependencyinstance.After this fix: The test executions output different values (
1and2or2and1) because each gets its own DI scope with a freshDependencyinstance.Solution
The issue was in
DependencyInjectionDataSourceAttribute<TScope>.GenerateDataSources()where the scope was created once during data generation and reused across all test executions:Changed to create a new scope for each test execution:
This ensures proper test isolation while maintaining backward compatibility. Each scope is properly disposed after its corresponding test execution through the test context's disposal events.
Fixes #2930.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.