Test helpers and extension methods to simplify testing of .NET source generators.
var result = SourceGenerator.Run<YourSourceGenerator>("your source");var result = IncrementalGenerator.Run<YourSourceGenerator>("your source");var generatedSources = result.GetSources();var generatedSource = result.GetSource("TestId.g.cs");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);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);Support for Verify is built-in using the VerifyAsync method.
public class SourceGeneratorTests
{
[Fact]
public Task ShouldProductTestId()
{
var result = IncrementalGenerator.Run<YourSourceGenerator>("your source");
return result.VerifyAsync("TestId.g.cs");
}
}[TestFixture]
public class SourceGeneratorTests
{
[Test]
public Task ShouldProductTestId()
{
var result = IncrementalGenerator.Run<YourSourceGenerator>("your source");
return result.VerifyAsync("TestId.g.cs");
}
}[TestClass]
public class SourceGeneratorTests :
GeneratorDriverTestBase
{
[TestMethod]
public Task ShouldProductTestId()
{
var result = IncrementalGenerator.Run<YourSourceGenerator>("your source");
return VerifyAsync("TestId.g.cs");
}
}