-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Add PH2161 analyzer to avoid [Retry] attribute and RetryBaseAttribute derivatives #977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-976
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # PH2159: Avoid Retry attribute | ||
|
|
||
| | Property | Value | | ||
| |--|--| | ||
| | Package | [Philips.CodeAnalysis.MsTestAnalyzers](https://www.nuget.org/packages/Philips.CodeAnalysis.MsTestAnalyzers) | | ||
| | Diagnostic ID | PH2159 | | ||
| | Category | [MsTest](../MsTest.md) | | ||
| | Analyzer | [AvoidAttributeAnalyzer](https://github.com/philips-software/roslyn-analyzers/blob/main/Philips.CodeAnalysis.MsTestAnalyzers/AvoidAttributeAnalyzer.cs) | ||
| | CodeFix | No | | ||
| | Severity | Error | | ||
| | Enabled By Default | Yes | | ||
|
|
||
| ## Introduction | ||
|
|
||
| MSTest has a [Retry] attribute to attempt to overcome flaky tests. It would be preferable to avoid the flaky test. | ||
|
|
||
| More thoroughly, one should avoid any attribute that derives from RetryBaseAttribute. | ||
|
|
||
| ## How to solve | ||
|
|
||
| Remove the flaky test or fix the underlying cause of the flakiness. Rely on making your tests deterministic rather than retrying them. | ||
|
|
||
| ## Example | ||
|
|
||
| Code that triggers a diagnostic: | ||
| ``` cs | ||
| [TestMethod] | ||
| [Retry(3)] | ||
| public void BadTestMethod() | ||
| { | ||
| Assert.AreEqual(1, 1); | ||
| } | ||
| ``` | ||
|
|
||
| Code that also triggers a diagnostic (custom attribute deriving from RetryBaseAttribute): | ||
| ``` cs | ||
| public class CustomRetryAttribute : RetryBaseAttribute | ||
| { | ||
| public CustomRetryAttribute(int count) : base(count) { } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [CustomRetry(5)] | ||
| public void BadTestMethod() | ||
| { | ||
| Assert.AreEqual(1, 1); | ||
| } | ||
| ``` | ||
|
|
||
| ## Similar Analyzers | ||
|
|
||
| The following analyzers detect other situations that prevent tests from executing (either unwittingly or nefariously): | ||
|
|
||
| | ID | Title | | ||
| |--|--| | ||
| | [PH2013](.\PH2013.md) | Avoid Ignore attribute | | ||
| | [PH2034](.\PH2034.md) | Test methods must be in TestClass | | ||
| | [PH2036](.\PH2036.md) | Test methods must be public | | ||
| | [PH2038](.\PH2038.md) | Test classes must be public | | ||
| | [PH2059](.\PH2059.md) | Public methods must be TestMethods | | ||
|
|
||
| ## Configuration | ||
|
|
||
| This analyzer does not offer any special configuration. The general ways of [suppressing](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings) diagnostics apply. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
Philips.CodeAnalysis.Test/MsTest/AvoidRetryAttributeAnalyzerTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // © 2025 Koninklijke Philips N.V. See License.md in the project root for license information. | ||
|
|
||
| using System.Text.RegularExpressions; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Philips.CodeAnalysis.Common; | ||
| using Philips.CodeAnalysis.MsTestAnalyzers; | ||
| using Philips.CodeAnalysis.Test.Helpers; | ||
| using Philips.CodeAnalysis.Test.Verifiers; | ||
|
|
||
| namespace Philips.CodeAnalysis.Test.MsTest | ||
| { | ||
| [TestClass] | ||
| public class AvoidRetryAttributeAnalyzerTest : DiagnosticVerifier | ||
| { | ||
| [TestMethod] | ||
| [TestCategory(TestDefinitions.UnitTests)] | ||
| public async Task AvoidRetryAttributeTestAsync() | ||
| { | ||
| var givenText = @" | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting | ||
| { | ||
| public class RetryAttribute : System.Attribute | ||
| { | ||
| public RetryAttribute(int count) { } | ||
| } | ||
| } | ||
|
|
||
| [TestClass] | ||
| class Foo | ||
| { | ||
| [TestMethod] | ||
| [Retry(3)] | ||
| public void TestMethod() | ||
| { | ||
| Assert.IsTrue(true); | ||
| } | ||
| } | ||
| "; | ||
|
|
||
| DiagnosticResult expected = new() | ||
| { | ||
| Id = DiagnosticId.AvoidRetryAttribute.ToId(), | ||
| Message = new Regex(".*"), | ||
| Severity = DiagnosticSeverity.Error, | ||
| Locations = new[] | ||
| { | ||
| new DiagnosticResultLocation("Test0.cs", 16, 4) | ||
| } | ||
| }; | ||
|
|
||
| await VerifyDiagnostic(givenText, expected).ConfigureAwait(false); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory(TestDefinitions.UnitTests)] | ||
| public async Task AvoidCustomRetryAttributeTestAsync() | ||
| { | ||
| var givenText = @" | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting | ||
| { | ||
| public class RetryBaseAttribute : System.Attribute | ||
| { | ||
| public RetryBaseAttribute(int count) { } | ||
| } | ||
| } | ||
|
|
||
| public class CustomRetryAttribute : Microsoft.VisualStudio.TestTools.UnitTesting.RetryBaseAttribute | ||
| { | ||
| public CustomRetryAttribute(int count) : base(count) { } | ||
| } | ||
|
|
||
| [TestClass] | ||
| class Foo | ||
| { | ||
| [TestMethod] | ||
| [CustomRetry(5)] | ||
| public void TestMethod() | ||
| { | ||
| Assert.IsTrue(true); | ||
| } | ||
| } | ||
| "; | ||
|
|
||
| DiagnosticResult expected = new() | ||
| { | ||
| Id = DiagnosticId.AvoidRetryAttribute.ToId(), | ||
| Message = new Regex(".*"), | ||
| Severity = DiagnosticSeverity.Error, | ||
| Locations = new[] | ||
| { | ||
| new DiagnosticResultLocation("Test0.cs", 21, 4) | ||
| } | ||
| }; | ||
|
|
||
| await VerifyDiagnostic(givenText, expected).ConfigureAwait(false); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory(TestDefinitions.UnitTests)] | ||
| public async Task NonRetryAttributesAreIgnoredAsync() | ||
| { | ||
| var givenText = @" | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [TestClass] | ||
| class Foo | ||
| { | ||
| [TestMethod] | ||
| [TestCategory(""Unit"")] | ||
| public void TestMethod() | ||
| { | ||
| Assert.IsTrue(true); | ||
| } | ||
| } | ||
| "; | ||
|
|
||
| await VerifySuccessfulCompilation(givenText).ConfigureAwait(false); | ||
| } | ||
|
|
||
| protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() | ||
| { | ||
| return new AvoidAttributeAnalyzer(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's switch to 2161. (Another PR has claimed 2159 and 2160). We'll need to update our PH2159.md file name and contents as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use PH2161 as requested. Changed the diagnostic ID in DiagnosticId.cs and AvoidAttributeAnalyzer.cs, renamed the documentation file from PH2159.md to PH2161.md, and updated all references. All 2013 tests pass. (commit 5d8c238)