testfx icon indicating copy to clipboard operation
testfx copied to clipboard

MSTEST0057 false positive when attribute has static field with initializer

Open gao-artur opened this issue 1 month ago • 2 comments

MSTEST0057 reports a false positive when the attribute has a static field or property with an initializer. Not happens for const

public class DerivedTestMethodAttribute : TestMethodAttribute
{
    private static string Text { get; } = "I'm static property";
    private static readonly string _text = "I'm static field";

    public DerivedTestMethodAttribute([CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
        : base(callerFilePath, callerLineNumber)
    {
    }
}

MSTEST0057: TestMethodAttribute derived class 'AftTestMethodAttribute' should add CallerFilePath and CallerLineNumber parameters to its constructor

BTW, the documentation is missing for this diagnostic.

gao-artur avatar Dec 07 '25 11:12 gao-artur

I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  • https://github.com/microsoft/testfx/issues/6935

MihuBot avatar Dec 07 '25 11:12 MihuBot

Yes, that's probably a duplicate. When a class has a static field with an initializer, the compiler generates a static constructor.

I'd add these test cases to the analyzer anyway.

gao-artur avatar Dec 07 '25 12:12 gao-artur

Hello,

This has been fixed and will be released in MSTest 4.1 https://github.com/microsoft/testfx/pull/6937

mariam-abdulla avatar Dec 16 '25 09:12 mariam-abdulla