Skip to content

Fix S3433 FP: Allow generic type parameter in test method for MSTest #9785

@drieseng

Description

@drieseng

Description

Currently, S3433 is reported when a data-driven test method in MSTest has a generic type parameter.
This is effectively supported by MSTest, so S3433 should not be reported.

Reproducer

Compile the following class with MSTest and S3433 enabled:

using System;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public sealed class SonarBug
{
    [TestMethod]
    [DynamicData(nameof(GetNameOfT_TestData))]
    public void GetNameOfT<T>(T value, string expected)
        where T : struct, Enum
    {
        var actual = Enum.GetName(value);
        Assert.AreEqual(expected, actual);
    }

    private static IEnumerable<object?[]> GetNameOfT_TestData()
    {
        yield return [DateTimeKind.Local, "Local"];
        yield return [DateTimeKind.Utc, "Utc"];
    }
}

Expected result:

S3433 is not reported. When you run the test you'll get two test results.

Actual result:

S3433 is reported.

Product and Version

SonarAnalyzer.CSharp v10.18.0.131500

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