VerificationGlue
VerificationGlue.Tests gives [VerifyTest] tests a service provider from your modules. Put a module attribute on a test class or a test method. No DependencyModules adapter package is necessary. VerificationGlue's generator reads the module attributes and builds the container of each test through ModuleTestComposition in DependencyModules.Testing. Thus the testing attributes of DependencyModules.Testing have the same function as with DependencyModules.xUnit.
The examples on this page come from integ-tests/SutProject.VerificationGlueTests. The build workflow runs that project. VerificationGlue documents its own part in Test a DependencyModules application. To move tests from [ModuleTest], refer to Migrate from DependencyModules.xUnit.
Install
Reference VerificationGlue.Tests, DependencyModules.Runtime, and DependencyModules.Testing 1.8.0 or later. Also reference the project that contains your modules. If the test project declares modules or services, also add DependencyModules.SourceGenerator.
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Shop/Shop.csproj" />
<PackageReference Include="VerificationGlue.Tests" Version="0.6.0-preview.1" />
<PackageReference Include="DependencyModules.Runtime" Version="1.8.0" />
<PackageReference Include="DependencyModules.SourceGenerator" Version="1.8.0" PrivateAssets="all" />
<PackageReference Include="DependencyModules.Testing" Version="1.8.0" />
</ItemGroup>
<ItemGroup>
<Using Include="VerificationGlue.Tests" />
<Using Include="VerificationGlue.VerifySyntax" Static="true" />
</ItemGroup>VerificationGlue.Tests targets net10.0 only. Do not add xunit.v3, DependencyModules.xUnit, or a mock package to the same project. A VerificationGlue.Tests project is a Microsoft.Testing.Platform application. dotnet test needs "test": { "runner": "Microsoft.Testing.Platform" } in global.json. This repository keeps the VSTest runner for its other test projects, so scripts/test-verificationglue.sh runs the built application with dotnet exec.
VerificationGlue.Tests 0.6.0-preview.1 uses a preview of Microsoft.Testing.Platform 3. This repository restores that preview from the test-tools feed in NuGet.config.
Give a test the services of a module
Put the attribute of a module on the test class. Each test gets a new container with the services of the module, and its parameters come from that container. [ServiceCollection] and [Container<T>] are not necessary.
[SutModule]
public partial class SimpleResolveTests
{
[VerifyTest]
public void SimpleTest(IDependencyOne dependencyOne)
{
Verify(dependencyOne is DependencyOne);
Verify(dependencyOne.SingletonService is SingletonService);
Verify(dependencyOne.ScopedService is ScopedService);
}
}The test class is partial, because VerificationGlue's generator adds to it. A module attribute can be on the assembly, the class, or the method. The modules are applied from the assembly to the class to the method, as with [ModuleTest].
A module that the test project declares operates the same way:
[SingletonService(Realm = typeof(LocalModule))]
public class Greeter : IGreeter
{
public string Greet(string name) => $"Hello {name}";
}
[DependencyModule(OnlyRealm = true)]
public partial class LocalModule;
[LocalModule]
public partial class LocalModuleTests
{
[VerifyTest]
public void AModuleOfTheTestProjectRegistersItsServices(IGreeter greeter)
{
Verify(greeter.Greet("World") == "Hello World");
}
}Module parameters, realms, and module dependencies
A module attribute gives its constructor arguments and named properties to the module:
[VerifyTest]
[ParameterizedModule("local-string", 20, C = "CValue")]
public void ConstructorArgumentsAndNamedPropertiesReachTheModule(
SomeRuntimeDependency someRuntimeDependency
)
{
Verify(someRuntimeDependency.SomeDependency == "local-string");
Verify(someRuntimeDependency.IntDependency == 20);
Verify(someRuntimeDependency.CValue == "CValue");
}An OnlyRealm module registers only the services of its realm. Modules that use each other, such as ModuleA with [ModuleB] and ModuleB with [ModuleA], load both. A module that two attributes name loads one time:
[SutModule]
public partial class ModuleDuplicationTests
{
[VerifyTest]
[SutModule]
public void AModuleOnTheClassAndTheMethodRegistersOnce(IEnumerable<IDependencyOne> dependencies)
{
Verify(dependencies.Count() == 1);
}
}On one class or one method, DependencyModules.xUnit and DependencyModules.NUnit apply the module attributes of the test assembly after the module attributes of other assemblies. VerificationGlue applies them in the order of the source. When the order of two modules on one target is important, write them in the order that they must load.
[Container<T>]
[Container<T>] from VerificationGlue can name a module. The container adds the module first, and then the module attributes in scope:
[Container<SutModule>]
public partial class ContainerOfModuleTests
{
[VerifyTest]
public void TheNamedModuleRegistersItsServices(IDependencyOne dependencyOne)
{
Verify(dependencyOne is DependencyOne);
}
[VerifyTest]
[ParameterizedModule("method-string", 5, C = "method-c")]
public void TheModuleAttributesInScopeLoadWithIt(
IDependencyOne dependencyOne,
SomeRuntimeDependency dependency
)
{
Verify(dependencyOne is DependencyOne);
Verify(dependency.SomeDependency == "method-string");
}
}Mocks
A [Mock] parameter gets a VerificationGlue mock. The mock replaces the registration of its type, so the services of the module get the mock. The next test gets the real service again:
using VerificationGlue.Mocks;
[SutModule]
public partial class MockTests
{
[VerifyTest]
public void TheServiceReceivesTheMock(
IDependencyOne dependencyOne,
[Mock] IScopedService scopedService
)
{
Verify(ReferenceEquals(dependencyOne.ScopedService, scopedService));
Verify(scopedService is not ScopedService);
}
[VerifyTest]
public void ATestWithoutTheMockGetsTheRealService(IDependencyOne dependencyOne)
{
Verify(dependencyOne.ScopedService is ScopedService);
Verify(dependencyOne.SingletonService is SingletonService);
}
}Set up a mock with Returns and examine it with Received from VerificationGlue.Mocks. [NSubstituteSupport], [MoqSupport], and [FakeItEasySupport] do not create the mock of a [Mock] parameter.
A keyed [Mock] replaces the registration of its key only:
[KeyedModule]
public partial class KeyedMockTests
{
[VerifyTest]
public void AKeyedMockReachesTheServiceThatInjectsTheKey(
[Mock] [FromKeyedServices("C")] IKeyedRegistration registration,
CKeyedDependency dependency
)
{
registration.Key.Returns("mocked");
Verify(ReferenceEquals(dependency.Registration, registration));
Verify(dependency.Registration.Key == "mocked");
}
}DependencyModules.Testing.Attributes and VerificationGlue.Mocks both declare MockAttribute. VerificationGlue.Tests adds a global alias of MockAttribute to VerificationGlue.Mocks.MockAttribute. Thus a file that imports both namespaces can write [Mock]:
using DependencyModules.Testing.Attributes;
using VerificationGlue.Mocks;
[SutModule]
public partial class MockAliasTests
{
[VerifyTest]
public void MockBindsThroughTheAlias(
IDependencyOne dependencyOne,
[Mock] IScopedService scopedService
)
{
Verify(ReferenceEquals(dependencyOne.ScopedService, scopedService));
}
}VerificationGlue's generators also read [DependencyModules.Testing.Attributes.Mock] as a mock. To turn off the alias, set VerificationGlueMockAlias to false. Refer to [Mock] from either package.
[TestExport]
[TestExport] registers a service for the tests in its scope. The registration does not continue into the next test:
[SutModule]
public partial class TestExportTests
{
[VerifyTest]
[TestExport(typeof(ISingletonService), Implementation = typeof(ExportedSingletonService))]
public void OverridesARegistrationForOneTest(ISingletonService singletonService)
{
Verify(singletonService is ExportedSingletonService);
}
[VerifyTest]
public void TheOverrideDoesNotLeakIntoTheNextTest(ISingletonService singletonService)
{
Verify(singletonService is SingletonService);
}
}A [Mock] parameter is registered after [TestExport], so the mock replaces the export:
[SutModule]
[TestExport(typeof(ISingletonService), Implementation = typeof(ExportedSingletonService))]
public partial class MockBeatsTestExportTests
{
[VerifyTest]
public void AMockBeatsTheExport(
IDependencyOne dependencyOne,
[Mock] ISingletonService singletonService
)
{
singletonService.GetName().Returns("mocked");
Verify(ReferenceEquals(dependencyOne.SingletonService, singletonService));
Verify(dependencyOne.SingletonService.GetName() == "mocked");
}
}DM0021 reports a [Mock] parameter and a [TestExport] on the same method for the same service. Thus the export in this example is on the class.
[InjectValues]
[InjectValues] gives constructor arguments to a class that has no registration. The container gives the other arguments:
public record InjectModel(IDependencyOne DependencyOne, string StringValue);
[SutModule]
public partial class InjectValueTests
{
[VerifyTest]
public void InjectTestValue([InjectValues("Hello World!")] InjectModel injectModel)
{
Verify(injectModel.DependencyOne is DependencyOne);
Verify(injectModel.StringValue == "Hello World!");
}
}Environments
An attribute that implements IModuleEnvironmentProvider sets the environment of a test. The narrowest attribute applies: the method, then the class, then the assembly. [IfEnvironment] services use that environment:
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class SeededEnvironmentAttribute(string name) : Attribute, IModuleEnvironmentProvider
{
public IModuleEnvironment? ProvideEnvironment(MethodInfo testMethod) =>
new ModuleEnvironment(false, name);
}
[SingletonService(Realm = typeof(SeededEnvironmentModule))]
[IfEnvironment("seeded-environment")]
public class GatedByEnvironment : IGatedByEnvironment;
[DependencyModule(OnlyRealm = true)]
public partial class SeededEnvironmentModule;
[SeededEnvironmentModule]
[SeededEnvironment("seeded-environment")]
public partial class EnvironmentSeedingPrecedenceTests
{
[VerifyTest]
public void AClassLevelSeedApplies(IServiceProvider provider)
{
Verify(provider.GetService(typeof(IGatedByEnvironment)) is GatedByEnvironment);
}
[VerifyTest]
[SeededEnvironment("inner-environment")]
public void TheMethodsSeedBeatsTheClasses(
IServiceProvider provider,
IModuleEnvironment environment
)
{
Verify(provider.GetService(typeof(IGatedByEnvironment)) is null);
Verify(environment.EnvironmentName == "inner-environment");
}
}For more information, refer to Environments.
[Shared]
[Container<SutModule>(Scope = ContainerScope.Class)] builds one container for the class and a child container for each test. A [Shared] parameter gets the instance of the class container. The [Mock] parameters of a test are registered in the child container of that test, so each test gets its own mock:
[Container<SutModule>(Scope = ContainerScope.Class)]
public partial class SharedTests
{
private static readonly ConcurrentBag<ISingletonService> SharedInstances = [];
private static readonly ConcurrentBag<IScopedService> Mocks = [];
[VerifyTest]
public void FirstTest(
[Shared] ISingletonService shared,
IDependencyOne dependencyOne,
[Mock] IScopedService scopedService
)
{
SharedInstances.Add(shared);
Mocks.Add(scopedService);
Verify(SharedInstances.All(seen => ReferenceEquals(seen, shared)));
Verify(Mocks.Count(seen => ReferenceEquals(seen, scopedService)) == 1);
Verify(ReferenceEquals(dependencyOne.ScopedService, scopedService));
}
}[Shared] applies to the service type. In the test that has the [Shared] parameter, all services of that type come from the class container. A test without [Shared] gets an instance of its own child container.
A container that is shared with Scope uses the default environment. VerificationGlue describes this limit in Environments.
CurrentTest
CurrentTest gives the [VerifyTest] test that runs. Key is the TestContext of the test, and DisplayName is its display name:
[SutModule]
public partial class CurrentTestTests
{
[VerifyTest]
public void KeyIsTheTestContextOfTheRunningTest(TestContext context)
{
Verify(ReferenceEquals(CurrentTest.Key, context));
}
[VerifyTest]
public void ALineIsWrittenToTheOutputOfTheTest()
{
Verify(CurrentTest.TryWriteLine("written through CurrentTest"));
}
}VerificationGlue builds the container in the test. Thus an ITestStartupAttribute sees the test that runs. With DependencyModules.xUnit, CurrentTest has no test while the container is built. For more information, refer to CurrentTest and test output.
Differences from DependencyModules.xUnit
- A
[Mock]parameter always gets a VerificationGlue mock. ITestCaseInfois not registered. Use aTestContextparameter.- Use
[Row]and[RowSource]from VerificationGlue for data rows. - On one target, the module attributes are applied in the order of the source.
Migrate from DependencyModules.xUnit gives the complete list.