testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Analyzer: Test class should have test method

Open Evangelink opened this issue 1 year ago • 4 comments

Summary

Title: Test class should have test method Category: Design Description: Test class should have at least one test method, be abstract or static Default severity: Info

AB#1950783

Evangelink avatar Feb 01 '24 14:02 Evangelink

✅ Successfully linked to Azure Boards work item(s):

testplatform-bot avatar Feb 02 '24 15:02 testplatform-bot

From @AliveDevil

Ah, so

[TestClass]
static class GlobalState {
 [AssemblyInitializer] …

would work? If this is the case, the wording "non-abstract or static type […] should contain at least one test method" should not include "static" (or rephrase that it applies to non-abstract and non-static / non (abstract or static)), then the analyzer can be stricter for these types.

Evangelink avatar Feb 21 '24 15:02 Evangelink

I agree with your comment @AliveDevil, the wording is not ideal and maybe we should only mention the exception cases in the rule documentation page but not on the analyzer description.

The following examples are cases where the rule should not trigger:

  1. Test class with no tests but some [AssemblyInitialize] or/and [AssemblyCleanup] methods. Usually this type is static and doesn't contain anything else but some helpers but this is not hard constraint so it's probably easier to just check the presence of the attributes mentioned.

~~2. Test class with some [ClassInitialize] or [ClassCleanup] with inheritance mode. The ideal case is that this class is abstract but this is not a requirement so once again we could be looking only at the presence of the attributes.~~

Note that the rule could apply to derived types from [TestClass] because the related virtual method forces to return derived or instances of [TstMethod].

Evangelink avatar Feb 21 '24 16:02 Evangelink

Correction for point 2, the class doesn't need to be marked as [TestClass] so we only need to avoid raising for case 1.

Evangelink avatar Feb 21 '24 16:02 Evangelink