Skip to content

Testing

Test utilities for Roslyn analyzers, generators, and code fixes.

Installation

dotnet add package Deepstaging.Roslyn.Testing --prerelease

Architecture

RoslynTestBase
  ├── SymbolsFor(source)           → SymbolTestContext
  │     ├── RequireNamedType()       (ValidSymbol<INamedTypeSymbol>)
  │     ├── Type("Foo").Methods()    (fluent queries)
  │     └── GetProperty()            (OptionalSymbol<IPropertySymbol>)
  ├── AnalyzeWith<T>(source)       → AnalyzerTestContext
  │     ├── ShouldReportDiagnostic()
  │     └── ShouldHaveNoDiagnostics()
  ├── GenerateWith<T>(source)      → GeneratorTestContext
  │     ├── ShouldGenerate()
  │     ├── WithFileNamed()
  │     └── VerifySnapshot()
  ├── FixWith<T>(source)           → CodeFixTestContext
  ├── AnalyzeAndFixWith<TA,TF>()   → CodeFixTestContext
  │     ├── ShouldProduce()
  │     └── ShouldAddAdditionalDocument()
  └── RenderTemplateFrom<T>()     → TemplateTestContext
        ├── Render()
        └── VerifySnapshot()

Quick Start

All tests inherit from RoslynTestBase:

public class MyTests : RoslynTestBase
{
    [Test]
    public async Task Analyzer_reports_diagnostic()
    {
        await AnalyzeWith<MyAnalyzer>(source)
            .ShouldReportDiagnostic("MY001")
            .WithSeverity(DiagnosticSeverity.Error);
    }

    [Test]
    public async Task Generator_produces_output()
    {
        await GenerateWith<MyGenerator>(source)
            .ShouldGenerate()
            .WithFileNamed("Customer.g.cs")
            .WithNoDiagnostics()
            .VerifySnapshot();
    }

    [Test]
    public async Task CodeFix_transforms_source()
    {
        await AnalyzeAndFixWith<MyAnalyzer, MyCodeFix>(source)
            .ForDiagnostic("MY001")
            .ShouldProduce(expectedSource);
    }
}

Reference Configuration

If tests need your own assemblies, configure once via ModuleInitializer:

[ModuleInitializer]
public static void Init() =>
    ReferenceConfiguration.AddReferencesFromTypes(typeof(MyAttribute));

API Reference

Type Description
RoslynTestBase Entry points for all test types
SymbolTestContext Compile source and query symbols
AnalyzerTestContext Assert on analyzer diagnostics
GeneratorTestContext Assert on generator output with snapshots
CodeFixTestContext Assert on code fix transformations
TemplateTestContext Assert on template rendering
Assertions TUnit assertion extensions for symbols, emit, and compilations
ReferenceConfiguration Configure assembly references for compilations