diff --git a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/TestClassShouldHaveTestMethod.cs b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/TestClassShouldHaveTestMethod.cs index f79bb32e6d5..3999c7393fd 100644 --- a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/TestClassShouldHaveTestMethod.cs +++ b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/TestClassShouldHaveTestMethod.cs @@ -1,4 +1,4 @@ -/* +/* * SonarAnalyzer for .NET * Copyright (C) 2015-2017 SonarSource SA * mailto: contact AT sonarsource DOT com @@ -61,8 +61,10 @@ protected override void Initialize(SonarAnalysisContext context) } var classDeclaration = (ClassDeclarationSyntax)c.Node; - var classSymbol = c.SemanticModel.GetDeclaredSymbol(classDeclaration); + var classSymbol = c.SemanticModel.GetDeclaredSymbol(classDeclaration); + if (classSymbol == null || + classSymbol.IsAbstract || !classSymbol.GetAttributes().Any(a => a.AttributeClass.IsAny(HandledTestClassAttributes))) { return; diff --git a/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/TestClassShouldHaveTestMethod.cs b/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/TestClassShouldHaveTestMethod.cs index ce224703583..9fe4c098d68 100644 --- a/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/TestClassShouldHaveTestMethod.cs +++ b/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/TestClassShouldHaveTestMethod.cs @@ -1,4 +1,4 @@ -using System; +using System; using NUnit.Framework; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -58,4 +58,32 @@ class ClassTest9 [TestMethod] public void Foo() { } } + + [TestClass] + public abstract class MyCommonCode1 + { + } + + [TestClass] + public class MySubCommonCode1 : MyCommonCode1 // Noncompliant + { + } + + public class MySubCommonCode11 : MyCommonCode1 // Compliant + { + } + + [TestFixture] + public abstract class MyCommonCode2 + { + } + + [TestFixture] + public class MySubCommonCode2 : MyCommonCode2 // Noncompliant + { + } + + public class MySubCommonCode22 : MyCommonCode2 // Compliant + { + } }