A context in which a Metalama unit test can run, configured with Metalama services and optionally mocks.
Implements
Namespace: Metalama.Testing.UnitTesting
Assembly: Metalama.Testing.UnitTesting.dll
Syntax
public class MetalamaTestContext : ITempFileManager, IApplicationInfoProvider, IDateTimeProvider, IBackstageService, IDisposableRemarks
This class provides access to the Metalama code model and services for unit testing compile-time logic. Tests should create a MetalamaTestContext by calling CreateTestContext(string?, string?) rather than using the constructor directly.
Key capabilities:
<ul><li><xref href="Metalama.Testing.UnitTesting.MetalamaTestContext.CreateCompilation(System.String%2cSystem.String%2cSystem.Boolean%2cSystem.Collections.Generic.IEnumerable%7bMicrosoft.CodeAnalysis.MetadataReference%7d%2cSystem.String%2cSystem.Boolean)" data-throw-if-not-resolved="false"></xref>: Create an <xref href="Metalama.Framework.Code.ICompilation" data-throw-if-not-resolved="false"></xref> from source code</li><li><xref href="Metalama.Testing.UnitTesting.MetalamaTestContext.WithExecutionContext(Metalama.Framework.Code.ICompilation%2cSystem.String)" data-throw-if-not-resolved="false"></xref>: Set the execution context for APIs like <xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory" data-throw-if-not-resolved="false"></xref></li><li><xref href="Metalama.Testing.UnitTesting.MetalamaTestContext.ServiceProvider" data-throw-if-not-resolved="false"></xref>: Access Metalama services</li><li><xref href="Metalama.Testing.UnitTesting.MetalamaTestContext.CancellationToken" data-throw-if-not-resolved="false"></xref>: A token that signals a timeout or the cancellation of the test</li></ul></p>
The MetalamaTestContext must be disposed at the end of each test method; failure to do so will result
in an exception from the finalizer. Using the using statement or using declaration is recommended.
Constructors
| Name | Description |
|---|---|
| MetalamaTestContext(MetalamaTestContextOptions, IAdditionalServiceCollection?, CancellationToken) | Initializes a new instance of the MetalamaTestContext class and specify an optional IAdditionalServiceCollection. Tests typically do not call this constructor directly, but instead the CreateTestContext(IAdditionalServiceCollection, string?, string?) method. |
| MetalamaTestContext(MetalamaTestContextOptions, CancellationToken) | Initializes a new instance of the MetalamaTestContext class. Tests typically do not call this constructor directly, but instead the CreateTestContext(IAdditionalServiceCollection, string?, string?) method. |
Properties
| Name | Description |
|---|---|
| BaseDirectory | Gets the directory that was specifically created for the current test and where all specific files should be stored. |
| CancellationToken | Gets the CancellationToken that the test must pass to the code under test. It is signalled when the timeout defined by Timeout elapses, or when the test framework cancels the test, whichever comes first. |
| ExpectsReportedExceptions | Gets or sets a value indicating whether the test deliberately reports exceptions through the telemetry capture
path. When |
| FaultInjector | Gets the injector of the fault injection points that the code under test reaches, which lets the test make production code throw at a chosen place. |
| Logger | |
| PlugIns | |
| ProjectOptions | |
| ReportedTelemetryExceptions | Gets the exceptions reported through the telemetry capture path in this context, with the routing each reporter chose (per-project repository directory, tooling, or disabled). See #1701. |
| ServiceProvider | Gets the ProjectServiceProvider for the current context. |
| SyncProvider | Gets the provider of the synchronization points that the code under test reaches, which lets the test drive concurrent code into a specific interleaving. All of its synchronization points are released when this context is disposed, so a failing test does not hang. |
| TestName | Gets the test name, for diagnostics. |