To ensure proper testing, it is important to include test cases in a test class. If a test class does not have any test cases, it can give the wrong impression that the class being tested has been thoroughly tested, when in reality, it has not.
This rule will raise an issue when any of these conditions are met:
NUnit, a class is marked with TestFixture but does not contain any method marked with Test,
TestCase, TestCaseSource, or Theory. MSTest, a class is marked with TestClass but does not contain any method marked with TestMethod or
DataTestMethod. It does not apply to xUnit since xUnit does not require a test
class attribute.
There are scenarios where not having any test cases within a test class is perfectly acceptable and not seen as a problem.
To facilitate the creation of common test cases, test logic, or test infrastructure, it is advisable to use a base class.
Additionally, in both NUnit and MSTest, abstract classes that are annotated with their respective attributes
(TestFixture in NUnit and TestClass in MSTest) are automatically ignored.
Therefore, there is no need to raise an issue in this particular scenario.
More information here:
A base class containing one or more test cases to provide generic test cases is also considered a compliant scenario.
AssemblyInitialize or AssemblyCleanup methodsThis particular exception scenario only applies to the MSTest test framework.
The AssemblyInitialize and AssemblyCleanup attributes are used to annotate methods that are executed only once at the
beginning and at the end of a test run. These attributes can only be applied once per assembly.
It is logical to have a dedicated class for these methods, and this scenario is also considered compliant.
Furthermore, it is important to note that the test engine will execute a method annotated with either the AssemblyInitialize or
AssemblyCleanup attribute only if that method is part of a class annotated with the TestClass attribute.
More information here:
To fix this issue in MSTest, it is important that all test classes annotated with the [TestClass] attribute contain at
least one test case.
To achieve this, at least one method needs to be annotated with one of the following method attributes:
TestMethod DataTestMethod
[TestClass]
public class SomeOtherClassTest { } // Noncompliant: no test
[TestClass]
public class SomeOtherClassTest
{
[TestMethod]
public void SomeMethodShouldReturnTrue() { }
}
To fix this issue in NUnit, it is important that all test classes annotated with the [TestFixture] attribute contain at
least one test case.
To achieve this, at least one method needs to be annotated with one of the following method attributes:
Test TestCase TestCaseSource Theory
[TestFixture]
public class SomeClassTest { } // Noncompliant: no test
[TestFixture]
public class SomeClassTest
{
[Test]
public void SomeMethodShouldReturnTrue() { }
}