SourceGeneratorTestHelpers.MSTest 11.0.0

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

Compare with an approved snapshot

VerifyAsync provides built-in text snapshot testing without a Verify package dependency. It returns a standard Task and uses the assertion exception for your test framework.

XUnit
using SourceGeneratorTestHelpers;
using SourceGeneratorTestHelpers.XUnit;
using Xunit;

public class SourceGeneratorTests
{
    [Fact]
    public Task ShouldProduceTestId()
    {
        var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");
        return result.VerifyAsync("TestId.g.cs");
    }
}
NUnit
using NUnit.Framework;
using SourceGeneratorTestHelpers;
using SourceGeneratorTestHelpers.NUnit;

[TestFixture]
public class SourceGeneratorTests
{
    [Test]
    public Task ShouldProduceTestId()
    {
        var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");
        return result.VerifyAsync("TestId.g.cs");
    }
}
MSTest
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SourceGeneratorTestHelpers;
using SourceGeneratorTestHelpers.MSTest;

[TestClass]
public class SourceGeneratorTests : GeneratorDriverTestBase
{
    [TestMethod]
    public Task ShouldProduceTestId()
    {
        var (_, result) = IncrementalGenerator.Run<YourSourceGenerator>("your source");
        return VerifyAsync(result, "TestId.g.cs");
    }
}
Snapshot files and approval

Snapshots are stored beside the calling test source file. Default names match Verify 31.20.0: <TestClass>.<TestMethod>[_parameter=value...].verified.txt. The test class and method come from the running test framework, so renaming the source file does not rename snapshots. Nested test classes include their enclosing class name. The test above uses SourceGeneratorTests.ShouldProduceTestId.verified.txt. The compiler supplies the caller file path only to locate the snapshot directory. If a build maps source paths to a location that is unavailable when tests run, pass a fully qualified sourceFile path in an existing directory.

When the approved file is missing or the output changes, the assertion writes a .received.txt file and fails. In projects targeting multiple frameworks, received names include the selected runtime target, for example SourceGeneratorTests.ShouldProduceTestId.DotNet11_0.received.txt, matching Verify's convention. Approved snapshots remain shared across target frameworks. Review the received file, then rename or copy it over the corresponding .verified.txt file to approve the output. Commit approved files to source control and ignore *.received.txt. A passing comparison deletes any stale received file. Tests never create or overwrite approved files automatically and do not launch external diff tools.

Both texts are normalized to LF line endings before ordinal comparison. Other whitespace, including trailing newlines, remains significant. Received files use UTF-8 without a byte order mark. Missing generated output fails explicitly; an existing generated file with empty content can match an empty approved snapshot.

Parameterized tests automatically append parameter names and values, for example SourceGeneratorTests.ShouldProduceTestId_input=empty_enabled=True.verified.txt. Null values use null, numeric and date values use invariant formatting, and characters invalid in parameter values are replaced with -. NUnit fixture constructor arguments are included before method arguments.

For custom names or multiple snapshots in one test, supply a distinct, stable name for each output:

await result.VerifyAsync("TestId.g.cs", snapshotName: "GeneratesTestId.EmptyInput");

This uses GeneratesTestId.EmptyInput.verified.txt beside the test source file. Names exclude the .verified.txt suffix and must be valid file names without directory separators. Names are not sanitized automatically. Automatic naming requires a running test in the matching framework. Explicit names can also be used outside that framework's runner. Use distinct names for multiple comparisons in one test; calls targeting the same received file must not overlap.

The diagnostics-tuple overloads retain assertOnErrors: true and assertOnWarnings: false by default. Argument validation and enabled diagnostic assertions happen before snapshot file access. Cancellation and file-access failures propagate to the caller; pass cancellationToken to cancel asynchronous file operations. The raw GeneratorDriverRunResult overloads compare generated text without asserting diagnostics.

Migrating from Verify
  • Ordinary await result.VerifyAsync("TestId.g.cs") calls keep the same shape, but now return Task instead of SettingsTask.
  • Remove VerifySettings arguments, fluent Verify configuration, and Verify-specific initialization. This feature compares generated text; it does not provide Verify's object serialization or scrubbers.
  • Existing text snapshots using Verify's default class, method, and parameter naming can be reused directly. For custom Verify naming, pass the complete existing file stem as snapshotName. Move snapshots from custom Verify directories beside the calling test source file.
  • GeneratorDriverTestBase remains available for MSTest but no longer inherits VerifyBase. Tests that used other inherited Verify methods must replace those calls or reference Verify independently.
  • MSTest supplies the inherited TestContext property for automatic naming. Existing consumer TestContext properties are respected as well.
  • The helper packages no longer supply Verify's implicit imports. Import your test framework and helper namespace explicitly.
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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.0 8 11/10/2026
10.1.2 186 6/21/2026
10.1.0 147 6/20/2026
10.0.1 136 6/20/2026
10.0.0 244 11/15/2025
8.1.0 312 3/26/2024
8.0.14 257 3/26/2024
8.0.13 245 3/26/2024
8.0.12 262 3/25/2024
8.0.11 259 3/25/2024
8.0.10 274 3/25/2024
8.0.9 245 3/25/2024
8.0.8 265 3/18/2024
8.0.7 272 3/5/2024
8.0.6 259 2/23/2024
8.0.5 255 2/23/2024