Skip to content
Skills Library
Programming · Testing

purview-sourcegenerator-testing

/purview-sourcegenerator-testing

Use when writing or fixing tests for source generators, diagnostic analyzers, code fixes or refactorings built on Purview.SourceGeneratorFramework - picking the right runner and base class, configuring SourceGeneratorTestOptions, querying produced code with CodeQuery, testing attribute-data models against real AttributeData with AttributeDataFactory, and asserting incremental pipeline caching stage by stage.

Repository
unions ↗
Licence
MIT
Last repository change
12.11.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 12.11.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Use when writing or fixing tests for source generators, diagnostic analyzers, code fixes or refactorings built on Purview.SourceGeneratorFramework - picking the right runner and base class, configuring SourceGeneratorTestOptions, querying produced code with CodeQuery, testing attribute-data models against real AttributeData with AttributeDataFactory, and asserting incremental pipeline caching stage by stage.

Original author description

Use when writing or fixing tests for source generators, diagnostic analyzers, code fixes or refactorings built on Purview.SourceGeneratorFramework - picking the right runner and base class, configuring SourceGeneratorTestOptions, querying produced code with CodeQuery, testing attribute-data models against real AttributeData with AttributeDataFactory, and asserting incremental pipeline caching stage by stage.

How it works

  1. Picking the right runner
  2. Result types
  3. Querying produced code with CodeQuery
  4. Configuring options and a reusable starting point
  5. Testing attribute applications and attribute-data models
  6. Best practices
  7. Incremental cache testing (RunIncrementalAsync)
  8. License
Original SKILL.md headings · 432 lines, 2,327 words
SKILL.md preview
---
name: purview-sourcegenerator-testing
description: >-
  Use when writing or fixing tests for source generators, diagnostic analyzers, code fixes or
  refactorings built on Purview.SourceGeneratorFramework - picking the right runner and base class,
  configuring SourceGeneratorTestOptions, querying produced code with CodeQuery, testing attribute-data
  models against real AttributeData with AttributeDataFactory, and asserting incremental pipeline caching
  stage by stage.
metadata:
  product: Purview.SourceGeneratorFramework
  repository: purview-dev/sourcegenerator-framework
---

# Testing source generators, analyzers, code fixes and refactorings

Use this skill whenever a task involves authoring, fixing, or modernising tests for Roslyn components
(generators, diagnostic analyzers, code fix providers, refactoring providers) built with
`Purview.SourceGeneratorFramework`. It covers the framework-agnostic test runner layer and the
`CodeQuery` syntax-lookup API. For the TUnit base classes and assertion extensions, also load the
`purview-sourcegenerator-tunit-tests` skill.

Modernising an existing suite onto `CodeQuery` and adding a stage-by-stage cache test? The playbook
is in [references/test-modernisation.md](references/test-modernisation.md).

## Picking the right runner

| Roslyn type | Runner |
|---|---|
| `IIncrementalGenerator` / `ISourceGenerator` | `SourceGeneratorTestRunner<TGenerator>` |
| `DiagnosticAnalyzer` | `DiagnosticAnalyzerTestRunner<TAnalyzer>` |
| `CodeFixProvider` | `CodeFixTestRunner<TAnalyzer, TCodeFix>` (single) |
| — | `CodeFixTestRunner<TAnalyzer, TCodeFix>.RunFixAllAsync` (project-wide) |
| `CodeRefactoringProvider` | `RefactoringTestRunner<TRefactoring>` |

TUnit projects should prefer the matching base class instead (see `purview-sourcegenerator-tunit-tests`):
`TUnitSourceGeneratorTestBase`, `TUnitDiagnosticAnalyzerTestBase`, `TUnitCodeFixTestBase`,
`TUnitRefactoringTestBase`.

## Result types

- `DriverRunResult` (generator) — `DriverResult`, `AllSyntaxTrees`/`PrimarySyntaxTrees`,
  `CompilationResult.Compilation`, `GetGeneratedTree`, `GetSource`, `GetTypeByMetadataName`, `LogEntries`.
- `AnalyzerTestResult` — `Diagnostics`, `Compilation`.
- `CodeFixTestResult` — `Diagnostics`, `CodeActions`, `FixedSource`, `Compilation`, `ChangedSolution`.
- `CodeFixFixAllResult` — `Diagnostics`, `CodeActions`, `FixedSources`, `ChangedSolution`.
- `RefactorTestResult` — `CodeActions`, `FixedSources`, `ChangedSolution`, `Compilation`.

Use `DriverRunResultExtensions` (`AssertNoCompilationErrors`, `AssertNoGenerationExceptions`,
`AssertSingleGeneratedSource`, `AssertGeneratedSourceContains`, …) for quick checks, but prefer
`CodeQuery` for structural assertions.

## Querying produced code with `CodeQuery`

Every result exposes a `CodeQuery` via extensions in `CodeQueryResultExtensions`:

```csharp
result.Generated()          // DriverRunResult: generated trees (default, generated-first)
result.Output()             // DriverRunResult: entire output compilation (user + generated)
analyzerResult.Code()       // AnalyzerTestResult: input compilation
codeFixResult.Code()        // CodeFixTestResult: input compilation
codeFixResult.FixedCode()   // CodeFixTestResult: parsed fixed source (or post-fix solution)
fixAllResult.FixedCode()    // CodeFixFixAllResult: post-fix documents
refactorResult.FixedCode()  // RefactorTestResult: post-refactor documents
```

Every `Get` has an accompanying `Has` (bool) and `TryGet` (out): `GetMethod`/`HasMethod`/`TryGetMethod`,
`GetClass`, `GetStruct`, `GetInterface`, `GetEnum`, `GetDelegate`, `GetRecord`, `GetProperty`,
`GetField`, `GetConstructor`, `GetNamespace`, `GetTypeDeclaration`, plus generic `Get<T>`/`Has<T>`
and `GetSyntaxTree`/`HasSyntaxTree`. `Get` throws `SyntaxNotFoundException` when nothing matches.

Type lookups accept an optional generic arity — `GetClass(name, arity)` / `HasClass(name, arity)` — and the
`TypeReference`/`TypeIdentity` overloads match arity automatically from the identity, so
`new TypeIdentity("ResourceDefinition", ns, arity: 1)` finds `ResourceDefinition<T>` without matching the
non-generic `ResourceDefinition`.

Every `Get` returns a `CodeQueryResult<T>` — the matched syntax node (`Node`) plus a query scoped to it
(`Query`), with implicit conversions to both the node and the scoped query. Use `.Node` for direct syntax
access, or chain member queries (the originating query is carried by the result, so it is not passed again).

Types can be matched against `TypeReference`/`TypeIdentity`, resolved through the compilation's semantic
model (nullable value types are significant, so `int?` never matches `int`):

```csharp
result.Generated().HasMethod("DoWork", TypeReference.Create<int>(), TypeReference.Create<int>().Nullable(), complexType);
result.Generated().HasReturnType("Compute", TypeReference.Create<int>());
result.Generated().GetMethod("Format").HasParameters(TypeReference.Create<string>(), objectReference);
```

When a test expects a nullable annotation, prefer the test-only `query.MakeNullable(type)` extension (on a
`CodeQuery`, accepting a `TypeReference` or `TypeIdentity`). It resolves the annotation against the query's
compilation and, unlike `TypeReference.Nullable()`/`TypeIdentity.MakeNullable()`, does not trigger the
`PSGFR16` context-overload suggestion (tests have no generation context to pass).

Member chaining from a type declaration (`MemberQueryExtensions`):

```csharp
var service = result.Generated().GetClass("ServiceCollectionExtensions"); // or GetClass(name, "Namespace")
service.HasProperty("Count", intType);
service.HasIndexer(stringType, intType);
service.HasMethod("Add", intType, complexType);
service.HasMethodReturnType("Add", stringType);
service.HasConstructor(stringType);
service.HasAttribute("SomeAttribute");
```

Node-inspection checks on scoped results:

