From 7cec0a4c50d9a6426213ea6e0cdb65b5c8b21166 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:12:51 +0000 Subject: [PATCH] fix: add clarifying comment and tests for PublicMethodMissingTestAttributeAnalyzer (#4863) The MethodKind.Ordinary filter already correctly excludes property accessors (PropertyGet/PropertySet), event accessors (EventAdd/EventRemove), constructors, destructors, and operators. Added a clarifying comment to document this behavior and new test cases for properties, events, overrides, and static methods to prevent future regressions. --- ...MethodMissingTestAttributeAnalyzerTests.cs | 102 +++++++++++++++++- ...ublicMethodMissingTestAttributeAnalyzer.cs | 3 + 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/TUnit.Analyzers.Tests/PublicMethodMissingTestAttributeAnalyzerTests.cs b/TUnit.Analyzers.Tests/PublicMethodMissingTestAttributeAnalyzerTests.cs index ad1b42724e..f09738aa61 100644 --- a/TUnit.Analyzers.Tests/PublicMethodMissingTestAttributeAnalyzerTests.cs +++ b/TUnit.Analyzers.Tests/PublicMethodMissingTestAttributeAnalyzerTests.cs @@ -143,7 +143,7 @@ public class MyClass : IAsyncDisposable public void MyTest() { } - + public ValueTask DisposeAsync() { return ValueTask.CompletedTask; @@ -152,4 +152,104 @@ public ValueTask DisposeAsync() """ ); } + + [Test] + public async Task Property_Getter_Setter_No_Error() + { + await Verifier + .VerifyAnalyzerAsync( + """ + using TUnit.Core; + + public class MyClass + { + [Test] + public void MyTest() + { + } + + public string? Name { get; set; } + + public int Age + { + get { return 0; } + set { } + } + } + """ + ); + } + + [Test] + public async Task Event_Accessors_No_Error() + { + await Verifier + .VerifyAnalyzerAsync( + """ + using System; + using TUnit.Core; + + public class MyClass + { + [Test] + public void MyTest() + { + } + + public event EventHandler MyEvent + { + add { } + remove { } + } + } + """ + ); + } + + [Test] + public async Task Override_Method_No_Error() + { + await Verifier + .VerifyAnalyzerAsync( + """ + using TUnit.Core; + + public class MyClass + { + [Test] + public void MyTest() + { + } + + public override string ToString() + { + return "test"; + } + } + """ + ); + } + + [Test] + public async Task Static_Method_No_Error() + { + await Verifier + .VerifyAnalyzerAsync( + """ + using TUnit.Core; + + public class MyClass + { + [Test] + public void MyTest() + { + } + + public static void StaticHelper() + { + } + } + """ + ); + } } diff --git a/TUnit.Analyzers/PublicMethodMissingTestAttributeAnalyzer.cs b/TUnit.Analyzers/PublicMethodMissingTestAttributeAnalyzer.cs index 05838c678b..6fe17e4054 100644 --- a/TUnit.Analyzers/PublicMethodMissingTestAttributeAnalyzer.cs +++ b/TUnit.Analyzers/PublicMethodMissingTestAttributeAnalyzer.cs @@ -32,6 +32,9 @@ private void AnalyzeSymbol(SymbolAnalysisContext context) return; } + // MethodKind.Ordinary excludes property accessors (PropertyGet/PropertySet), + // event accessors (EventAdd/EventRemove), constructors, destructors, operators, + // and other compiler-generated method kinds. foreach (var method in methods .Where(x => x.MethodKind == MethodKind.Ordinary) .Where(x => !x.IsAbstract)