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 S2699 FP: AssertionMethodAttribute is ignored when assertion method is inherited #6525

Closed
killergege opened this issue Dec 12, 2022 · 3 comments · Fixed by #6652
Closed
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@killergege
Copy link

Description

When writing a test without an explicit assertion but using a method to perform that assertion, S2699 is raised if the method is from a parent class.
SonarLint doesn't seem to report the issue, it is reported only when scanned on the pipeline with SonarScanner

Repro steps

Reports a S2699:

public class ParentTest
{
     [AssertionMethod]
    protected void TestAssert<T>(T ex)
    {
        Assert.That(ex!.ToString(), Is.EqualTo(""));
    }
}

public class ChildTest : ParentTest
{
    [Test]
    public void Test()
    {
        TestAssert(new Exception("Message", new Exception("Inner exception.")));
    }
}

This doesn't report S2699 :

public class ParentTest
{
     [AssertionMethod]
    protected virtual void TestAssert<T>(T ex)
    {
        Assert.That(ex!.ToString(), Is.EqualTo(""));
    }
}

public class ChildTest : ParentTest
{
    [Test]
    public void Test()
    {
        TestAssert(new Exception("Message", new Exception("Inner exception.")));
    }

    [AssertionMethod]
    protected override void TestAssert<T>(T ex)
    {
        base.TestAssert(ex);
    }
}

Expected behavior

When assertion method is inherited, it should be detected as an assertion method.

Actual behavior

When assertion methid is inherited, Sonar reports that the test has no assertion

Known workarounds

Override the parent method and set the assertion attribute on that one, and inside call the parent

Related information

  • C#/VB.NET Plugins version : SonarLint for VS2022 v.6.10.0.57359
  • Visual Studio version : 2022
  • MSBuild / dotnet version : MSBuild version 17.3.2+561848881 / .NET 6
  • SonarScanner for .NET version (if used) : SonarScanner for MSBuild 5.8 / SonarScanner 4.7.0.2747
  • Operating System: Ubuntu / Windows 10
@killergege killergege changed the title S2699 - AssertionMethod is ignored when assertion method is inherited S2699 - AssertionMethodAttribute is ignored when assertion method is inherited Dec 12, 2022
@martin-strecker-sonarsource martin-strecker-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Dec 13, 2022
@martin-strecker-sonarsource
Copy link
Contributor

Thank you for reporting this.
I was able to reproduce the behavior partially in #6528. It is indeed a false positive but as far as I can tell it is only raised if the annotated method is overridden. I was not able to reproduce your original example where the assertion method is not overridden. Maybe this is because ParentTest is defined in another assembly than ChildTest but I didn't test this scenario.
I labeled the issue as False Positive and it was added to our backlog.

@killergege
Copy link
Author

I may have simplified the case too much. Indeed, in my "real" case, the parent class is in another assembly.

@martin-strecker-sonarsource
Copy link
Contributor

Peach validation: No new issues found

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
3 participants