```csharp
result.Generated().GetClass("Service").HasAccessibility(Accessibility.Public);      // resolves C# defaults
result.Generated().GetClass("Service").GetProperty("Name").HasSetterAccessibility(Accessibility.Private);
result.Generated().GetClass("ResourceDefinition", 1).HasGenericTypeParameters("TResourceType");
result.Generated().GetClass("ResourceDefinition", 1).HasBaseType(new TypeIdentity("ResourceDefinition", ns));
result.Generated().GetClass("Service").HasNestedType("Builder");                    // GetNestedType(...) to fetch
result.Generated().GetClass("Service").IsInNamespace("Example.Models");             // IsInGlobalNamespace() for no namespace
result.Generated().IsInNamespace(cls.Node, "Example.Models");                       // or on the query directly
```

## Configuring options and a reusable starting point

`SourceGeneratorTestOptions` is the base record. Common knobs:

- `AdditionalNamespaces` / `IncludeDefaultNamespaces` — namespaces prepended to test source.
- `AdditionalAssemblyTypes` / `AdditionalReferences` — assemblies referenced by the test compilation
  (use `AdditionalAssemblyTypes = [typeof(SomeType)]` to pull in a whole assembly).
- `AdditionalSources` — extra source files added to every run.
- `AnalyzerConfigOptions` — `build_property.*` values; keys without the prefix are also exposed as MSBuild
  properties.
- `DisableSourceGeneratorPropertyName` / `DisableSourceGeneratorValue` — generator disable toggle.
- `NullableContextOptions`, `OutputKind`, `LanguageVersion`, `CompileToAssembly`.
- `ValidateCodeWriterScopes`, `EnableLogging`.
- `ExcludeGeneratedSourceHintNames` — hides generated marker trees from `PrimarySyntaxTrees`.

**Easy starting point recipe.** Derive an options record that seeds the namespaces and assemblies your
generator needs, so every test gets a working compilation with no boilerplate:

```csharp
public sealed record MyGeneratorTestOptions : SourceGeneratorTestOptions
{
    public MyGeneratorTestOptions()
    {
        AdditionalNamespaces = AdditionalNamespaces.Add("My.Namespace");
        AdditionalAssemblyTypes = AdditionalAssemblyTypes.AddRange(
            typeof(SomeDependencyType),
            typeof(TypeIdentity)   // the framework's Shared assembly, when needed
        );
        DisableSourceGeneratorPropertyName = PropertyLibrary.DisableMyGenerator;
    }
}
```

`Compile()` returns a copy with `CompileToAssembly = true`, preserving the derived options type. Use the
base class hooks `OnBeforeRun`/`OnBeforeRunAsync`/`OnAfterRun` to mutate sources/options per run (for
example to append a marker attribute source via `WithAdditionalSources`).

When `CompileToAssembly` is enabled, emission is fully in-memory. On .NET 8+ the assembly loads into a
collectible `AssemblyLoadContext`, so the result is `IDisposable` — `using var result = ...` unloads it and
keeps repeated runs from polluting the default context. `result.CompilationResult.Assembly` is the runnable
assembly (generated code can execute); `result.CompilationResult.Metadata` / `.MetadataAssembly` give a
metadata-only reflection view (types, members, attributes) over the emitted assembly without executing code.

