Open sandboxFocus

Class UnitTestClass

A base class for xUnit tests that need to unit-test compile-time helper classes or query the Metalama code model.

Inheritance
UnitTestClass
Implements
Namespace: Metalama.Testing.UnitTesting
Assembly: Metalama.Testing.UnitTesting.dll
Syntax
public abstract class UnitTestClass : IDisposable
Remarks

This class provides the infrastructure for writing unit tests of compile-time logic used by aspects, without executing the aspects themselves. Test methods should call CreateTestContext(string?, string?) to obtain a TestContext that provides access to Metalama services.

A typical test workflow is:

  1. Create a test context using CreateTestContext(string?, string?)
  2. Create a compilation using CreateCompilation(string, string?, bool, IEnumerable<MetadataReference>?, string?, bool)
  3. Optionally switch execution context using WithExecutionContext(ICompilation, string?) if your code uses ExpressionFactory
  4. Query the code model or call your compile-time helper classes
  5. Assert expected results

The test context must be disposed at the end of each test method.

Constructors

Name Description
UnitTestClass(ITestOutputHelper?, bool)

Properties

Name Description
TestOutput

Gets an object allowing to write to the test output.

Methods

Name Description
AddExceptionHandler(IAdditionalServiceCollection)
AddSyntaxGenerationOptions(IAdditionalServiceCollection)
ConfigureExtensions(ITestExtensionCollector)
ConfigureServices(IAdditionalServiceCollection)

Adds services or mocks that are common to all tests in the current class. This method is called by CreateTestContext(string?, string?) and the services parameter is the one passed to the CreateTestContext(IAdditionalServiceCollection, string?, string?), if any, or an empty collection otherwise.

CreateAdditionalServiceCollection(params IService[])

Creates a collection of additional services that can then be passed to CreateTestContext(IAdditionalServiceCollection, string?, string?).

CreateDefaultTestContextOptions()
CreateTestContext(IAdditionalServiceCollection, string?, string?)

Creates a test context with a collection of additional services or mocks.

CreateTestContext(TestContextOptions?, IAdditionalServiceCollection?, string?, string?)

Creates a test context, optionally with a non-default TestContextOptions or a collection of additional services or mocks.

CreateTestContext(string?, string?)
CreateTestContextCore(TestContextOptions, IAdditionalServiceCollection)
Dispose()

Extension Methods

See Also