SourceGeneratorTestHelpers 10.1.0

dotnet add package SourceGeneratorTestHelpers --version 10.1.0
                    
NuGet\Install-Package SourceGeneratorTestHelpers -Version 10.1.0
                    
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="SourceGeneratorTestHelpers" Version="10.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SourceGeneratorTestHelpers" Version="10.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SourceGeneratorTestHelpers" />
                    
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 SourceGeneratorTestHelpers --version 10.1.0
                    
#r "nuget: SourceGeneratorTestHelpers, 10.1.0"
                    
#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 SourceGeneratorTestHelpers@10.1.0
                    
#: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=SourceGeneratorTestHelpers&version=10.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SourceGeneratorTestHelpers&version=10.1.0
                    
Install as a Cake Tool

Source Generator Test Helpers

Test helpers and extension methods to simplify testing of .NET source generators.

develop nuget downloads

Testing a source generator

var (generator, result) = SourceGenerator.Run<YourSourceGenerator>("your source");

You can also run a generator against multiple source files by passing any IEnumerable<string>.

var sources = new[]
{
    "namespace Tests; public partial class First;",
    "namespace Tests; public partial class Second;"
};

var (generator, result) = SourceGenerator.Run<YourSourceGenerator>(sources);

Testing an incremental source generator

var (generator, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");

The same multiple-source overload is available for incremental generators.

var sources = new[]
{
    "namespace Tests; public partial class First;",
    "namespace Tests; public partial class Second;"
};

var (generator, result) = IncrementalGenerator.Run<YourSourceGenerator>(sources);

Obtaining the generated source

Getting all generated sources

var generatedSources =  result.GetSources();

A single source that ends with a specific file path

var generatedSource =  result.GetSource("TestId.g.cs");

Compare the generated source with the expected source

You can produce a diff between the generated source and the expected source. The result will contain a boolean hasDifferences and a line by line diff in differences.

var (hasDifferences, differences) = Diff.Compare(generatedSource, expectedSource);

Assert the difference

Using one of the testing framework packages below, you can also assert the difference between the generated source and the expected source.

XUnit NUnit MSTest

var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");

result.ShouldProduce("TestId.g.cs", "expected source");

Note: If you do not wish to assert on errors produced during diagnostics of the source generator run, you can simply disable them as such.

var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");

result.ShouldProduce("TestId.g.cs", "expected source", false);

Verify the difference

Support for Verify is built-in using the VerifyAsync method.

XUnit
public class SourceGeneratorTests
{
    [Fact]
    public Task ShouldProductTestId()
    {
        var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");
        return result.VerifyAsync("TestId.g.cs");
    }
}
NUnit
[TestFixture]
public class SourceGeneratorTests
{
    [Test]
    public Task ShouldProductTestId()
    {
        var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");
        return result.VerifyAsync("TestId.g.cs");
    }
}
MSTest
[TestClass]
public class SourceGeneratorTests :
    GeneratorDriverTestBase
{
    [TestMethod]
    public Task ShouldProductTestId()
    {
        var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");
        return VerifyAsync(result, "TestId.g.cs");
    }
}
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on SourceGeneratorTestHelpers:

Package Downloads
SourceGeneratorTestHelpers.XUnit

Test helpers and extension methods to simplify testing of .NET source generators.

SourceGeneratorTestHelpers.MSTest

Test helpers and extension methods to simplify testing of .NET source generators.

SourceGeneratorTestHelpers.NUnit

Test helpers and extension methods to simplify testing of .NET source generators.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.1.0 76 6/20/2026
10.0.1 56 6/20/2026
10.0.0 561 11/15/2025
8.1.0 1,351 3/26/2024
8.0.14 376 3/26/2024
8.0.13 383 3/26/2024
8.0.12 409 3/25/2024
8.0.11 402 3/25/2024
8.0.10 395 3/25/2024
8.0.9 390 3/25/2024
8.0.8 424 3/18/2024
8.0.7 385 3/5/2024
8.0.6 452 2/23/2024
8.0.5 388 2/23/2024
8.0.4 231 2/23/2024
8.0.3 215 2/23/2024
8.0.2 225 2/22/2024
8.0.1 204 2/21/2024
8.0.0 248 2/21/2024