diff --git a/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md b/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md index 7dc5acd353..189a13b38b 100644 --- a/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md +++ b/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md @@ -6,3 +6,4 @@ Rule ID | Category | Severity | Notes --------|----------|----------|------- MSTEST0084 | Usage | Info | OSPlatformAttributesShouldBeConsistentAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0084) +MSTEST0085 | Usage | Info | TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0085) diff --git a/src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs b/src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs index 0681f02b5d..60dfec653f 100644 --- a/src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs +++ b/src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs @@ -89,4 +89,5 @@ internal static class DiagnosticIds public const string InheritedMemberFromDifferentMSTestVersionRuleId = "MSTEST0082"; public const string UseExecutableConditionAttributeInsteadOfProcessCheckRuleId = "MSTEST0083"; public const string OSPlatformAttributesShouldBeConsistentRuleId = "MSTEST0084"; + public const string TestClassAttributeShouldNotBeAppliedToAbstractClassRuleId = "MSTEST0085"; } diff --git a/src/Analyzers/MSTest.Analyzers/Resources.resx b/src/Analyzers/MSTest.Analyzers/Resources.resx index 09055a20ab..93991e49db 100644 --- a/src/Analyzers/MSTest.Analyzers/Resources.resx +++ b/src/Analyzers/MSTest.Analyzers/Resources.resx @@ -391,6 +391,18 @@ The type declaring these methods should also respect the following rules: Test classes should have valid layout + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class should have at least one test method or be 'static' with method(s) marked by '[AssemblyInitialize]' and/or '[AssemblyCleanup]'. {Locked="[AssemblyInitialize]"}{Locked="[AssemblyCleanup]"}{Locked="static"}{Locked="class"} diff --git a/src/Analyzers/MSTest.Analyzers/TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer.cs b/src/Analyzers/MSTest.Analyzers/TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer.cs new file mode 100644 index 0000000000..82060d3622 --- /dev/null +++ b/src/Analyzers/MSTest.Analyzers/TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer.cs @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Immutable; + +using Analyzer.Utilities.Extensions; + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +using MSTest.Analyzers.Helpers; + +namespace MSTest.Analyzers; + +/// +/// MSTEST0085: . +/// +[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] +public sealed class TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer : DiagnosticAnalyzer +{ + private static readonly LocalizableResourceString Title = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassTitle), Resources.ResourceManager, typeof(Resources)); + private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat), Resources.ResourceManager, typeof(Resources)); + private static readonly LocalizableResourceString Description = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassDescription), Resources.ResourceManager, typeof(Resources)); + + /// + /// Gets the diagnostic descriptor reported by this analyzer. + /// + public static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( + DiagnosticIds.TestClassAttributeShouldNotBeAppliedToAbstractClassRuleId, + Title, + MessageFormat, + Description, + Category.Usage, + DiagnosticSeverity.Info, + isEnabledByDefault: true); + + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(context => + { + if (context.Compilation.TryGetOrCreateTypeByMetadataName( + WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute, + out INamedTypeSymbol? testClassAttributeSymbol)) + { + context.RegisterSymbolAction( + context => AnalyzeSymbol(context, testClassAttributeSymbol), + SymbolKind.NamedType); + } + }); + } + + private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testClassAttributeSymbol) + { + var namedTypeSymbol = (INamedTypeSymbol)context.Symbol; + if (namedTypeSymbol.TypeKind != TypeKind.Class + || !namedTypeSymbol.IsAbstract + || namedTypeSymbol.IsStatic) + { + return; + } + + foreach (AttributeData attribute in namedTypeSymbol.GetAttributes()) + { + if (attribute.AttributeClass.Inherits(testClassAttributeSymbol) + && attribute.ApplicationSyntaxReference is { } syntaxReference) + { + context.ReportDiagnostic(syntaxReference.CreateDiagnostic(Rule, context.CancellationToken, namedTypeSymbol.Name)); + } + } + } +} diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf index 0292c931b7..4594843cdf 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf @@ -985,6 +985,21 @@ Typ deklarující tyto metody by měl také respektovat následující pravidla: Použijte Assert místo StringAssert {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid Testovací class {0} by měla být platná. diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf index 702503f595..cead7dad3f 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf @@ -985,6 +985,21 @@ Der Typ, der diese Methoden deklariert, sollte auch die folgenden Regeln beachte Verwenden Sie „Assert“ anstelle von „StringAssert“. {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid Die Test class „{0}“ muss gültig sein diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf index 090a19821e..af83b79b6a 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf @@ -985,6 +985,21 @@ El tipo que declara estos métodos también debe respetar las reglas siguientes: Usar 'Assert' en lugar de 'StringAssert' {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid La class de prueba '{0}' debe ser válida diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf index bec31d6c1c..eaf2f48e6a 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf @@ -985,6 +985,21 @@ Le type déclarant ces méthodes doit également respecter les règles suivantes Utilisez « Assert » au lieu de « StringAssert » {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid La classe de test « {0} » doit être valide diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf index a52a6c23e4..43833e5f62 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf @@ -985,6 +985,21 @@ Anche il tipo che dichiara questi metodi deve rispettare le regole seguenti: Usa 'Assert' invece di 'StringAssert' {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid La classe di test '{0}' deve essere valida diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf index c021fd0fa0..70fffe2cf0 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf @@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules: 'StringAssert' の代わりに 'Assert' を使用 {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid テスト class '{0}' は有効である必要があります diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf index d6ab440445..51456d2a89 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf @@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules: 'StringAssert' 대신 'Assert' 사용 {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid 테스트 class '{0}'은(는) 유효해야 합니다. diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf index 56aaeb7397..ae5448db14 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf @@ -985,6 +985,21 @@ Typ deklarujący te metody powinien również przestrzegać następujących regu Użyj ciągu „Assert” zamiast ciągu „StringAssert” {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid Test class '{0}' should be valid diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf index c8b63e85fb..3ec52f67b2 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf @@ -985,6 +985,21 @@ O tipo que declara esses métodos também deve respeitar as seguintes regras: Usar "Assert" em vez de "StringAssert" {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid A classe de teste ''{0}'' deve ser válida diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf index 2bfd6a9c72..0e4fc1d100 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf @@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules: Используйте "Assert" вместо "StringAssert" {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid Тестовый class "{0}" должен быть допустимым diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf index e343b6abfc..f95f5341c9 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf @@ -985,6 +985,21 @@ Bu metotları bildiren türün ayrıca aşağıdaki kurallara uyması gerekir: ‘StringAssert’ yerine ‘Assert’ kullanın {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid '{0}' test class geçerli olmalıdır diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf index 061f108db8..b4fd9a30ad 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf @@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules: 使用 'Assert' 而不是 'StringAssert' {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid 测试 class“{0}”应该有效 diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf index 9595e24e82..8eb125ab63 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf @@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules: 使用 'Assert' 而不是 'StringAssert' {Locked="StringAssert"}{Locked="Assert"} + + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute. + {Locked="MSTest"}{Locked="[TestClass]"} + + + '[TestClass]' has no effect on abstract class '{0}' + '[TestClass]' has no effect on abstract class '{0}' + {0} is the class name. {Locked="[TestClass]"} + + + '[TestClass]' should not be applied to abstract classes + '[TestClass]' should not be applied to abstract classes + {Locked="[TestClass]"} + Test class '{0}' should be valid 測試 class '{0}' 應為有效 diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzerTests.cs new file mode 100644 index 0000000000..82d2b8ea1d --- /dev/null +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzerTests.cs @@ -0,0 +1,138 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier< + MSTest.Analyzers.TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer, + Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; +using VerifyVB = MSTest.Analyzers.Test.VisualBasicCodeFixVerifier< + MSTest.Analyzers.TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer, + Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; + +namespace MSTest.Analyzers.Test; + +[TestClass] +public sealed class TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzerTests +{ + [TestMethod] + public async Task WhenTestClassAttributeIsAppliedToAbstractClass_Diagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [{|#0:TestClass|}] + public abstract class BaseTests + { + } + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer.Rule) + .WithLocation(0) + .WithArguments("BaseTests")); + } + + [TestMethod] + public async Task WhenTestClassAttributeIsAppliedToAbstractRecord_Diagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [{|#0:TestClass|}] + public abstract record BaseTests; + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer.Rule) + .WithLocation(0) + .WithArguments("BaseTests")); + } + + [TestMethod] + public async Task WhenDerivedTestClassAttributeIsAppliedToAbstractClass_Diagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public sealed class CustomTestClassAttribute : TestClassAttribute + { + } + + [{|#0:CustomTestClass|}] + public abstract class BaseTests + { + } + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer.Rule) + .WithLocation(0) + .WithArguments("BaseTests")); + } + + [TestMethod] + public async Task WhenTestClassAttributeIsAppliedToConcreteClass_NoDiagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTests + { + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenTestClassAttributeIsAppliedToStaticClass_NoDiagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public static class AssemblyFixtures + { + [AssemblyInitialize] + public static void Initialize(TestContext testContext) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenAbstractClassHasNoTestClassAttribute_NoDiagnostic() + { + string code = """ + public abstract class BaseTests + { + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenTestClassAttributeIsAppliedToMustInheritClass_Diagnostic() + { + string code = """ + Imports Microsoft.VisualStudio.TestTools.UnitTesting + + <{|#0:TestClass|}> + Public MustInherit Class BaseTests + End Class + """; + + await VerifyVB.VerifyAnalyzerAsync( + code, + VerifyVB.Diagnostic(TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer.Rule) + .WithLocation(0) + .WithArguments("BaseTests")); + } +}