@@ -131,4 +131,139 @@ public static void ClassCleanup()
131131
132132 await VerifyCS . VerifyAnalyzerAsync ( code ) ;
133133 }
134+
135+ [ TestMethod ]
136+ public async Task WhenAssigningTestContextParameterToInstanceMember_NoDiagnostic ( )
137+ {
138+ // Assigning a TestContext *parameter* to an instance field or property should not trigger
139+ // the diagnostic. This exercises the analyzer's 'Instance: null' guard while the value still
140+ // satisfies the 'Value: IParameterReferenceOperation' check (only the target-side guard fails).
141+ string code = """
142+ using Microsoft.VisualStudio.TestTools.UnitTesting;
143+
144+ [TestClass]
145+ public class MyTestClass
146+ {
147+ private TestContext _testContext;
148+ public TestContext TestContextProperty { get; set; }
149+
150+ public void Store(TestContext tc)
151+ {
152+ _testContext = tc;
153+ TestContextProperty = tc;
154+ }
155+ }
156+ """ ;
157+
158+ await VerifyCS . VerifyAnalyzerAsync ( code ) ;
159+ }
160+
161+ [ TestMethod ]
162+ public async Task WhenAssigningTestContextParameterToLocalVariable_NoDiagnostic ( )
163+ {
164+ // Assigning a TestContext parameter to a local variable is fine — the assignment target is
165+ // not an IMemberReferenceOperation, so the analyzer's pattern doesn't match. An explicit
166+ // assignment (not a declaration initializer) is used so OperationKind.SimpleAssignment fires.
167+ string code = """
168+ using Microsoft.VisualStudio.TestTools.UnitTesting;
169+
170+ [TestClass]
171+ public class MyTestClass
172+ {
173+ [AssemblyInitialize]
174+ public static void AssemblyInit(TestContext tc)
175+ {
176+ TestContext local;
177+ local = tc;
178+ local.WriteLine("");
179+ }
180+ }
181+ """ ;
182+
183+ await VerifyCS . VerifyAnalyzerAsync ( code ) ;
184+ }
185+
186+ [ TestMethod ]
187+ public async Task WhenAssigningNonTestContextParameterToStaticField_NoDiagnostic ( )
188+ {
189+ // Covers the IParameterReferenceOperation *type-mismatch* path: the value is a parameter
190+ // reference, but its type is not TestContext, so the analyzer must not fire.
191+ string code = """
192+ using Microsoft.VisualStudio.TestTools.UnitTesting;
193+
194+ [TestClass]
195+ public class MyTestClass
196+ {
197+ private static string s_name;
198+
199+ [AssemblyInitialize]
200+ public static void AssemblyInit(TestContext tc)
201+ {
202+ Store("value");
203+ }
204+
205+ private static void Store(string name)
206+ {
207+ s_name = name;
208+ }
209+ }
210+ """ ;
211+
212+ await VerifyCS . VerifyAnalyzerAsync ( code ) ;
213+ }
214+
215+ [ TestMethod ]
216+ public async Task WhenAssigningNonParameterValueToStaticField_NoDiagnostic ( )
217+ {
218+ // Covers the 'Value is not IParameterReferenceOperation' path: a literal (or any non-parameter
219+ // expression) assigned to a static field must not be flagged, even when the field type is TestContext.
220+ string code = """
221+ using Microsoft.VisualStudio.TestTools.UnitTesting;
222+
223+ [TestClass]
224+ public class MyTestClass
225+ {
226+ private static string s_name;
227+
228+ [AssemblyInitialize]
229+ public static void AssemblyInit(TestContext tc)
230+ {
231+ s_name = "value";
232+ }
233+ }
234+ """ ;
235+
236+ await VerifyCS . VerifyAnalyzerAsync ( code ) ;
237+ }
238+
239+ #if NET
240+ [ TestMethod ]
241+ public async Task WhenAssigningTestContextInHelperMethod_Diagnostic ( )
242+ {
243+ // The diagnostic fires on any static member assignment of a TestContext parameter,
244+ // regardless of whether the containing method is [AssemblyInitialize] or [ClassInitialize].
245+ string code = """
246+ using Microsoft.VisualStudio.TestTools.UnitTesting;
247+
248+ [TestClass]
249+ public class MyTestClass
250+ {
251+ private static TestContext s_testContext;
252+
253+ [AssemblyInitialize]
254+ public static void AssemblyInit(TestContext tc)
255+ {
256+ Store(tc);
257+ }
258+
259+ private static void Store(TestContext tc)
260+ {
261+ [|s_testContext = tc|];
262+ }
263+ }
264+ """ ;
265+
266+ await VerifyCS . VerifyAnalyzerAsync ( code ) ;
267+ }
268+ #endif
134269}
0 commit comments