Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 7, 2025

Methods decorated with multiple TestMethodAttribute-derived attributes compile but only the first attribute returned by reflection will be used, which can be confusing. For example:

public class MyTestMethod : TestMethodAttribute { }

[TestClass]
public class MyTestClass
{
    [TestMethod]
    [MyTestMethod]  // Warning: multiple test method attributes
    public void M() { }
}

Changes

Analyzer Implementation

  • Created DuplicateTestMethodAttributeAnalyzer that detects methods with >1 TestMethodAttribute-derived attribute
  • Reports warning-level diagnostic (MSTEST0059) on the method
  • Memory-optimized using counter instead of list allocation

Resources & Localization

  • Added diagnostic ID, title, message, and description to Resources.resx
  • Updated all 13 .xlf localization files with new entries
  • Updated AnalyzerReleases.Unshipped.md
  • Messages clarify that only the first attribute returned by reflection will be used when multiple attributes are present

Tests

  • Coverage for single/multiple attributes, derived types, DataTestMethod combinations, edge cases
Original prompt

This section details on the original issue you should resolve

<issue_title>Add analyzer for duplicate test method attribute</issue_title>
<issue_description>The following code will compile successfully:

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

[TestClass]
public class MyTestClass
{
    [TestMethod]
    [MyTestMethod]
    public void M() { }
}

We should produce a warning so that user picks one of the two attributes.</issue_description>

<agent_instructions>Create a new analyzer that will detect that a method is marked with a single attribute that is or inherits from [TestMethod]</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add analyzer for duplicate test method attribute Add analyzer for duplicate test method attributes (MSTEST0059) Dec 7, 2025
Copilot AI requested a review from Evangelink December 7, 2025 14:46
Copilot finished work on behalf of Evangelink December 7, 2025 14:46
<value>Test method '{0}' has multiple attributes that inherit from 'TestMethodAttribute'. Only one test method attribute is allowed.</value>
</data>
<data name="DuplicateTestMethodAttributeDescription" xml:space="preserve">
<value>A test method should be marked with exactly one attribute that is or inherits from 'TestMethodAttribute'. Having multiple such attributes can lead to unexpected behavior.</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not so much unexpected behavior. The problem is that only one attribute will be called (the first returned by reflection).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please update the messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the messages to clarify that only the first attribute returned by reflection will be used, rather than saying "unexpected behavior". Changes committed in b4de188.

@Evangelink Evangelink marked this pull request as ready for review December 7, 2025 15:10
@Evangelink Evangelink enabled auto-merge (squash) December 7, 2025 15:10
Changed description from "unexpected behavior" to explicitly state that only the first attribute returned by reflection will be used when multiple TestMethodAttribute-derived attributes are present.

Co-authored-by: Evangelink <[email protected]>
Copilot AI requested a review from Evangelink December 7, 2025 15:14
Copilot finished work on behalf of Evangelink December 7, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add analyzer for duplicate test method attribute

2 participants