Skip to content

Commit 480cb73

Browse files
authored
MSTEST0015: Test method should not be ignored (#2429)
1 parent 469595c commit 480cb73

21 files changed

+417
-1
lines changed

src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ MSTEST0011 | Usage | Warning | ClassCleanupShouldBeValidAnalyzer, [Documentation
1212
MSTEST0012 | Usage | Warning | AssemblyInitializeShouldBeValidAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0012)
1313
MSTEST0013 | Usage | Warning | AssemblyCleanupShouldBeValidAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0013)
1414
MSTEST0014 | Usage | Warning | DataRowShouldBeValidAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-analyzers-MSTEST0014)
15+
MSTEST0015 | Design | Info | TestMethodShouldNotBeIgnoredAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-analyzers-MSTEST0015)

src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ internal static class DiagnosticIds
1919
public const string AssemblyInitializeShouldBeValidRuleId = "MSTEST0012";
2020
public const string AssemblyCleanupShouldBeValidRuleId = "MSTEST0013";
2121
public const string DataRowShouldBeValidRuleId = "MSTEST0014";
22+
public const string TestMethodShouldNotBeIgnoredRuleId = "MSTEST0015";
2223
}

src/Analyzers/MSTest.Analyzers/Helpers/WellKnownTypeNames.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ namespace MSTest.Analyzers;
66
// IMPORTANT: Keep this file sorted alphabetically.
77
internal static class WellKnownTypeNames
88
{
9+
public const string MicrosoftVisualStudioTestToolsUnitTestingAssemblyCleanupAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute";
910
public const string MicrosoftVisualStudioTestToolsUnitTestingAssemblyInitializeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute";
1011
public const string MicrosoftVisualStudioTestToolsUnitTestingClassCleanupAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute";
1112
public const string MicrosoftVisualStudioTestToolsUnitTestingClassInitializeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute";
12-
public const string MicrosoftVisualStudioTestToolsUnitTestingAssemblyCleanupAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute";
1313
public const string MicrosoftVisualStudioTestToolsUnitTestingCssIterationAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.CssIterationAttribute";
1414
public const string MicrosoftVisualStudioTestToolsUnitTestingCssProjectStructureAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.CssProjectStructureAttribute";
1515
public const string MicrosoftVisualStudioTestToolsUnitTestingDataRowAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute";
1616
public const string MicrosoftVisualStudioTestToolsUnitTestingDescriptionAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute";
1717
public const string MicrosoftVisualStudioTestToolsUnitTestingDiscoverInternalsAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.DiscoverInternalsAttribute";
1818
public const string MicrosoftVisualStudioTestToolsUnitTestingDoNotParallelizeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelizeAttribute";
1919
public const string MicrosoftVisualStudioTestToolsUnitTestingExpectedExceptionAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute";
20+
public const string MicrosoftVisualStudioTestToolsUnitTestingIgnoreAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.IgnoreAttribute";
2021
public const string MicrosoftVisualStudioTestToolsUnitTestingOwnerAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.OwnerAttribute";
2122
public const string MicrosoftVisualStudioTestToolsUnitTestingParallelizeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute";
2223
public const string MicrosoftVisualStudioTestToolsUnitTestingPriorityAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.PriorityAttribute";

src/Analyzers/MSTest.Analyzers/Resources.Designer.cs

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analyzers/MSTest.Analyzers/Resources.resx

+9
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,15 @@
445445
<data name="TestMethodShouldBeValidTitle" xml:space="preserve">
446446
<value>Test methods should have valid layout</value>
447447
</data>
448+
<data name="TestMethodShouldNotBeIgnoredAnalyzerDescription" xml:space="preserve">
449+
<value>Test methods should not be ignored (marked with '[Ignore]').</value>
450+
</data>
451+
<data name="TestMethodShouldNotBeIgnoredAnalyzerFormat" xml:space="preserve">
452+
<value>Test method '{0}' should not be ignored</value>
453+
</data>
454+
<data name="TestMethodShouldNotBeIgnoredAnalyzerTitle" xml:space="preserve">
455+
<value>Test method should not be ignored</value>
456+
</data>
448457
<data name="UseAttributeOnTestMethodAnalyzerMessageFormat" xml:space="preserve">
449458
<value>[{0}] can only be set on methods marked with [TestMethod]</value>
450459
</data>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Immutable;
5+
6+
using Analyzer.Utilities.Extensions;
7+
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.Diagnostics;
10+
11+
using MSTest.Analyzers.Helpers;
12+
13+
namespace MSTest.Analyzers;
14+
15+
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
16+
public sealed class TestMethodShouldNotBeIgnoredAnalyzer : DiagnosticAnalyzer
17+
{
18+
private static readonly LocalizableResourceString Title = new(nameof(Resources.TestMethodShouldNotBeIgnoredAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
19+
private static readonly LocalizableResourceString Description = new(nameof(Resources.TestMethodShouldNotBeIgnoredAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
20+
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.TestMethodShouldNotBeIgnoredAnalyzerFormat), Resources.ResourceManager, typeof(Resources));
21+
22+
internal static readonly DiagnosticDescriptor TestMethodShouldNotBeIgnoredRule = DiagnosticDescriptorHelper.Create(
23+
DiagnosticIds.TestMethodShouldNotBeIgnoredRuleId,
24+
Title,
25+
MessageFormat,
26+
Description,
27+
Category.Design,
28+
DiagnosticSeverity.Info,
29+
isEnabledByDefault: true);
30+
31+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
32+
= ImmutableArray.Create(TestMethodShouldNotBeIgnoredRule);
33+
34+
public override void Initialize(AnalysisContext context)
35+
{
36+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
37+
context.EnableConcurrentExecution();
38+
context.RegisterCompilationStartAction(context =>
39+
{
40+
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingIgnoreAttribute, out var ignoreAttributeSymbol)
41+
&& context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestMethodAttribute, out var testMethodAttributeSymbol))
42+
{
43+
context.RegisterSymbolAction(
44+
context => AnalyzeSymbol(context, ignoreAttributeSymbol, testMethodAttributeSymbol),
45+
SymbolKind.Method);
46+
}
47+
});
48+
}
49+
50+
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol ignoreAttributeSymbol, INamedTypeSymbol testMethodAttributeSymbol)
51+
{
52+
var methodSymbol = (IMethodSymbol)context.Symbol;
53+
var methodAttributes = methodSymbol.GetAttributes();
54+
bool isTestMethod = false;
55+
bool isMethodIgnored = false;
56+
foreach (var methodAttribute in methodAttributes)
57+
{
58+
// Current method should be a test method or should inherit from the TestMethod attribute.
59+
if (methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol))
60+
{
61+
isTestMethod = true;
62+
}
63+
64+
if (SymbolEqualityComparer.Default.Equals(methodAttribute.AttributeClass, ignoreAttributeSymbol))
65+
{
66+
isMethodIgnored = true;
67+
}
68+
}
69+
70+
if (!isTestMethod || !isMethodIgnored)
71+
{
72+
return;
73+
}
74+
75+
context.ReportDiagnostic(methodSymbol.CreateDiagnostic(TestMethodShouldNotBeIgnoredRule, methodSymbol.Name));
76+
}
77+
}

