Skip to content

Code Analyzers don't recognize MatchSnapshot() as Assertion #228

@matthiaslischka

Description

@matthiaslischka

Is your feature request related to a problem? Please describe.
SonarQube Code Analyzer reported an issue that our test does not contain any assert:

Add at least one assertion to this test case.

The finding of assertion statements seems to be based on some loose rules around naming conventions and explicit checks for "known" frameworks. A recommended workaround for Sonar is to wrap and annotate the assertions:

public static class SnapshotExtension
{
    [AssertionMethod]
    public static void MatchSnapshot(
        this object? currentResult,
        Func<MatchOptions, MatchOptions>? matchOptions = null
    ) => Snapshooter.Xunit3.SnapshotExtension.MatchSnapshot(currentResult, matchOptions);
}

[AttributeUsage(AttributeTargets.Method)]
public class AssertionMethodAttribute : Attribute;

Describe the solution you'd like
I was hoping that this issue could be addressed directly in the library already instead of the consumers writing custom wrappers. However, I'm a bit clueless about those code analyzer rules myself and what's the proper way is to fix this in general - not only specific for SonarQube.

Maybe the problem would be solved if the method is called ShouldMatchSnapshot or this AssertionMethodAttribute also works for other analyzers. I'm not sure how big assertions frameworks address this but probably there are hardcoded rules for the biggest libraries.

I'm afraid this feature request would require some research, testing, and experimentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions