SourceGeneratorTestHelpers 10.1.0
dotnet add package SourceGeneratorTestHelpers --version 10.1.0
NuGet\Install-Package SourceGeneratorTestHelpers -Version 10.1.0
<PackageReference Include="SourceGeneratorTestHelpers" Version="10.1.0" />
<PackageVersion Include="SourceGeneratorTestHelpers" Version="10.1.0" />
<PackageReference Include="SourceGeneratorTestHelpers" />
paket add SourceGeneratorTestHelpers --version 10.1.0
#r "nuget: SourceGeneratorTestHelpers, 10.1.0"
#:package SourceGeneratorTestHelpers@10.1.0
#addin nuget:?package=SourceGeneratorTestHelpers&version=10.1.0
#tool nuget:?package=SourceGeneratorTestHelpers&version=10.1.0
Source Generator Test Helpers
Test helpers and extension methods to simplify testing of .NET source generators.
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.
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 | Versions 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. |
-
net10.0
- Basic.Reference.Assemblies.NetStandard20 (>= 1.8.9)
- DiffPlex (>= 1.9.0)
- Microsoft.CodeAnalysis.CSharp (= 4.14.0)
-
net8.0
- Basic.Reference.Assemblies.NetStandard20 (>= 1.8.9)
- DiffPlex (>= 1.9.0)
- Microsoft.CodeAnalysis.CSharp (= 4.14.0)
-
net9.0
- Basic.Reference.Assemblies.NetStandard20 (>= 1.8.9)
- DiffPlex (>= 1.9.0)
- Microsoft.CodeAnalysis.CSharp (= 4.14.0)
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 |