src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,21 @@
575575
<target state="translated">Testovací metody by měly mít platné rozložení.</target>
576576
<note />
577577
</trans-unit>
578+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
579+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
580+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
581+
<note />
582+
</trans-unit>
583+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
584+
<source>Test method '{0}' should not be ignored</source>
585+
<target state="new">Test method '{0}' should not be ignored</target>
586+
<note />
587+
</trans-unit>
588+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
589+
<source>Test method should not be ignored</source>
590+
<target state="new">Test method should not be ignored</target>
591+
<note />
592+
</trans-unit>
578593
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
579594
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
580595
<target state="translated">[{0}] lze nastavit pouze u metod označených pomocí metody [TestMethod].</target>

src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,21 @@
569569
<target state="translated">Testmethoden müssen über ein gültiges Layout verfügen.</target>
570570
<note />
571571
</trans-unit>
572+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
573+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
574+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
575+
<note />
576+
</trans-unit>
577+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
578+
<source>Test method '{0}' should not be ignored</source>
579+
<target state="new">Test method '{0}' should not be ignored</target>
580+
<note />
581+
</trans-unit>
582+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
583+
<source>Test method should not be ignored</source>
584+
<target state="new">Test method should not be ignored</target>
585+
<note />
586+
</trans-unit>
572587
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
573588
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
574589
<target state="translated">[{0}] kann nur für Methoden festgelegt werden, die mit [TestMethod] markiert sind.</target>

