Skip to content

Conversation

@Alex-Sob
Copy link

@Alex-Sob Alex-Sob commented Sep 17, 2025

I would like to propose some improvements for code analyzer tests.

I have worked with analyzers in a few .NET repos, running and verifying a test often looks like this:

public async Task Test()
{
    var source = "...";
    var expectedDiagnostics = new[] { ... };
    await VerifyCS.VerifyAnalyzerAsync(source, expectedDiagnostics); // VerifyCS is a type alias
}

Proposal

I believe that a factory method that creates a CSharpAnalyzerTest<TAnalyzer, TVerifier> could improve both usability and performance of tests:

public partial class CSharpAnalyzerTest<TAnalyzer, TVerifier> : AnalyzerTest<TVerifier>
{
    // For net9.0 the second parameter could be 'params ReadOnlySpan<DiagnosticResult> expectedDiagnostics'
    public static CSharpAnalyzerTest<TAnalyzer, TVerifier> Create([StringSyntax("C#-test")] string source, params DiagnosticResult[] expectedDiagnostics);
}

public static class CSharpAnalyzerTestExtensions
{
    public static CSharpAnalyzerTest<TAnalyzer, TVerifier> WithSource<TAnalyzer, TVerifier>(this CSharpAnalyzerTest<TAnalyzer, TVerifier> test, [StringSyntax("C#-test")] string source);
}

Using such helper methods could have some benefits:

  • It could improve performance of tests in .NET 9 if diagnostics are passed as params ReadOnlySpan<DiagnosticResult> that avoids array allocation. This is not possible with the async VerifyAnalyzerAsync method.

    I tried to use <LangVersion Condition="<if net9.0 or greater>">13</LangVersion> in the project, but this results build error "error CS1617: Invalid option '13' for /langversion" (is this an msbuild/compiler bug?).

  • The [SyntaxAttribute("C#-test")] attribute applied to source parameter will enable syntax highlighting for test source code in Visual Studio. Please see description of PR #63642 that I submitted to ASP.NET Core repo.

    SyntaxAttribute is available starting from .NET 7, this is why I added this target framework in Directory.Build.props.

  • This allows to use additional helper methods that can be used to customize the test, to provide additional sources or test configuration if needed. For example, if each test needs some common code, test source code can be split into multiple 'files' and additional source can be provided using WithSource extension:

    private const string Program = """
        public class Program
        {
            public static void Main() { }
        }
        """;
    
    public async Task Test()
    {
        var source = "...";
        var test = CSTest.Create(source, CreateDiagnostic()).WithSource(Program); // CSTest is type alias
        await test.RunAsync();
    }
  • This will avoid the need to create 'local' helper methods in analyzer test classes that need to customize a test in some way.

Examples

I also suggested such changes in PR #63684 in ASP.NET Core repo and used to write tests in the following PRs:
#63657
#63819

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant