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

Ensure a least one (relevant) constructor is Public

Open JasonBock opened this issue 8 years ago • 9 comments

Description:

A class marked with [TestFixture] must have all of their constructors as public as well as any methods marked with [Test] or [TestCase].

Example:

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

To fix it:

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

Analyzer Message:

"A constructor for a test fixture class must be public." "A test method must be public."

Code Fixes:

Change the visibility value for the constructor or test method to public.

JasonBock avatar Nov 29 '16 02:11 JasonBock

There must be a public constructor but there could be non-public ones as well.

As with #4, you would want to allow for other attributes besides Test and TestCase.

CharliePoole avatar Nov 29 '16 04:11 CharliePoole

@CharliePoole "There must be a public constructor" - does that mean at least 1? Does it matter if it takes arguments or not?

JasonBock avatar Nov 29 '16 04:11 JasonBock

At least 1. Possibly more. The constructor needs to take arguments if arguments are provided. IOW, the constructor has to match the TestFixture arguments in the same way that a method has to match TestCase arguments.

CharliePoole avatar Nov 29 '16 04:11 CharliePoole

@CharliePoole I'm guessing that the rules around type conversion for TestFixture arguments to constructor parameters is the same as the ones for TestCase?

JasonBock avatar Nov 29 '16 05:11 JasonBock

Probably the same or at least close, but written at different times so could be different.

CharliePoole avatar Nov 29 '16 06:11 CharliePoole

This should probably also be aware that [TestFixture] is optional.

@CharliePoole would do a better job of explaining the specifics that I can, but I think any class that contains [Test] methods, is implicitly a [TestFixture]?

ChrisMaddock avatar Nov 29 '16 09:11 ChrisMaddock

Perhaps also check whether the test method is abstract, see https://github.com/nunit/nunit/blob/db5bee0f8bb74d68202377917f6d958c0e139bfd/src/NUnitFramework/framework/Internal/Builders/NUnitTestCaseBuilder.cs#L145

mikkelbu avatar Apr 23 '19 19:04 mikkelbu

Handling public test methods have been extracted into #245

mikkelbu avatar May 21 '20 19:05 mikkelbu

Re-reading the description, I see it talks about classes marked with [TestFixture]. Actually, any class used as a TestFixture needs a public constructor. One way a class becomes a TestFixture is through use of the TestFixtureAttribute, but there are lots of other ways...

  • Classes containing tests
  • Classes inheriting from classes that contain tests
  • Classes inheriting from classes that are marked with [TestFixture]

I don't know whether it's in scope for an analyzer to dig that deep, but if not, it seems wrong to me to do a partial job, especially since fixtures without the attribute seem to be the most usual thing to do nowadays.

CharliePoole avatar May 21 '20 23:05 CharliePoole