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:
- For
NUnit
, a class is marked with TestFixture
but does not contain any method marked with Test
,
TestCase
, TestCaseSource
, or Theory
.
- For
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.
Exceptions
There are scenarios where not having any test cases within a test class is perfectly acceptable and not seen as a problem.
Abstract classes
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:
Derived classes that inherit test cases from a base class
A base class containing one or more test cases to provide generic test cases is also considered a compliant scenario.
Classes that contain AssemblyInitialize
or AssemblyCleanup
methods
This 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: