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
@github-actions github-actions bot added this to False Positive in Backlog 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.

@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Jan 23, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Jan 23, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Jan 23, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Jan 25, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S2699 - AssertionMethodAttribute is ignored when assertion method is inherited Fix S2699 FP: AssertionMethodAttribute is ignored when assertion method is inherited Feb 3, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Feb 20, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Mar 1, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Mar 2, 2023
@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban Mar 3, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource added this to the 8.55 milestone Mar 8, 2023
Best Kanban automation moved this from Review approved to Validate Peach Mar 14, 2023
@martin-strecker-sonarsource
Copy link
Contributor

Peach validation: No new issues found

@martin-strecker-sonarsource martin-strecker-sonarsource moved this from Validate Peach to Done in Best Kanban Mar 16, 2023
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
Best Kanban
  
Done
Backlog
  
False Positive
3 participants