nunit.analyzers icon indicating copy to clipboard operation
nunit.analyzers copied to clipboard

Ensure Classes With TestFixtureAttribute Have at Least One Test

Open JasonBock opened this issue 9 years ago • 6 comments

Description:

If a class is marked with [TestFixture], it must have at least one method marked with [Test] or [TestCase].

Example:

[TestFixture]
public class MyTests { }

To fix it:

[TestFixture]
public class MyTests
{
  [Test]
  public void TestMethod() { }
}

Analyzer Message:

"A test fixture class must have at least one test method."

Default Severity Level:

Error

Code Fixes:

none

JasonBock avatar Nov 29 '16 02:11 JasonBock

This is usually true but not always, for several reasons:

  1. There are other attributes that designate tests, e.g. TestCaseSource. Users can define new attributes that also designate tests.

  2. A TestFixture may inherit from a base class where the tests are defined - that is the tests must exist but not necessarily be defined in the class.

CharliePoole avatar Nov 29 '16 04:11 CharliePoole

@CharliePoole so I'm guessing this isn't possible to do as an analyzer, unless:

  1. You know which attributes constitute as "test" attributes, like Test, TestCase and TestCaseSource, along with a discoverable way to know what other user-created attributes designate tests
  2. The class marked with [TestFixture] is sealed as you know it cannot be the base class to another class.

JasonBock avatar Nov 29 '16 04:11 JasonBock

For all practical purposes, I think that's probably true.

CharliePoole avatar Nov 29 '16 04:11 CharliePoole

@CharliePoole BTW how does a user create their own test designation attributes?

JasonBock avatar Nov 29 '16 05:11 JasonBock

By implementing certain interfaces. Is that something you are able to detect?

CharliePoole avatar Nov 29 '16 06:11 CharliePoole

@CharliePoole Yes. If you give me the rules I should be able to detect that.

JasonBock avatar Nov 29 '16 14:11 JasonBock