src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,21 @@
569569
<target state="translated">Los métodos de prueba deben tener un diseño válido</target>
570570
<note />
571571
</trans-unit>
572+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
573+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
574+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
575+
<note />
576+
</trans-unit>
577+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
578+
<source>Test method '{0}' should not be ignored</source>
579+
<target state="new">Test method '{0}' should not be ignored</target>
580+
<note />
581+
</trans-unit>
582+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
583+
<source>Test method should not be ignored</source>
584+
<target state="new">Test method should not be ignored</target>
585+
<note />
586+
</trans-unit>
572587
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
573588
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
574589
<target state="translated">[{0}] solo se puede establecer en métodos marcados con [TestMethod]</target>

src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,21 @@
569569
<target state="translated">Les méthodes de test doivent avoir une disposition valide</target>
570570
<note />
571571
</trans-unit>
572+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
573+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
574+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
575+
<note />
576+
</trans-unit>
577+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
578+
<source>Test method '{0}' should not be ignored</source>
579+
<target state="new">Test method '{0}' should not be ignored</target>
580+
<note />
581+
</trans-unit>
582+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
583+
<source>Test method should not be ignored</source>
584+
<target state="new">Test method should not be ignored</target>
585+
<note />
586+
</trans-unit>
572587
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
573588
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
574589
<target state="translated">[{0}] ne peut être défini que sur les méthodes marquées avec [TestMethod]</target>

src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,21 @@
569569
<target state="translated">I metodi di test dovrebbero avere un layout valido</target>
570570
<note />
571571
</trans-unit>
572+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
573+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
574+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
575+
<note />
576+
</trans-unit>
577+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
578+
<source>Test method '{0}' should not be ignored</source>
579+
<target state="new">Test method '{0}' should not be ignored</target>
580+
<note />
581+
</trans-unit>
582+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
583+
<source>Test method should not be ignored</source>
584+
<target state="new">Test method should not be ignored</target>
585+
<note />
586+
</trans-unit>
572587
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
573588
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
574589
<target state="translated">[{0}] può essere impostato solo su metodi contrassegnati con [TestMethod]</target>

src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,21 @@
570570
<target state="translated">テスト メソッドには有効なレイアウトが必要です</target>
571571
<note />
572572
</trans-unit>
573+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
574+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
575+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
576+
<note />
577+
</trans-unit>
578+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
579+
<source>Test method '{0}' should not be ignored</source>
580+
<target state="new">Test method '{0}' should not be ignored</target>
581+
<note />
582+
</trans-unit>
583+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
584+
<source>Test method should not be ignored</source>
585+
<target state="new">Test method should not be ignored</target>
586+
<note />
587+
</trans-unit>
573588
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
574589
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
575590
<target state="translated">[{0}] は、[TestMethod] でマークされたメソッドにのみ設定できます</target>

src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,21 @@
570570
<target state="translated">테스트 메서드에는 유효한 레이아웃이 있어야 합니다.</target>
571571
<note />
572572
</trans-unit>
573+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
574+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
575+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
576+
<note />
577+
</trans-unit>
578+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
579+
<source>Test method '{0}' should not be ignored</source>
580+
<target state="new">Test method '{0}' should not be ignored</target>
581+
<note />
582+
</trans-unit>
583+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
584+
<source>Test method should not be ignored</source>
585+
<target state="new">Test method should not be ignored</target>
586+
<note />
587+
</trans-unit>
573588
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
574589
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
575590
<target state="translated">[{0}]은(는) [TestMethod] 표시된 메서드에만 설정할 수 있습니다.</target>

src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,21 @@
569569
<target state="translated">Metody testowe powinny mieć prawidłowy układ</target>
570570
<note />
571571
</trans-unit>
572+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerDescription">
573+
<source>Test methods should not be ignored (marked with '[Ignore]').</source>
574+
<target state="new">Test methods should not be ignored (marked with '[Ignore]').</target>
575+
<note />
576+
</trans-unit>
577+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerFormat">
578+
<source>Test method '{0}' should not be ignored</source>
579+
<target state="new">Test method '{0}' should not be ignored</target>
580+
<note />
581+
</trans-unit>
582+
<trans-unit id="TestMethodShouldNotBeIgnoredAnalyzerTitle">
583+
<source>Test method should not be ignored</source>
584+
<target state="new">Test method should not be ignored</target>
585+
<note />
586+
</trans-unit>
572587
<trans-unit id="UseAttributeOnTestMethodAnalyzerMessageFormat">
573588
<source>[{0}] can only be set on methods marked with [TestMethod]</source>
574589
<target state="translated">[{0}] można ustawić tylko dla metod oznaczonych znakiem [TestMethod]</target>

0 commit comments

Comments
 (0)