nunit.analyzers
nunit.analyzers copied to clipboard
Ensure a least one (relevant) constructor is Public
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.
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 "There must be a public constructor" - does that mean at least 1? Does it matter if it takes arguments or not?
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 I'm guessing that the rules around type conversion for TestFixture
arguments to constructor parameters is the same as the ones for TestCase
?
Probably the same or at least close, but written at different times so could be different.
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]
?
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
Handling public test methods have been extracted into #245
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.