Skip to content

Commit c9ee4fa

Browse files
EvangelinkCopilot
andauthored
test: add edge case tests for DoNotUseSystemDescriptionAttributeAnalyzer (MSTEST0031)
Add two new test cases: - [DataTestMethod] + [System.ComponentModel.Description] → diagnostic (confirms Inherits() catches attributes derived from TestMethodAttribute) - [TestMethod] + MSTest's [Description] (no System.ComponentModel using) → no diagnostic (resolves to the correct MSTest attribute) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 741048f commit c9ee4fa

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

test/UnitTests/MSTest.Analyzers.UnitTests/DoNotUseSystemDescriptionAttributeAnalyzerTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,65 @@ public void Method()
165165

166166
await VerifyCS.VerifyAnalyzerAsync(code);
167167
}
168+
169+
[TestMethod]
170+
public async Task WhenDataTestMethodHasSystemDescriptionAttribute_Diagnostic()
171+
{
172+
// DataTestMethodAttribute inherits from TestMethodAttribute, so the Inherits() check should
173+
// catch it and report the same diagnostic as for [TestMethod].
174+
string code = """
175+
using Microsoft.VisualStudio.TestTools.UnitTesting;
176+
177+
[TestClass]
178+
public class MyTestClass
179+
{
180+
[DataTestMethod]
181+
[DataRow(1)]
182+
[System.ComponentModel.Description("Description")]
183+
public void [|MyTestMethod|](int x)
184+
{
185+
}
186+
}
187+
""";
188+
189+
string fixedCode = """
190+
using Microsoft.VisualStudio.TestTools.UnitTesting;
191+
192+
[TestClass]
193+
public class MyTestClass
194+
{
195+
[DataTestMethod]
196+
[DataRow(1)]
197+
[Description("Description")]
198+
public void MyTestMethod(int x)
199+
{
200+
}
201+
}
202+
""";
203+
204+
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
205+
}
206+
207+
[TestMethod]
208+
public async Task WhenTestMethodHasMSTestDescriptionAttribute_NoDiagnostic()
209+
{
210+
// When only the MSTest namespace is in scope, the short-form [Description] resolves to
211+
// Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute, not
212+
// System.ComponentModel.DescriptionAttribute. The analyzer must not fire.
213+
string code = """
214+
using Microsoft.VisualStudio.TestTools.UnitTesting;
215+
216+
[TestClass]
217+
public class MyTestClass
218+
{
219+
[TestMethod]
220+
[Description("Description")]
221+
public void MyTestMethod()
222+
{
223+
}
224+
}
225+
""";
226+
227+
await VerifyCS.VerifyAnalyzerAsync(code);
228+
}
168229
}

0 commit comments

Comments
 (0)