A set of libraries for building and testing incremental C# source generators using Roslyn.
| Package | Description | Packable |
|---|---|---|
Purview.SourceGeneratorFramework |
Core helpers, models, and MSBuild integration for writing incremental source generators. | Yes |
Purview.SourceGeneratorFramework.Testing |
Framework-agnostic test runner and assertions for source generator unit tests. | Yes |
Purview.SourceGeneratorFramework.Testing.TUnit |
TUnit-specific test base classes and assertions for source generator tests. | Yes |
SourceGeneratorFramework.Testing.Generators |
Internal Roslyn source generator used by the framework package. | No |
SourceGeneratorFramework.ExampleGenerator |
Reference implementation showing how to build a generator with the framework. | No |
- .NET SDK 8.0 or later
- The test projects target
net8.0,net9.0, andnet10.0 - Source generators target
netstandard2.0
Restore and build the solution using the just recipes or dotnet directly:
just build
# or
dotnet build src/SourceGeneratorFramework.slnx -c Releasejust tests
# or
dotnet test src/SourceGeneratorFramework.slnx -c ReleaseWhen a test project must both run its generator (to use generated attributes or types in test
fixtures) and reference the generator type through the testing framework, use two project
references: one with OutputItemType="Analyzer" and ReferenceOutputAssembly="false", and one
normal reference with ReferenceOutputAssembly="true". The complete pattern is documented in the
Purview.SourceGeneratorFramework.Testing README.
The framework package includes AttributeDataModelGenerator (implemented in SourceGeneratorFramework.Testing.Generators), which generates readonly record struct parser models for .NET attributes. It removes the repetitive boilerplate of hand-writing FromAttributeData methods for every attribute you want to inspect in a source generator.
Supported features:
- Manual mapping of named arguments, constructor arguments by index, and constructor arguments by name
- Auto-discovery of all constructor parameters and public named properties
- Nested generated models (e.g., a shared
ValidationAttributeDatamodel reused insideRequiredAttributeData) - Inheritance matching for base attribute models
- Optional
DefaultValueruntime fallback when a property is not found on the attribute - An
Emptysentinel that usesdefault(T)for every property
See the SourceGeneratorFramework.Testing.Generators README for examples, including validation attributes with nested types.
just pack
# or
dotnet pack src/SourceGeneratorFramework.slnx -c Release -o ./artifactsThis project is licensed under the MIT license.