Purview.SourceGeneratorFramework.Testing 1.0.0-prerelease.23

This is a prerelease version of Purview.SourceGeneratorFramework.Testing.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Purview.SourceGeneratorFramework.Testing --version 1.0.0-prerelease.23
                    
NuGet\Install-Package Purview.SourceGeneratorFramework.Testing -Version 1.0.0-prerelease.23
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Purview.SourceGeneratorFramework.Testing" Version="1.0.0-prerelease.23" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Purview.SourceGeneratorFramework.Testing" Version="1.0.0-prerelease.23" />
                    
Directory.Packages.props
<PackageReference Include="Purview.SourceGeneratorFramework.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Purview.SourceGeneratorFramework.Testing --version 1.0.0-prerelease.23
                    
#r "nuget: Purview.SourceGeneratorFramework.Testing, 1.0.0-prerelease.23"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Purview.SourceGeneratorFramework.Testing@1.0.0-prerelease.23
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Purview.SourceGeneratorFramework.Testing&version=1.0.0-prerelease.23&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Purview.SourceGeneratorFramework.Testing&version=1.0.0-prerelease.23&prerelease
                    
Install as a Cake Tool

Purview.SourceGeneratorFramework.Testing

Framework-agnostic test runner and assertions for unit testing incremental C# source generators.

Installation

dotnet add package Purview.SourceGeneratorFramework.Testing

What's included

  • SourceGeneratorTestRunner<TGenerator> — compiles a snippet of C# source, runs the generator, automatically registers an isolated framework logging sink, and returns a DriverRunResult with generated syntax trees, the output compilation, and captured log entries.
  • SourceGeneratorTestBase<TGenerator> — abstract base class that accepts an ITestOutput instance for framework-specific logging integration.
  • SourceGeneratorTestOptions — options for configuring references, namespaces, analyzer-config values, output kind, and whether to emit the output compilation to an assembly.
  • DriverRunResult — wrapper around GeneratorDriverRunResult that exposes generated trees, the output compilation, emitted assembly, and log entries.
  • DriverRunResultExtensions — assertion helpers such as AssertNoCompilationErrors, AssertNoGenerationExceptions, AssertSingleGeneratedSource, AssertGeneratedSourceContains, and more.
  • ITestOutput / NullTestOutput — abstraction for capturing generator log output during tests.

Usage

Reference the package from a test project and write a test using the runner directly:

<ItemGroup>
  <PackageReference Include="Purview.SourceGeneratorFramework.Testing" />
  <PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
</ItemGroup>
using Purview.SourceGeneratorFramework.Testing;

public class MyGeneratorTests
{
    [Test]
    public async Task GeneratesExpectedSource()
    {
        var source = """
            [MyNamespace.MyAttribute]
            public partial class MyClass { }
            """;

        var runner = new SourceGeneratorTestRunner<MyGenerator>();
        var result = await runner.RunAsync(source);

        result.AssertNoCompilationErrors();
        var generated = result.AssertSingleGeneratedSource();

        // Use your test framework's assertions, e.g. with TUnit:
        // await Assert.That(generated).Contains("public static partial class MyClass");
    }
}

Or derive from SourceGeneratorTestBase<TGenerator> and plug in your own ITestOutput implementation.

Running the generator in the test project

Sometimes the test project's own source uses types produced by the generator—for example, an integration test may attach a generated marker attribute to a fixture class while also passing the generator type to SourceGeneratorTestRunner<TGenerator>.

Reference the generator project twice, once in each role:

<ItemGroup>
  
  <ProjectReference
    Include="..\..\src\MyGenerator\MyGenerator.csproj"
    PrivateAssets="all"
    OutputItemType="Analyzer"
    ReferenceOutputAssembly="false"
  />

  
  <ProjectReference
    Include="..\..\src\MyGenerator\MyGenerator.csproj"
    PrivateAssets="all"
    ReferenceOutputAssembly="true"
  />
</ItemGroup>

The analyzer reference makes generated declarations available to the test project's compilation. The normal reference makes the generator's CLR type available to the testing API. These are separate from the in-memory compilation created by SourceGeneratorTestRunner; source supplied to the runner is still compiled and generated independently.

The normal reference also exposes the generator's assembly dependencies to every target framework of the test project. Keep the generator on the oldest compatible Roslyn version—for example, Roslyn 4.13 when tests target .NET 8, .NET 9, and .NET 10. A generator built against Roslyn 5 and System.Collections.Immutable 10 will conflict with the framework assemblies supplied by .NET 8 and .NET 9. Use the framework's RegisterEmbeddedAttribute helper when avoiding a newer Roslyn API such as AddEmbeddedAttributeDefinition.

Options

Configure a test run with SourceGeneratorTestOptions:

var options = new SourceGeneratorTestOptions
{
    IncludeDefaultNamespaces = true,
    AdditionalNamespaces = ["MyNamespace"],
    AdditionalAssemblyTypes = [typeof(SomeExternalType)],
    CompileToAssembly = true,
    EnableLogging = true,
    AnalyzerConfigOptions = { ["MyGenerator_Disable"] = "true" }
};

var result = await runner.RunAsync(source, options);

Analyzer options are preserved under their supplied keys. Keys without the Roslyn build_property. prefix are additionally exposed as compiler-visible MSBuild properties, so either MyGenerator_Disable or build_property.MyGenerator_Disable can be used in tests.

See SourceGeneratorFramework.Testing.TUnit for a ready-made TUnit integration.

License

This project is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Purview.SourceGeneratorFramework.Testing:

Package Downloads
Purview.SourceGeneratorFramework.Testing.TUnit

Purview SourceGeneratorFramework libraries for building and testing incremental C# source generators.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-prerelease.25 40 8/27/2026
1.0.0-prerelease.24 58 8/19/2026
1.0.0-prerelease.23 64 8/19/2026
1.0.0-prerelease.22 62 8/19/2026
1.0.0-prerelease.21 59 8/18/2026
1.0.0-prerelease.20 56 8/18/2026
1.0.0-prerelease.19 76 8/17/2026
1.0.0-prerelease.18 69 8/16/2026
1.0.0-prerelease.17 71 8/14/2026
1.0.0-prerelease.16 59 8/13/2026
1.0.0-prerelease.15 60 8/13/2026
1.0.0-prerelease.14 56 8/13/2026
1.0.0-prerelease.13 66 8/13/2026
1.0.0-prerelease.12 65 8/12/2026
1.0.0-prerelease.11 58 8/12/2026
1.0.0-prerelease.10 59 8/12/2026
1.0.0-prerelease.9 69 8/11/2026
1.0.0-prerelease.7 67 8/9/2026
1.0.0-prerelease.6 64 8/8/2026
1.0.0-prerelease.5 62 8/7/2026
Loading failed