Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S2187 FP: Support test attributes deriving from ITestBuilder in NUnit #5564

Closed
dammejed opened this issue Apr 14, 2022 · 0 comments · Fixed by #5565
Closed

Fix S2187 FP: Support test attributes deriving from ITestBuilder in NUnit #5564

dammejed opened this issue Apr 14, 2022 · 0 comments · Fixed by #5565
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@dammejed
Copy link
Contributor

Description

I'm seeing S2187 reported when using test fixtures that have test methods that derive from AutoData (an attribute defined in Autofixture.NUnit3) instead of the built-in Test, TestMethod, etc attributes from NUnit.

Such attributes implement the ITestBuilder interface from NUnit, and these tests are correctly detected and run by visual studio.

Repro steps

Create a test fixture in a correctly-configured Test project that uses the NUnit, AutoFixture, and Autofixture.NUnit3 packages, with the following code:

using AutoFixture.NUnit3;
using NUnit.Framework;

namespace FalsePositive
{
    [TestFixture]
    public class FalsePositiveS2187TestFixture
    {
        [AutoData]
        public void AutoDataTest(string sut)
        {
            Assert.NotNull(sut);
        }
    }
}

Expected behavior

No S2187 is raised.

Actual behavior

S2187 is reported on the FalsePositiveS2187TestFixture class.

Known workarounds

  • Suppress the issue.
  • Put a superfluous Test attribute on at least one method in the affected class.

Related information

  • Visual Studio 17.0.5
  • SonarLint for VS 2022 version 6.1.0.45943
  • I don't know what version of the C# plugin on SonarQube, but we have SonarQube 9.4.0.

As far as I understand it, this could be fixed by additionally blessing methods with attributes that implement the ITestBuilder interface, rather than the specific ones provided by NUnit.
That seems like it'd be fairly straightforward-- I'd be happy to investigate doing so myself.

@dammejed dammejed changed the title S2187 reported when using test attributes deriving from ITestMethodBuilder in NUnit tests. S2187 reported when using test attributes deriving from ITestBuilder in NUnit tests. Apr 14, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Apr 19, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 8.38 milestone Apr 19, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S2187 reported when using test attributes deriving from ITestBuilder in NUnit tests. Fix S2187 FP: Support test attributes deriving from ITestBuilder in NUnit Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants