diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/PreferConstructorOverTestInitializeAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/PreferConstructorOverTestInitializeAnalyzerTests.cs index d6a1f2d64e..5d2ee0bc71 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/PreferConstructorOverTestInitializeAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/PreferConstructorOverTestInitializeAnalyzerTests.cs @@ -404,6 +404,84 @@ private void SomePrivateMethod() await VerifyCS.VerifyCodeFixAsync(code, fixedCode); } + [TestMethod] + public async Task WhenTestInitializeMethodInNonTestClass_Diagnostic() + { + // The analyzer fires regardless of whether the containing class has [TestClass], + // and the fixer replaces the method with a constructor carrying its body. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public class MyClass + { + [TestInitialize] + public void [|MyInit|]() + { + int x = 1; + } + } + """; + string fixedCode = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public class MyClass + { + public MyClass() + { + int x = 1; + } + } + """; + + await VerifyCS.VerifyCodeFixAsync(code, fixedCode); + } + + [TestMethod] + public async Task WhenTestClassHasOnlyParameterizedCtorAndTestInitialize_CodeFix_MergesIntoParameterizedCtor() + { + // The fixer merges the TestInitialize body into the first non-static constructor found, + // even when that constructor has parameters and there is no default constructor. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + private int _x; + private int _y; + + public MyTestClass(int y) + { + _y = y; + } + + [TestInitialize] + public void [|MyTestInit|]() + { + _x = 1; + } + } + """; + string fixedCode = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + private int _x; + private int _y; + + public MyTestClass(int y) + { + _y = y; + _x = 1; + } + } + """; + + await VerifyCS.VerifyCodeFixAsync(code, fixedCode); + } + [TestMethod] public async Task WhenTestClassHasTestInitializeWithMultiLineBody_PreservesIndentation() { diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/PreferDisposeOverTestCleanupAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/PreferDisposeOverTestCleanupAnalyzerTests.cs index 1627e30caf..02ebee0752 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/PreferDisposeOverTestCleanupAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/PreferDisposeOverTestCleanupAnalyzerTests.cs @@ -381,6 +381,39 @@ public class MyTestClass } #endif + [TestMethod] + public async Task WhenTestCleanupMethodInNonTestClass_Diagnostic() + { + // The analyzer fires regardless of whether the containing class has [TestClass], + // and the fixer adds IDisposable to the base list and replaces the method with Dispose(). + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public class MyClass + { + [TestCleanup] + public void [|MyCleanup|]() + { + int x = 1; + } + } + """; + string fixedCode = """ + using System; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public class MyClass : IDisposable + { + public void Dispose() + { + int x = 1; + } + } + """; + + await VerifyCS.VerifyCodeFixAsync(code, fixedCode); + } + [TestMethod] public async Task WhenTestClassHasTestCleanupWithMultiLineBody_PreservesIndentation() {