-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathTestContainerDiscovery_Tests.cs
470 lines (403 loc) · 22.1 KB
/
TestContainerDiscovery_Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AutoMoq;
using FineCodeCoverage.Core.Initialization;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Engine.MsTestPlatform.CodeCoverage;
using FineCodeCoverage.Impl;
using FineCodeCoverage.Impl.TestContainerDiscovery;
using FineCodeCoverage.Options;
using Microsoft.VisualStudio.TestWindow.Extensibility;
using Moq;
using NUnit.Framework;
namespace Test
{
internal class TestOperationStateInvocationManager_Tests
{
private AutoMoqer mocker;
private TestOperationStateInvocationManager testOperationStateInvocationManager;
[SetUp]
public void SetUp()
{
mocker = new AutoMoqer();
testOperationStateInvocationManager = mocker.Create<TestOperationStateInvocationManager>();
}
[Test]
public void Should_Return_True_When_Initialized_And_TestExecutionStarting()
{
mocker.GetMock<IInitializeStatusProvider>().Setup(initializeStatusProvider => initializeStatusProvider.InitializeStatus).Returns(InitializeStatus.Initialized);
Assert.That(testOperationStateInvocationManager.CanInvoke(TestOperationStates.TestExecutionStarting), Is.True);
}
[Test]
public void Should_Return_False_When_Not_Initialized_And_TestExecutionStarting()
{
mocker.GetMock<IInitializeStatusProvider>().Setup(initializeStatusProvider => initializeStatusProvider.InitializeStatus).Returns(InitializeStatus.Initializing);
Assert.That(testOperationStateInvocationManager.CanInvoke(TestOperationStates.TestExecutionStarting), Is.False);
}
[TestCase(true)]
[TestCase(false)]
public void Should_Return_True_For_All_Other_States_If_Was_Initialized_When_TestExecutionStarting(bool initializedWhenStarting)
{
var startingInitializeStatus = initializedWhenStarting ? InitializeStatus.Initialized : InitializeStatus.Initializing;
mocker.GetMock<IInitializeStatusProvider>().Setup(initializeStatusProvider => initializeStatusProvider.InitializeStatus).Returns(startingInitializeStatus);
testOperationStateInvocationManager.CanInvoke(TestOperationStates.TestExecutionStarting);
Assert.That(testOperationStateInvocationManager.CanInvoke(TestOperationStates.TestExecutionCancelAndFinished), Is.EqualTo(initializedWhenStarting));
}
[TestCase(TestOperationStates.TestExecutionStarting)]
[TestCase(TestOperationStates.TestExecutionFinished)]
public void Should_Log_When_Cannot_Invoke(TestOperationStates testOperationState)
{
testOperationStateInvocationManager.CanInvoke(testOperationState);
mocker.Verify<FineCodeCoverage.Output.ILogger>(logger => logger.Log($"Skipping {testOperationState} as FCC not initialized"));
}
}
internal class TestContainerDiscovery_Tests
{
private AutoMoqer mocker;
private TestContainerDiscoverer testContainerDiscoverer;
private void RaiseOperationStateChanged(TestOperationStates testOperationStates,IOperation operation = null)
{
var args = operation == null ? new OperationStateChangedEventArgs(testOperationStates) : new OperationStateChangedEventArgs(operation, (RequestStates)testOperationStates);
mocker.GetMock<IOperationState>().Raise(s => s.StateChanged += null, args);
}
private void RaiseTestExecutionStarting(IOperation operation = null)
{
RaiseOperationStateChanged(TestOperationStates.TestExecutionStarting,operation);
}
private void RaiseTestExecutionFinished(IOperation operation = null)
{
RaiseOperationStateChanged(TestOperationStates.TestExecutionFinished,operation);
}
private void RaiseTestExecutionCancelling()
{
RaiseOperationStateChanged(TestOperationStates.TestExecutionCanceling);
}
private void AssertShouldNotReloadCoverage()
{
mocker.Verify<IFCCEngine>(engine => engine.ReloadCoverage(It.IsAny<Func<Task<List<ICoverageProject>>>>()), Times.Never());
}
private void AssertReloadsCoverage()
{
mocker.Verify<IFCCEngine>(engine => engine.ReloadCoverage(It.IsAny<Func<Task<List<ICoverageProject>>>>()), Times.Once());
}
private void SetUpOptions(Action<Mock<IAppOptions>> setupAppOptions)
{
var mockAppOptions = new Mock<IAppOptions>();
setupAppOptions(mockAppOptions);
mocker.GetMock<IAppOptionsProvider>().Setup(appOptionsProvider => appOptionsProvider.Get()).Returns(mockAppOptions.Object);
}
private (IOperation operation, List<ICoverageProject> coverageProjects, Mock<ITestOperation> mockTestOperation) SetUpForProceedPath()
{
var operation = new Mock<IOperation>().Object;
var mockTestOperation = new Mock<ITestOperation>();
var coverageProjects = new List<ICoverageProject>();
mockTestOperation.Setup(t => t.GetCoverageProjectsAsync()).Returns(Task.FromResult(coverageProjects));
mocker.GetMock<ITestOperationFactory>().Setup(f => f.Create(operation)).Returns(mockTestOperation.Object);
return (operation, coverageProjects, mockTestOperation);
}
[SetUp]
public void SetUp()
{
mocker = new AutoMoqer();
var mockCoverageCollectableFromTestExplorer = mocker.GetMock<ICoverageCollectableFromTestExplorer>();
mockCoverageCollectableFromTestExplorer.Setup(coverageCollectableFromTestExplorer => coverageCollectableFromTestExplorer.IsCollectableAsync()).ReturnsAsync(true);
testContainerDiscoverer = mocker.Create<TestContainerDiscoverer>();
testContainerDiscoverer.RunAsync = (taskProvider) =>
{
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
taskProvider().Wait();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
};
var mockTestOperationStateInvocationManager = mocker.GetMock<ITestOperationStateInvocationManager>();
mockTestOperationStateInvocationManager.Setup(testOperationStateInvocationManager => testOperationStateInvocationManager.CanInvoke(It.IsAny<TestOperationStates>())).Returns(true);
}
[Test]
public void It_Should_Load_The_Package()
{
mocker.Verify<IPackageLoader>(packageLoader => packageLoader.LoadPackageAsync(It.IsAny<CancellationToken>()));
}
[Test]
public void Should_Stop_Coverage_When_Tests_Are_Cancelled()
{
RaiseTestExecutionCancelling();
mocker.Verify<IFCCEngine>(e => e.StopCoverage());
}
[Test]
public void Should_StopCoverage_When_TestExecutionStarting()
{
RaiseTestExecutionStarting();
mocker.Verify<IFCCEngine>(engine => engine.StopCoverage());
}
[Test]
public void Should_Stop_Ms_CodeCoverage_When_TestExecutionStarting_And_Ms_Code_Coverage_Collecting()
{
var mockMsCodeCoverageRunSettingsService = SetMsCodeCoverageCollecting();
mockMsCodeCoverageRunSettingsService.Verify(
msCodeCoverageRunSettingsService => msCodeCoverageRunSettingsService.StopCoverage(),
Times.Never
);
RaiseTestExecutionStarting();
mockMsCodeCoverageRunSettingsService.Verify(
msCodeCoverageRunSettingsService => msCodeCoverageRunSettingsService.StopCoverage()
);
}
[TestCase(MsCodeCoverageCollectionStatus.Collecting,true)]
[TestCase(MsCodeCoverageCollectionStatus.NotCollecting,true)]
[TestCase(MsCodeCoverageCollectionStatus.Error,true)]
[TestCase(MsCodeCoverageCollectionStatus.Collecting, false)]
[TestCase(MsCodeCoverageCollectionStatus.NotCollecting, false)]
[TestCase(MsCodeCoverageCollectionStatus.Error, false)]
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
public void Should_Notify_MsCodeCoverage_When_Test_Execution_Not_Finished_IfCollectingAsync(MsCodeCoverageCollectionStatus status, bool cancelling)
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
{
var mockMsCodeCoverageRunSettingsService = SetMsCodeCoverageCollecting(status);
var operation = new Mock<IOperation>().Object;
var mockTestOperationFactory = mocker.GetMock<ITestOperationFactory>();
var testOperation = new Mock<ITestOperation>().Object;
mockTestOperationFactory.Setup(testOperationFactory => testOperationFactory.Create(operation)).Returns(testOperation);
RaiseOperationStateChanged(
cancelling ? TestOperationStates.TestExecutionCanceling : TestOperationStates.TestExecutionCancelAndFinished,
operation
);
var times = status == MsCodeCoverageCollectionStatus.Collecting ? Times.Once() : Times.Never();
mockMsCodeCoverageRunSettingsService.Verify(
msCodeCoverageRunSettingsService => msCodeCoverageRunSettingsService.TestExecutionNotFinishedAsync(testOperation), times
);
}
private Mock<IMsCodeCoverageRunSettingsService> SetMsCodeCoverageCollecting(MsCodeCoverageCollectionStatus status = MsCodeCoverageCollectionStatus.Collecting)
{
var mockMsCodeCoverageRunSettingsService = mocker.GetMock<IMsCodeCoverageRunSettingsService>();
mockMsCodeCoverageRunSettingsService.Setup(
msCodeCoverageRunSettingsService =>
msCodeCoverageRunSettingsService.IsCollectingAsync(It.IsAny<ITestOperation>())
).ReturnsAsync(status);
SetUpOptions(mockOptions => mockOptions.Setup(options => options.Enabled).Returns(true));
RaiseTestExecutionStarting();
return mockMsCodeCoverageRunSettingsService;
}
[Test]
public void Should_Not_ReloadCoverage_When_TestExecutionStarting_And_Settings_RunInParallel_Is_False()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(true);
mockAppOptions.Setup(o => o.RunInParallel).Returns(false);
});
RaiseTestExecutionStarting();
AssertShouldNotReloadCoverage();
}
[Test]
public void Should_Not_ReloadCoverage_When_TestExecutionFinished_And_Reloading_When_Tests_Start()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(true);
mockAppOptions.Setup(o => o.RunInParallel).Returns(true);
});
RaiseTestExecutionFinished();
AssertShouldNotReloadCoverage();
}
[Test]
public void Should_ReloadCoverage_When_TestExecutionFinished_If_RunInParallel_And_Not_Collecting_With_MsCodeCoverage()
{
var (operation,_,__) = SetUpForProceedPath();
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(true);
mockAppOptions.Setup(o => o.RunInParallel).Returns(true);
mockAppOptions.Setup(o => o.RunMsCodeCoverage).Returns(RunMsCodeCoverage.Yes);
});
RaiseTestExecutionFinished(operation);
mocker.Verify<IFCCEngine>(engine => engine.ReloadCoverage(It.IsAny<Func<Task<List<ICoverageProject>>>>()));
}
[Test]
public void Should_Collect_Ms_Code_Coverage_When_TestExecutionFinished_And_Ms_Code_Coverage_Collecting()
{
SetMsCodeCoverageCollecting();
var operation = new Mock<IOperation>().Object;
var testOperation = new Mock<ITestOperation>().Object;
var mockTestOperationFactory = mocker.GetMock<ITestOperationFactory>();
mockTestOperationFactory.Setup(testOperationFactory => testOperationFactory.Create(operation)).Returns(testOperation);
RaiseTestExecutionFinished(operation);
#pragma warning disable VSTHRD110 // Observe result of async calls
mocker.Verify<IMsCodeCoverageRunSettingsService>(
msCodeCoverageRunSettingsService =>
msCodeCoverageRunSettingsService.CollectAsync(operation,testOperation)
);
#pragma warning restore VSTHRD110 // Observe result of async calls
}
[Test]
public async Task Should_ReloadCoverage_When_TestExecutionStarting_And_Settings_RunInParallel_Is_True_Async()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(true);
mockAppOptions.Setup(o => o.RunInParallel).Returns(true);
});
var (operation, coverageProjects, mockTestOperation) = SetUpForProceedPath();
Task<List<ICoverageProject>> reloadCoverageTask = null;
mocker.GetMock<IFCCEngine>().Setup(engine => engine.ReloadCoverage(It.IsAny<Func<Task<List<ICoverageProject>>>>())).
Callback<Func<Task<List<ICoverageProject>>>>(callback =>
{
reloadCoverageTask = callback();
});
RaiseTestExecutionStarting(operation);
Assert.AreSame(coverageProjects, await reloadCoverageTask);
}
[Test]
public void Should_Not_ReloadCoverage_When_TestExecutionStarting_And_Settings_RunInParallel_Is_True_When_Enabled_Is_False_And_DisabledNoCoverage_True()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(false);
mockAppOptions.Setup(o => o.RunInParallel).Returns(true);
mockAppOptions.Setup(o => o.DisabledNoCoverage).Returns(true);
});
RaiseTestExecutionStarting();
AssertShouldNotReloadCoverage();
}
[Test]
public void Should_ReloadCoverage_When_TestExecutionStarting_And_Settings_RunInParallel_Is_True_When_Enabled_Is_False_And_DisabledNoCoverage_False()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(false);
mockAppOptions.Setup(o => o.RunInParallel).Returns(true);
mockAppOptions.Setup(o => o.DisabledNoCoverage).Returns(false);
});
RaiseTestExecutionStarting();
AssertReloadsCoverage();
}
[TestCase(true, 10, 1, 0, true, Description = "Should run when tests fail if settings RunWhenTestsFail is true")]
[TestCase(false, 10, 1, 0, false, Description = "Should not run when tests fail if settings RunWhenTestsFail is false")]
[TestCase(false, 0, 1, 1, false, Description = "Should not run when total tests does not exceed the RunWhenTestsExceed setting")]
[TestCase(false, 0, 1, 0, true, Description = "Should run when total tests does not exceed the RunWhenTestsExceed setting")]
public async Task Conditional_Run_Coverage_When_TestExecutionFinished_Async(bool runWhenTestsFail, long numberFailedTests, long totalTests, int runWhenTestsExceed, bool expectReloadedCoverage)
{
var (operation, coverageProjects, mockTestOperation) = SetUpForProceedPath();
mockTestOperation.Setup(o => o.FailedTests).Returns(numberFailedTests);
mockTestOperation.Setup(o => o.TotalTests).Returns(totalTests);
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(true);
mockAppOptions.Setup(o => o.RunInParallel).Returns(false);
mockAppOptions.Setup(o => o.RunWhenTestsFail).Returns(runWhenTestsFail);
mockAppOptions.Setup(o => o.RunWhenTestsExceed).Returns(runWhenTestsExceed);
});
Task<List<ICoverageProject>> reloadCoverageTask = null;
mocker.GetMock<IFCCEngine>().Setup(engine => engine.ReloadCoverage(It.IsAny<Func<Task<List<ICoverageProject>>>>())).
Callback<Func<Task<List<ICoverageProject>>>>(callback =>
{
reloadCoverageTask = callback();
});
RaiseTestExecutionFinished(operation);
if (expectReloadedCoverage)
{
Assert.AreSame(coverageProjects, await reloadCoverageTask);
}
else
{
AssertShouldNotReloadCoverage();
}
}
[Test]
public void Should_Not_Run_Coverage_When_TestExecutionFinished_If_Enabled_Is_False()
{
var operation = new Mock<IOperation>().Object;
var mockTestOperation = new Mock<ITestOperation>();
mockTestOperation.Setup(t => t.TotalTests).Returns(1);
mocker.GetMock<ITestOperationFactory>().Setup(f => f.Create(operation)).Returns(mockTestOperation.Object);
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(false);
mockAppOptions.Setup(o => o.RunWhenTestsFail).Returns(true);
mockAppOptions.Setup(o => o.RunWhenTestsExceed).Returns(0);
mockAppOptions.Setup(o => o.RunInParallel).Returns(false);
});
RaiseTestExecutionFinished();
AssertShouldNotReloadCoverage();
}
[Test]
public void Should_Handle_Any_Exception_In_OperationState_Changed_Handler_Logging_The_Exception()
{
var exception = new Exception();
mocker.GetMock<IFCCEngine>().Setup(engine => engine.StopCoverage()).Throws(exception);
RaiseTestExecutionCancelling();
mocker.Verify<FineCodeCoverage.Output.ILogger>(logger => logger.Log("Error processing unit test events", exception));
}
[TestCase(true)]
[TestCase(false)]
public void Should_Not_Handle_OperationState_Changes_When_The_testOperationStateInvocationManager_Cannot_Invoke(bool canInvoke)
{
var invoked = false;
testContainerDiscoverer.testOperationStateChangeHandlers = new Dictionary<TestOperationStates, Func<IOperation, Task>>
{
{TestOperationStates.TestExecutionCanceling, (_) => {invoked = true; return Task.CompletedTask; } }
};
var mockTestOperationStateInvocationManager = mocker.GetMock<ITestOperationStateInvocationManager>();
mockTestOperationStateInvocationManager.Setup(testOperationStateInvocationManager => testOperationStateInvocationManager.CanInvoke(It.IsAny<TestOperationStates>())).Returns(canInvoke);
RaiseTestExecutionCancelling();
Assert.That(invoked, Is.EqualTo(canInvoke));
}
[Test]
public void Should_Send_TestExecutionStartingMessage_When_TestExecutionStarting()
{
var operation = new Mock<IOperation>().Object;
RaiseTestExecutionStarting(operation);
mocker.Verify<IEventAggregator>(eventAggregator => eventAggregator.SendMessage(It.IsAny<TestExecutionStartingMessage>(),null));
}
[Test]
public void Should_MsCodeCoverageRunSettingsService_TestExecutionNotFinishedAsync_When_IsCollecting_TestExecutionFinishedAsync_And_CollectAsync_Not_Called()
{
var mockMsCodeCoverageRunSettingsService = mocker.GetMock<IMsCodeCoverageRunSettingsService>();
mockMsCodeCoverageRunSettingsService.Setup(
msCodeCoverageRunSettingsService =>
msCodeCoverageRunSettingsService.IsCollectingAsync(It.IsAny<ITestOperation>())
).ReturnsAsync(MsCodeCoverageCollectionStatus.Collecting);
SetUpOptions(mockOptions =>
{
mockOptions.Setup(options => options.Enabled).Returns(true);
mockOptions.Setup(options => options.RunWhenTestsFail).Returns(false);
});
var mockTestOperation = new Mock<ITestOperation>();
mockTestOperation.SetupGet(testOperation => testOperation.FailedTests).Returns(1);
mocker.GetMock<ITestOperationFactory>().Setup(f => f.Create(It.IsAny<IOperation>())).Returns(mockTestOperation.Object);
RaiseTestExecutionStarting();
RaiseTestExecutionFinished();
mockMsCodeCoverageRunSettingsService.Verify(msCodeCoverageRunSettingsService => msCodeCoverageRunSettingsService.TestExecutionNotFinishedAsync(mockTestOperation.Object));
}
[Test]
public void Should_Log_Coverage_Starting_With_Run_Number_When_TestExecutionStartingAsync_And_Coverage_Not_Disabled()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(true);
});
var operation = new Mock<IOperation>().Object;
RaiseTestExecutionStarting(operation);
mocker.Verify<FineCodeCoverage.Output.ILogger>(
logger => logger.Log("================================== COVERAGE STARTING - 1 =================================="));
RaiseTestExecutionStarting(operation);
mocker.Verify<FineCodeCoverage.Output.ILogger>(
logger => logger.Log("================================== COVERAGE STARTING - 2 =================================="));
}
[Test]
public void Should_Not_Log_Coverage_Starting_When_Coverage_Disabled()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(false);
mockAppOptions.Setup(o => o.DisabledNoCoverage).Returns(true);
});
var operation = new Mock<IOperation>().Object;
RaiseTestExecutionStarting(operation);
mocker.Verify<FineCodeCoverage.Output.ILogger>(
logger => logger.Log("================================== COVERAGE STARTING - 1 =================================="), Times.Never());
}
}
}