Open sandboxFocus

Class TestContext

A context in which a Metalama unit test can run, configured with Metalama services and optionally mocks.

Inheritance
TestContext
Implements
ITempFileManager
IApplicationInfoProvider
IDateTimeProvider
IBackstageService
Namespace: Metalama.Testing.UnitTesting
Assembly: Metalama.Testing.UnitTesting.dll
Syntax
public class TestContext : ITempFileManager, IApplicationInfoProvider, IDateTimeProvider, IBackstageService, IDisposable
Remarks

This class provides access to the Metalama code model and services for unit testing compile-time logic. Tests should create a TestContext by calling CreateTestContext(string?, string?) rather than using the constructor directly.

Key capabilities:

The TestContext 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
TestContext(TestContextOptions, IAdditionalServiceCollection?, CancellationToken)

Initializes a new instance of the TestContext class and specify an optional IAdditionalServiceCollection. Tests typically do not call this constructor directly, but instead the CreateTestContext(IAdditionalServiceCollection, string?, string?) method.

TestContext(TestContextOptions, CancellationToken)

Initializes a new instance of the TestContext 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 a CancellationToken used to cancel the test in case of timeout. The timeout period is defined by the Timeout option.

Logger
PlugIns
ProjectOptions
ServiceProvider

Gets the ProjectServiceProvider for the current context.

TestName

Gets the test name, for diagnostics.

Methods

Name Description
CreateCSharpCompilation(IReadOnlyDictionary<string, string>, IReadOnlyDictionary<string, string>?, bool, IEnumerable<MetadataReference>?, string?, bool, IEnumerable<string>?, OutputKind, bool)
CreateCSharpCompilation(IReadOnlyDictionary<string, string>, string?, bool, IEnumerable<MetadataReference>?, string?, bool, IEnumerable<string>?, OutputKind, bool)
CreateCSharpCompilation(string, string?, bool, IEnumerable<MetadataReference>?, string?, bool, IEnumerable<string>?, OutputKind, bool)
CreateCompilation(Compilation)
CreateCompilation(IReadOnlyDictionary<string, string>, string?, bool, IEnumerable<MetadataReference>?, string?, bool)
CreateCompilation(string, string?, bool, IEnumerable<MetadataReference>?, string?, bool)
CreateCompilationModel(Compilation)
CreateCompilationModel(IReadOnlyDictionary<string, string>, string?, bool, IEnumerable<MetadataReference>?, string?, bool)
CreateCompilationModel(string, string?, bool, IEnumerable<MetadataReference>?, string?, bool)
CreateEmptyCSharpCompilation(string?, IEnumerable<MetadataReference>, OutputKind, ImmutableArray<string>, NullableContextOptions, bool)
CreateEmptyCSharpCompilation(string?, IEnumerable<Assembly>?, bool, OutputKind, ImmutableArray<string>, NullableContextOptions, bool)
Dispose()
Dispose(bool)
~TestContext()
GetCompilationOptions(OutputKind, ImmutableArray<string>, NullableContextOptions, bool)
GetCompilationParseOptions(IEnumerable<string>?)
GetMetadataReferences(IEnumerable<Assembly>?, bool)
WithExecutionContext(ICompilation, string?)

Switches the execution context to a test context for a given ICompilation, enabling APIs such as ExpressionFactory to work correctly.

Extension Methods

See Also