You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
The text was updated successfully, but these errors were encountered:
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
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.
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
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:
This doesn't report S2699 :
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
The text was updated successfully, but these errors were encountered: