Repository navigation
Add ModuleTestComposition, the runner-neutral entry point, to DependencyModules.Testing - #141
Merged
Merged
Conversation
ModuleTestComposition.BuildAsync builds a test's container in the steps that ModuleTestCase and ModuleTestCommand take, plus a current-test registration, the services shared from a parent, and overrides registered after the setup attributes. BuildScopeAsync builds the parent of a class or an assembly. DependencyModules.Testing now references DependencyModules.Runtime and Microsoft.Extensions.DependencyInjection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
What changed
This is step 1 of "Changes, in the order they land" in
docs/design/verificationglue.md.DependencyModules.Testing gets four public types in
DependencyModules.Testing.Impl, with the signatures of Part 3:ModuleTestComposition.BuildAsync(services, modules, testMethod, overrides, currentTest, parent)builds a test's container in the order of Part 3. The steps are: register anITestContainerSource, thencurrentTest, then the narrowest environment. Then it loads the modules reversed, runs the setup attributes with mock support first, and registers the parent's shared instances. Then it runs the parameter providers for each parameter that no override names, registers the overrides as instances, and builds with the narrowestIServiceProviderBuilderAttribute. Last, it runs the startup attributes and initializes theTestContainerSource. When a step throws, it disposes every container it built and rethrows. Overrides come after the setup attributes, so an override beats a[TestExport]for the same service.ModuleTestComposition.BuildScopeAsync(services, modules, scopeAttributes)builds the parent for a class or an assembly. It loads the modules reversed with the default environment, registers each[TestExport(Shared = true)], and builds withBuildServiceProvider().ModuleTestContainerhasServices,ResolveArgumentsAsync,GetParameterValueAsync, the twoResolveoverloads andDisposeAsync. It tracks every container that itsTestContainerSourcebuilds.DisposeAsyncdisposes them, the last one built first.TestServiceOverride(record) andTestMethodContext(plainITestMethodContext).Package references: DependencyModules.Testing now references DependencyModules.Runtime and
Microsoft.Extensions.DependencyInjection(8.0.1 for net8.0, 10.0.0 for net10.0). These are the same versions as DependencyModules.xUnit. That package replaces the directMicrosoft.Extensions.DependencyInjection.Abstractionsreference, which it brings in transitively. This follows the recommendation of open question 1. I removed the sentence inIModuleEnvironmentProvider's remarks that said Testing does not reference Runtime. No package references VerificationGlue.The xUnit, xUnit4 and NUnit runners are not changed. Step 2 moves them onto this entry point.
Other files:
PublicApiTests.TestingApi.verified.txtgains the four types, andTestParameterResolver's public surface is unchanged. There are CHANGELOG entries under Added and Changed. The Testing package description mentionsModuleTestComposition.Deviations from the design, and decisions it left open
TestParameterResolver"stays public and unchanged". Its public API is unchanged. It gained an internal constructor that takes a skip predicate, which is how step 7 skips the overridden parameters in both phases. It also gained internal members forGetParameterValueAsyncand for a resolve that does not construct interfaces.ModuleTestContaineruses these, so it resolves exactly as xUnit and NUnit do today.TestExportAttributegained an internalRegister(IServiceCollection).BuildScopeAsynchas noITestMethodContextto pass toSetupServiceCollection, so it callsRegisterdirectly.[Shared]", means a parameter withSharedAttributeor a subclass of it. AnyISharedTestRegistrationwould not work here, because[Mock]and[InjectValues]also implement it withShared = true.IsolatedServicesfrom the test's attributes and from the parent's scope attributes. The design does not say where to read them.GetServicereturns null), and the child then resolves its own. A[Shared]parameter with[FromKeyedServices]is shared under its key. The design does not cover either case.Resolve(ParameterInfo)returns null for an unregistered interface, abstract class or value type. It builds only a non-abstract class, as the doc comment in Part 3 says. When such a build fails, the build's exception propagates.ResolveArgumentsAsynckeeps today's behaviour, which tries to build any type.BuildAsyncthrowsArgumentExceptionbefore it registers anything when an override'sInstanceis not assignable to itsServiceType. The design has no such check.DisposeAsyncdisposes every container even when one throws. It rethrows a single exception as it is, and throws more than one as anAggregateException. WhenBuildAsyncfails, an exception from the cleanup disposal is dropped, so the caller gets the exception of the step that failed.ResolveArgumentsAsyncandGetParameterValueAsyncon a container fromBuildScopeAsyncthrowInvalidOperationException, because there is no test method.GetParameterValueAsyncthrowsArgumentExceptionfor a parameter of another method.BuildAsyncdoes not collectIDependencyModuleProviderattributes fromtestMethod.Attributes. The caller passes every module inmodules, as Part 3 describes. Step 2 must collect them in the runners, together with the[ModuleTest]types.Why
VerificationGlue's Testing mode (steps 5 to 7 of the design) needs one entry point that builds a DependencyModules test container from attribute instances, a
MethodInfoand mock overrides, with no VerificationGlue type in it. Step 2 moves xUnit and NUnit onto the same entry point, so the three runners share one build order.How it was tested
New tests are in
tests/DependencyModules.Specs/TestingTests/ModuleTestCompositionTests.cs(VerificationGlue.Tests, per CONTRIBUTING.md). They driveModuleTestCompositionagainst a plainServiceCollectionand cover:LoadModules.[TestExport]beats a module, and mock support runs first.[TestExport]. A keyed override registers under its key. A[Mock]parameter with an override runs no provider: without the skip, its missing mock-support attribute would throw. A keyed parameter is skipped only by an override with the same key. An override whose instance has the wrong type is rejected.CurrentTest.Provideris not changed, and nothing is registered for null.ITestContainerSourcebuilds and starts like the first container and keeps the parameters, and its containers are disposed.DisposeAsyncdisposes every container when they throw (AggregateException), and one failure is rethrown as it is.Resolveoverloads give the provider, registered, keyed and built services, and return null for the missing cases.BuildScopeAsyncregisters only the shared exports, loads modules reversed, and has no parameters. Children share the parent's instance of a[Shared]parameter (one instance from a transient registration, not disposed by the children) and of shared exports. Keyed sharing works. Isolated services are not taken from the parent. An override beats the parent's instance. A child resolves its own copy of a service the parent does not have.TestMethodContexthas argument checks.Commands run before the commit, all passing:
dotnet csharpier check .dotnet restoreanddotnet build DependencyModules.sln --configuration Release: 0 errors../scripts/coverage.sh 85: DependencyModules.Tests 896/896, SutProject.Tests 174/174, SutProject.NUnitTests 46/46 and WebApiApp.Tests 2/2, each on net8.0 and net10.0. DependencyModules.Specs passed 326/326. Line coverage is 92.9%.ModuleTestComposition,ModuleTestContainerandTestMethodContextare at 100%../scripts/test-xunit.sh 4: passed../scripts/verify-packages.sh: all package checks passed for net8.0 and net10.0.! grep -rqi verificationglue src --include='*.csproj': passed.Not done here: the xUnit and NUnit runners are not moved onto the entry point (step 2), and DM0021 is not changed (step 3). Nothing in VerificationGlue calls the entry point yet.
🤖 Generated with Claude Code
Opened by Agent Driver for step
stp-vgdm-entry-pointof Tasktsk-verificationglue.