**Always write `using var result = ...`.** `PSGFR42` ("Dispose test harness results with a using
declaration") flags a `DriverRunResult` that is produced and never disposed — including the indirect case
where a helper creates a result, projects a value out of it and returns only the projection. It is a
`warning` when the run syntactically compiles to an assembly (`.Compile()`, a `Compile` options member, or
`CompileToAssembly = true`) and `info` otherwise; the `DisposeTestResultCodeFixProvider` fix inserts the
`using`. A result that is returned to the caller, or disposed explicitly, is not flagged. Neither is awaiting
an assertion, nor calling a helper that is handed the result — an extension on it, or a method taking it as a
parameter: both hand the same instance back, so `await Assert.That(result).SomeAssertion()` and
`result.AssertNoGenerationExceptions()` are not flagged.

## Testing attribute applications and attribute-data models

A generator that reads attributes has two halves to test: what Roslyn gives it for an application, and what
its parsing (usually a generated attribute-data model) makes of that. `AttributeDataFactory` produces the
real `AttributeData` without running a generator; `AttributeArguments` describes it.

```csharp
var options = new MyTestOptions { AdditionalNamespaces = ["My.Attributes"] };

// One application. Placed on a class, or on the first declaration its AttributeUsage allows.
AttributeData attribute = AttributeDataFactory.FromUsage("[Log(\"text\", eventId: 7, Name = \"N\")]", options);

// Every application on a type, or on one of its members.
var onMember = AttributeDataFactory.FromSource(source, "Sample.IEvents", "Raised", options);

var arguments = AttributeArguments.From(attribute);
arguments.TryGetConstructorArgument(0, out var byOrdinal);              // #0 message: "text"
arguments.TryGetConstructorArgument("eventId", out var byName);         // Form = NamedParameter
arguments.TryGetNamedArgument("Name", out var named);                   // Form = NamedArgument
arguments.ToString();  // [LogAttribute(#0 message: "text", #1 eventId: 7 (by name), Name = "N")]

var model = LogAttributeData.FromAttributeData(attribute);
```

- References and usings come from the options: `AdditionalAssemblyTypes` for the assembly declaring the
  attribute, `AdditionalNamespaces` for its namespace, or declare the attribute in `AdditionalSources`.
- A compilation error throws `InvalidOperationException` listing the errors, so a misspelt parameter name
  fails the test rather than producing an application with a missing argument.
- `AttributeArgumentForm` tells `Positional`, `NamedParameter` (`message: "x"`), `Defaulted` (an optional
  parameter that was not written) and `NamedArgument` (`Name = "x"`) apart, which `AttributeData` alone
  cannot.
- `AttributeArgument.ValueEquals` compares numbers across types, enums by value or member name, `typeof`
  by `Type`/`TypeIdentity`/name, and arrays element-wise. Names are case-sensitive, as generated models are.
- The TUnit assertions over these (`HasConstructorArgument`, `HasNamedArgument`, …) are in
  `purview-sourcegenerator-tunit-tests`; what to cover for a model is in
  `purview-sourcegenerator-attribute-data-models`.

## Best practices

- **Test attribute parsing against real `AttributeData`** (`AttributeDataFactory`), not only by asserting
  on emitted parsing code: emitted code can look right and still read the wrong name.
- **Deterministic output**: `WriteAutoGeneratedHeader` is timestamp-free; assert with
  `ContainsGeneratedCode`/`GeneratesCode` (whitespace-flattened) or `CodeQuery`, never with timestamps.
- **Generator references**: to use a generated type in the test project AND pass the generator type to a
  runner, reference the generator project twice — once `OutputItemType="Analyzer"` and once as a normal
  reference.
- **Multi-target**: build generators against the Roslyn version that supports the test matrix; the
  framework is built against Roslyn 5.0 (net8.0/net9.0 assets keep a .NET 8–10 matrix loading); keep
  `System.Collections.Immutable` version pinned to the shared one.
- **Scope validation**: keep `PurviewSourceGeneratorFrameworkValidateCodeWriterScopes` enabled; it makes
  undisposed `CodeWriter` scopes fail tests.
- **Prefer `CodeQuery` over string matching** for structural assertions (members, signatures, namespaces).
- **Dispose results**: `using var result = ...`, enforced by `PSGFR42`.
- **Cap parallelism**: the harness shares one `MetadataReference` set per process, so memory is largely flat
  in the degree of parallelism, but the platform's auto-detected default is both slower and heavier than an
  explicit ceiling. Wall clock saturates around four concurrent tests; prefer
  `--maximum-parallel-tests 8` (or a `TestingPlatformCommandLineArguments` equivalent) for a project that
  multi-targets and runs several TFM executables at once.

## Incremental cache testing (`RunIncrementalAsync`)

To prove the pipeline caches correctly stage-by-stage, use `SourceGeneratorTestRunner.RunIncrementalAsync`
(or `GenerateIncrementalAsync` on the TUnit base). It runs a sequence of source sets over a **single shared
`GeneratorDriver`** and captures each run's `TrackedSteps`, keyed by tracking name. The canonical reference
implementation is `IncrementalPipelineCacheTests` in `SourceGeneratorShared.UnitTests`; the end-to-end
generator variant is `ServiceRegistrationCacheTests` in
`SourceGeneratorFramework.ExampleGenerator.UnitTests`. Copy the pattern into your own test project — do
not expect the source repo's files locally.

### What is being asserted and why

Roslyn reports one `IncrementalStepRunReason` per step output on each run:

- `New` — the step ran for the first time.
- `Modified` — the step ran and produced a different value than the previous run.
- `Unchanged` — the step ran but produced the same value.
- `Cached` — the step was skipped and its previous result reused from the incremental cache.

A pipeline is "caching correctly" when an unchanged input keeps every stage `Cached`/`Unchanged`, and a
targeted change marks **only** the stages whose inputs actually changed `Modified` while unrelated stages
stay `Cached`. If a generator accidentally leaks `Compilation`, `SemanticModel`, `ISymbol`,
`SyntaxNode`, or `Location` into a pipeline model, unrelated stages will report `Modified`/`New` on rerun —
these tests fail the build and catch the regression.

### The four scenarios every cache test should cover

1. **First run → all `New`.** Nothing can be cached on the first run; this confirms every stage is tracked
   under the expected name.
2. **Identical rerun → all `Cached`/`Unchanged`.** `RunIncrementalAsync(sources, ...)` runs the same source
   set twice for exactly this case. This is the strongest "it caches" proof.
3. **Source-only change → only the source/attribute stage `Modified`.** Changing an attributed class must
   mark `ForAttribute_*` (and downstream output) `Modified` while property/config stages stay `Cached`.
4. **Property-only change → only the property/configuration stage `Modified`.** Toggling an MSBuild
   property (via `IncrementalRunInput.AnalyzerConfig`) must mark `GetMSBuildPropertyValue_*` /
   `GetGenerationConfiguration` / `GetGenerationContext_*` `Modified` while `ForAttribute_*` stays `Cached`.

### How the runner makes this possible

`RunIncrementalAsync` creates **one** driver, enables incremental step tracking, and **reuses the same
`Compilation` instance for identical source sets** (keyed by prepared source text). Without that reuse,
Roslyn would see a fresh compilation on the second run and report stages `Modified`/`New` even though the
sources are byte-identical — the "cached" assertion would fail.

### Log entries per run

`IncrementalCacheRun.LogEntries` carries the framework log entries that run wrote, in order. The sink is
registered for the whole sequence and drained between runs, so entries never accumulate. A rerun whose
output step is cached logs nothing, which makes it a second signal alongside the step reasons:

```csharp
await Assert.That(result.Runs[0].LogEntries).IsNotEmpty();
await Assert.That(result.Runs[1].LogEntries).IsEmpty();
```

`DriverRunResult.LogEntries` (the `RunAsync` path) is likewise in write order. Both are empty when
`EnableLogging` is disabled.

### The `StepReasons` helper

`IncrementalCacheRun.Steps` is `ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>`
keyed by tracking name. Flatten each step's `Outputs` into the reasons list so assertions read cleanly:

```csharp
static ImmutableDictionary<string, ImmutableArray<IncrementalStepRunReason>> StepReasons(IncrementalCacheRun run)
{
    var builder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<IncrementalStepRunReason>>();
    foreach (var pair in run.Steps)
        builder[pair.Key] = [.. pair.Value.SelectMany(step => step.Outputs.Select(static output => output.Reason))];
    return builder.ToImmutable();
}
```

### Framework pipeline stage names

- `GetMSBuildPropertyValue_{Property}` — `IncrementalPipeline.PropertyValueProvider`.
- `GetGenerationConfiguration` — `IncrementalPipeline.GenerationContextValueProvider`.
- `GetGenerationContext_{Capabilities}` — e.g. `GetGenerationContext_EmptyCapabilities`.
- `ForAttribute_{AttributeType}` — `IncrementalPipeline.ForAttributeWithMetadataName`.

The framework reference (`IncrementalPipelineCacheTests`, framework-agnostic runner) shows all four
scenarios against `TestGenerator`/`DiagnosticTestGenerator`:

```csharp
using System.Collections.Immutable;
using Purview.SourceGeneratorFramework.TestGenerators;
using StepReason = Microsoft.CodeAnalysis.IncrementalStepRunReason;

public class IncrementalPipelineCacheTests
{
    const string AttributedSource = """
        [TestAttribute]
        public partial class MyClass { }
        """;
    const string ChangedAttributedSource = """
        [TestAttribute]
        public partial class AnotherClass { }
        """;
    const string TestAttributeSource = """
        [System.AttributeUsage(System.AttributeTargets.Class)]
        public sealed class TestAttribute : System.Attribute { }
        """;

    static SourceGeneratorTestOptions CreateOptions() =>
        new SourceGeneratorTestOptions()
            .WithAdditionalSources(TestAttributeSource)
            .WithExcludeGeneratedSourceHintNames("TestAttribute");

    static ImmutableDictionary<string, ImmutableArray<StepReason>> StepReasons(IncrementalCacheRun run) { /* as above */ }

    [Test]
    public async Task FirstRun_AllStagesAreNew(CancellationToken cancellationToken)
    {
        var result = await new SourceGeneratorTestRunner<TestGenerator>().RunIncrementalAsync(
            [new IncrementalRunInput([AttributedSource])],
            CreateOptions(),
            cancellationToken);

        var reasons = StepReasons(result.Runs[0]);
        await Assert.That(reasons).IsNotEmpty();
        await Assert.That(reasons.Values.SelectMany(r => r).All(r => r == StepReason.New)).IsTrue();
    }

    [Test]
    public async Task IdenticalRerun_AllStagesCached(CancellationToken cancellationToken)
    {
        var result = await new SourceGeneratorTestRunner<TestGenerator>()
            .RunIncrementalAsync([AttributedSource], CreateOptions(), cancellationToken);

        var second = StepReasons(result.Runs[1]);
        await Assert.That(second.Values.SelectMany(r => r).All(r => r is StepReason.Cached or StepReason.Unchanged)).IsTrue();
    }

    [Test]
    public async Task SourceChange_MarksAttributeStageModified_PropertyStagesStayCached(CancellationToken cancellationToken)
    {
        var result = await new SourceGeneratorTestRunner<TestGenerator>().RunIncrementalAsync(
            [new IncrementalRunInput([AttributedSource]), new IncrementalRunInput([ChangedAttributedSource])],
            CreateOptions(),
            cancellationToken);

        var second = StepReasons(result.Runs[1]);
        await Assert.That(second["ForAttribute_TestAttribute"]).Contains(StepReason.Modified);
        await Assert.That(second["GetMSBuildPropertyValue_DisableTestGenerator"].All(r => r == StepReason.Cached)).IsTrue();
    }

    [Test]
    public async Task PropertyChange_MarksPropertyStageModified_AttributeStageStaysCached(CancellationToken cancellationToken)
    {
        var result = await new SourceGeneratorTestRunner<TestGenerator>().RunIncrementalAsync(
            [
                new IncrementalRunInput([AttributedSource]),
                new IncrementalRunInput([AttributedSource], [("build_property.DisableTestGenerator", "true")]),
            ],
            CreateOptions(),
            cancellationToken);

        var second = StepReasons(result.Runs[1]);
        await Assert.That(second["GetMSBuildPropertyValue_DisableTestGenerator"]).Contains(StepReason.Modified);
        await Assert.That(second["ForAttribute_TestAttribute"].All(r => r == StepReason.Cached)).IsTrue();
    }
}
```

### End-to-end generator variant (`GenerateIncrementalAsync`)

TUnit tests derive from the base class and call `GenerateIncrementalAsync` (which wires the
`OnBeforeRun`/`OnBeforeRunAsync` hooks and the derived options). `ServiceRegistrationCacheTests` in
`SourceGeneratorFramework.ExampleGenerator.UnitTests` is the reference:

```csharp
public class ServiceRegistrationCacheTests
    : TUnitSourceGeneratorTestBase<ServiceRegistrationGenerator, ServiceRegistrationTestOptions>
{
    const string Source = """
        namespace Test;

        [GenerateService]
        public class MyService { }
        """;

    [Test]
    public async Task IdenticalRerun_AllStagesCached(CancellationToken cancellationToken)
    {
        var result = await GenerateIncrementalAsync([Source], cancellationToken: cancellationToken);

        var second = StepReasons(result.Runs[1]);
        string[] frameworkStages =
        [
            "GetMSBuildPropertyValue_EmitServiceRegistrationInfo",
            "GetGenerationConfiguration",
            "GetGenerationContext_EmptyCapabilities",
            "ForAttribute_GenerateServiceAttribute",
        ];
        await Assert.That(
            frameworkStages.All(stage =>
                second.TryGetValue(stage, out var reasons)
                && reasons.All(r => r is StepReason.Cached or StepReason.Unchanged))).IsTrue();
    }
}
```

**Why the example filters to `frameworkStages`:** `ServiceRegistrationGenerator` emits its own
`GenerateServiceAttribute` via post-initialization output, which is regenerated as a new `SyntaxTree` each
run. That makes Roslyn's *internal* `ForAttributeWithMetadataName` `Compilation` step legitimately report
`Modified` on an identical rerun. Asserting on the framework-named stages (which stay `Cached`/`Unchanged`)
is the meaningful check. If your generator does not depend on its own post-init output, the stricter
"every tracked step is `Cached`/`Unchanged`" assertion (as in the framework `IncrementalPipelineCacheTests`)
is correct.

Per-run MSBuild-property changes are supplied with `new IncrementalRunInput(sources, [("build_property.X", "value")])`.

## License

This project is licensed under the MIT license.
Files in this package (2)
  • SKILL.md 24 KB
  • references/test-modernisation.md 3 KB

Versions

The database has no other skills named purview-sourcegenerator-testing.

Database snapshot: 13.11.2026 · package version f81286e9709b (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100purview-sourcegenerator-tunit-tests
    Use when writing TUnit tests for source generators, diagnostic analyzers, code fixes or refactorings built on Purview.SourceGeneratorFramework - choosing the correct TUnit base class and method, customising SourceGeneratorTestOptions, using the TUnit assertion extensions…
  • 100purview-sourcegenerator-attribute-data-models
    Use when declaring or fixing a [Generate] attribute-data model in Purview.SourceGeneratorFramework - mapping constructor and named arguments with [Argument]/[Property], defaults, reusing a shape with [NestedModel], reading an ADM0001-ADM0015 diagnostic, or testing that a model…
  • 100pm-purview
    Use when the user asks which Purview (purview.dev) tools, packages, or practices fit this project; when starting a new or empty project and choosing its stack and scale; or when pm-init, pm-discover, pm-shape, or pm-strategy finds a .NET need that a Purview project covers…
  • 100orchardcore-source-generators
    Skill for using Orchard Core source generators and build-time analyzers. Covers the GenerateArguments attribute for compile-time named argument types, anonymous type interceptors for .NET 9+, PropertyBasedNamedEnumerable, the GenerateShape attribute and shape factory…
  • 100roslyn-analyzers
    Build, review, debug, package, and test Roslyn diagnostic analyzers, code fix providers, and incremental source generators. Use for DiagnosticAnalyzer, CodeFixProvider, IIncrementalGenerator, IOperation analysis, Microsoft.CodeAnalysis dependency pinning, Roslyn test harnesses,…
  • 100dotnet-analyzers
    Run dotnet format to fix code style and analyzer diagnostics (IDE0005, CA*, SA*). Use when fixing unnecessary usings, code style violations, analyzer warnings, or verifying format compliance

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.