@@ -23,7 +23,7 @@ public STATestClassTests(ITestExecutionContext testExecutionContext, TestAssetFi
23
23
}
24
24
25
25
[ ArgumentsProvider ( nameof ( TargetFrameworks . All ) , typeof ( TargetFrameworks ) ) ]
26
- public async Task STATestClass_OnWindows_LifeCycle ( string currentTfm )
26
+ public async Task STATestClass_OnWindows_OnLifeCycleTestClass_FixturesAndMethodsAreOnExpectedApartmentState ( string currentTfm )
27
27
{
28
28
if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
29
29
{
@@ -35,11 +35,45 @@ public async Task STATestClass_OnWindows_LifeCycle(string currentTfm)
35
35
TestHostResult testHostResult = await testHost . ExecuteAsync ( $ "--settings { runSettingsFilePath } --filter className=LifeCycleTestClass") ;
36
36
37
37
testHostResult . AssertExitCodeIs ( 0 ) ;
38
- testHostResult . AssertOutputContains ( "Passed!" ) ;
38
+ testHostResult . AssertOutputContains ( "Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1" ) ;
39
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.AssemblyInitialize" ) ;
40
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.ClassInitialize" ) ;
41
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.Constructor" ) ;
42
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.TestInitialize" ) ;
43
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.TestMethod1" ) ;
44
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.TestCleanup" ) ;
45
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.Dispose" ) ;
46
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.ClassCleanup" ) ;
47
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.AssemblyCleanup" ) ;
39
48
}
40
49
41
50
[ ArgumentsProvider ( nameof ( TargetFrameworks . All ) , typeof ( TargetFrameworks ) ) ]
42
- public async Task DerivedSTATestClass_OnWindows_ClassCleanupWithEndOfAssemblyBehavior_IsNotInsideTheSTAThread ( string currentTfm )
51
+ public async Task STATestClass_OnWindows_OnLifeCycleTestClassWithLastTestSkipped_FixturesAndMethodsAreOnExpectedApartmentState ( string currentTfm )
52
+ {
53
+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
54
+ {
55
+ return ;
56
+ }
57
+
58
+ var testHost = TestHost . LocateFrom ( _testAssetFixture . TargetAssetPath , AssetName , currentTfm ) ;
59
+ string runSettingsFilePath = Path . Combine ( testHost . DirectoryName , "mta.runsettings" ) ;
60
+ TestHostResult testHostResult = await testHost . ExecuteAsync ( $ "--settings { runSettingsFilePath } --filter className=LifeCycleTestClassWithLastTestSkipped") ;
61
+
62
+ testHostResult . AssertExitCodeIs ( 0 ) ;
63
+ testHostResult . AssertOutputContains ( "Passed! - Failed: 0, Passed: 1, Skipped: 1, Total: 2" ) ;
64
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.AssemblyInitialize" ) ;
65
+ testHostResult . AssertOutputContains ( "LifeCycleTestClassWithLastTestSkipped.ClassInitialize" ) ;
66
+ testHostResult . AssertOutputContains ( "LifeCycleTestClassWithLastTestSkipped.Constructor" ) ;
67
+ testHostResult . AssertOutputContains ( "LifeCycleTestClassWithLastTestSkipped.TestInitialize" ) ;
68
+ testHostResult . AssertOutputContains ( "LifeCycleTestClassWithLastTestSkipped.TestMethod1" ) ;
69
+ testHostResult . AssertOutputContains ( "LifeCycleTestClassWithLastTestSkipped.TestCleanup" ) ;
70
+ testHostResult . AssertOutputContains ( "LifeCycleTestClassWithLastTestSkipped.Dispose" ) ;
71
+ testHostResult . AssertOutputContains ( "LifeCycleTestClassWithLastTestSkipped.ClassCleanup" ) ;
72
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.AssemblyCleanup" ) ;
73
+ }
74
+
75
+ [ ArgumentsProvider ( nameof ( TargetFrameworks . All ) , typeof ( TargetFrameworks ) ) ]
76
+ public async Task DerivedSTATestClass_OnWindows_OnTestClassWithClassCleanupEndOfAssembly_ClassCleanupIsMTA ( string currentTfm )
43
77
{
44
78
if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
45
79
{
@@ -51,7 +85,16 @@ public async Task DerivedSTATestClass_OnWindows_ClassCleanupWithEndOfAssemblyBeh
51
85
TestHostResult testHostResult = await testHost . ExecuteAsync ( $ "--settings { runSettingsFilePath } --filter className=TestClassWithClassCleanupEndOfAssembly") ;
52
86
53
87
testHostResult . AssertExitCodeIs ( 0 ) ;
54
- testHostResult . AssertOutputContains ( "Passed!" ) ;
88
+ testHostResult . AssertOutputContains ( "Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1" ) ;
89
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.AssemblyInitialize" ) ;
90
+ testHostResult . AssertOutputContains ( "TestClassWithClassCleanupEndOfAssembly.ClassInitialize" ) ;
91
+ testHostResult . AssertOutputContains ( "TestClassWithClassCleanupEndOfAssembly.Constructor" ) ;
92
+ testHostResult . AssertOutputContains ( "TestClassWithClassCleanupEndOfAssembly.TestInitialize" ) ;
93
+ testHostResult . AssertOutputContains ( "TestClassWithClassCleanupEndOfAssembly.TestMethod1" ) ;
94
+ testHostResult . AssertOutputContains ( "TestClassWithClassCleanupEndOfAssembly.TestCleanup" ) ;
95
+ testHostResult . AssertOutputContains ( "TestClassWithClassCleanupEndOfAssembly.Dispose" ) ;
96
+ testHostResult . AssertOutputContains ( "TestClassWithClassCleanupEndOfAssembly.ClassCleanup" ) ;
97
+ testHostResult . AssertOutputContains ( "LifeCycleTestClass.AssemblyCleanup" ) ;
55
98
}
56
99
57
100
[ TestFixture ( TestFixtureSharingStrategy . PerTestGroup ) ]
@@ -75,6 +118,9 @@ public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : Test
75
118
<RunConfiguration>
76
119
<ExecutionThreadApartmentState>MTA</ExecutionThreadApartmentState>
77
120
</RunConfiguration>
121
+ <MSTest>
122
+ <CaptureTraceOutput>false</CaptureTraceOutput>
123
+ </MSTest>
78
124
</RunSettings>
79
125
80
126
#file STATestClass.csproj
@@ -112,53 +158,120 @@ public class LifeCycleTestClass : IDisposable
112
158
[AssemblyInitialize]
113
159
public static void AssemblyInitialize(TestContext context)
114
160
{
115
- Helper.AssertCorrectThreadApartmentMTAState();
161
+ Console.WriteLine("LifeCycleTestClass.AssemblyInitialize");
162
+ ThreadAssert.AssertApartmentStateIsMTA();
116
163
}
117
164
118
165
[AssemblyCleanup]
119
166
public static void AssemblyCleanup()
120
167
{
121
- Helper.AssertCorrectThreadApartmentMTAState();
168
+ Console.WriteLine("LifeCycleTestClass.AssemblyCleanup");
169
+ ThreadAssert.AssertApartmentStateIsMTA();
122
170
}
123
171
124
172
public LifeCycleTestClass()
125
173
{
126
- Helper.AssertCorrectThreadApartmentSTAState();
174
+ Console.WriteLine("LifeCycleTestClass.Constructor");
175
+ ThreadAssert.AssertApartmentStateIsSTA();
176
+ }
177
+
178
+ [ClassInitialize]
179
+ public static void ClassInitialize(TestContext context)
180
+ {
181
+ Console.WriteLine("LifeCycleTestClass.ClassInitialize");
182
+ ThreadAssert.AssertApartmentStateIsSTA();
183
+ }
184
+
185
+ [ClassCleanup(ClassCleanupBehavior.EndOfClass)]
186
+ public static void ClassCleanup()
187
+ {
188
+ Console.WriteLine("LifeCycleTestClass.ClassCleanup");
189
+ ThreadAssert.AssertApartmentStateIsSTA();
190
+ }
191
+
192
+ [TestInitialize]
193
+ public void TestInitialize()
194
+ {
195
+ Console.WriteLine("LifeCycleTestClass.TestInitialize");
196
+ ThreadAssert.AssertApartmentStateIsSTA();
197
+ }
198
+
199
+ [TestCleanup]
200
+ public void TestCleanup()
201
+ {
202
+ Console.WriteLine("LifeCycleTestClass.TestCleanup");
203
+ ThreadAssert.AssertApartmentStateIsSTA();
204
+ }
205
+
206
+ [TestMethod]
207
+ public void TestMethod1()
208
+ {
209
+ Console.WriteLine("LifeCycleTestClass.TestMethod1");
210
+ ThreadAssert.AssertApartmentStateIsSTA();
211
+ }
212
+
213
+ public void Dispose()
214
+ {
215
+ Console.WriteLine("LifeCycleTestClass.Dispose");
216
+ ThreadAssert.AssertApartmentStateIsSTA();
217
+ }
218
+ }
219
+
220
+ [STATestClass]
221
+ public class LifeCycleTestClassWithLastTestSkipped : IDisposable
222
+ {
223
+ public LifeCycleTestClassWithLastTestSkipped()
224
+ {
225
+ Console.WriteLine("LifeCycleTestClassWithLastTestSkipped.Constructor");
226
+ ThreadAssert.AssertApartmentStateIsSTA();
127
227
}
128
228
129
229
[ClassInitialize]
130
230
public static void ClassInitialize(TestContext context)
131
231
{
132
- Helper.AssertCorrectThreadApartmentSTAState();
232
+ Console.WriteLine("LifeCycleTestClassWithLastTestSkipped.ClassInitialize");
233
+ ThreadAssert.AssertApartmentStateIsSTA();
133
234
}
134
235
135
236
[ClassCleanup(ClassCleanupBehavior.EndOfClass)]
136
237
public static void ClassCleanup()
137
238
{
138
- Helper.AssertCorrectThreadApartmentSTAState();
239
+ Console.WriteLine("LifeCycleTestClassWithLastTestSkipped.ClassCleanup");
240
+ ThreadAssert.AssertApartmentStateIsSTA();
139
241
}
140
242
141
243
[TestInitialize]
142
244
public void TestInitialize()
143
245
{
144
- Helper.AssertCorrectThreadApartmentSTAState();
246
+ Console.WriteLine("LifeCycleTestClassWithLastTestSkipped.TestInitialize");
247
+ ThreadAssert.AssertApartmentStateIsSTA();
145
248
}
146
249
147
250
[TestCleanup]
148
251
public void TestCleanup()
149
252
{
150
- Helper.AssertCorrectThreadApartmentSTAState();
253
+ Console.WriteLine("LifeCycleTestClassWithLastTestSkipped.TestCleanup");
254
+ ThreadAssert.AssertApartmentStateIsSTA();
151
255
}
152
256
153
257
[TestMethod]
154
258
public void TestMethod1()
155
259
{
156
- Helper.AssertCorrectThreadApartmentSTAState();
260
+ Console.WriteLine("LifeCycleTestClassWithLastTestSkipped.TestMethod1");
261
+ ThreadAssert.AssertApartmentStateIsSTA();
262
+ }
263
+
264
+ [TestMethod]
265
+ [Ignore]
266
+ public void TestMethod2()
267
+ {
268
+ Assert.Fail("TestMethod2 should not be executed");
157
269
}
158
270
159
271
public void Dispose()
160
272
{
161
- Helper.AssertCorrectThreadApartmentSTAState();
273
+ Console.WriteLine("LifeCycleTestClassWithLastTestSkipped.Dispose");
274
+ ThreadAssert.AssertApartmentStateIsSTA();
162
275
}
163
276
}
164
277
@@ -171,54 +284,61 @@ public class TestClassWithClassCleanupEndOfAssembly : IDisposable
171
284
{
172
285
public TestClassWithClassCleanupEndOfAssembly()
173
286
{
174
- Helper.AssertCorrectThreadApartmentSTAState();
287
+ Console.WriteLine("TestClassWithClassCleanupEndOfAssembly.Constructor");
288
+ ThreadAssert.AssertApartmentStateIsSTA();
175
289
}
176
290
177
291
[ClassInitialize]
178
292
public static void ClassInitialize(TestContext context)
179
293
{
180
- Helper.AssertCorrectThreadApartmentSTAState();
294
+ Console.WriteLine("TestClassWithClassCleanupEndOfAssembly.ClassInitialize");
295
+ ThreadAssert.AssertApartmentStateIsSTA();
181
296
}
182
297
183
298
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
184
299
public static void ClassCleanup()
185
300
{
186
- Helper.AssertCorrectThreadApartmentMTAState();
301
+ Console.WriteLine("TestClassWithClassCleanupEndOfAssembly.ClassCleanup");
302
+ ThreadAssert.AssertApartmentStateIsMTA();
187
303
}
188
304
189
305
[TestInitialize]
190
306
public void TestInitialize()
191
307
{
192
- Helper.AssertCorrectThreadApartmentSTAState();
308
+ Console.WriteLine("TestClassWithClassCleanupEndOfAssembly.TestInitialize");
309
+ ThreadAssert.AssertApartmentStateIsSTA();
193
310
}
194
311
195
312
[TestCleanup]
196
313
public void TestCleanup()
197
314
{
198
- Helper.AssertCorrectThreadApartmentSTAState();
315
+ Console.WriteLine("TestClassWithClassCleanupEndOfAssembly.TestCleanup");
316
+ ThreadAssert.AssertApartmentStateIsSTA();
199
317
}
200
318
201
319
[TestMethod]
202
320
public void TestMethod1()
203
321
{
204
- Helper.AssertCorrectThreadApartmentSTAState();
322
+ Console.WriteLine("TestClassWithClassCleanupEndOfAssembly.TestMethod1");
323
+ ThreadAssert.AssertApartmentStateIsSTA();
205
324
}
206
325
207
326
public void Dispose()
208
327
{
209
- Helper.AssertCorrectThreadApartmentSTAState();
328
+ Console.WriteLine("TestClassWithClassCleanupEndOfAssembly.Dispose");
329
+ ThreadAssert.AssertApartmentStateIsSTA();
210
330
}
211
331
}
212
332
213
- public static class Helper
333
+ public static class ThreadAssert
214
334
{
215
- public static void AssertCorrectThreadApartmentMTAState ()
335
+ public static void AssertApartmentStateIsMTA ()
216
336
{
217
337
var apartmentState = Thread.CurrentThread.GetApartmentState();
218
338
Assert.AreEqual(ApartmentState.MTA, apartmentState);
219
339
}
220
340
221
- public static void AssertCorrectThreadApartmentSTAState ()
341
+ public static void AssertApartmentStateIsSTA ()
222
342
{
223
343
var apartmentState = Thread.CurrentThread.GetApartmentState();
224
344
Assert.AreEqual(ApartmentState.STA, apartmentState);
0 commit comments