-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathSingleMicrosoftTestPlatformRunnerTests.cs
More file actions
1256 lines (1016 loc) · 42.6 KB
/
SingleMicrosoftTestPlatformRunnerTests.cs
File metadata and controls
1256 lines (1016 loc) · 42.6 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Shouldly;
using Stryker.Abstractions;
using Stryker.Abstractions.Testing;
using Stryker.TestRunner.Tests;
using Stryker.TestRunner.MicrosoftTestPlatform.Models;
using Stryker.TestRunner.Results;
namespace Stryker.TestRunner.MicrosoftTestPlatform.UnitTest;
[TestClass]
public class SingleMicrosoftTestPlatformRunnerTests
{
private Dictionary<string, List<TestNode>> _testsByAssembly = null!;
private Dictionary<string, MtpTestDescription> _testDescriptions = null!;
private TestSet _testSet = null!;
private object _discoveryLock = null!;
[TestInitialize]
public void Initialize()
{
_testsByAssembly = new Dictionary<string, List<TestNode>>();
_testDescriptions = new Dictionary<string, MtpTestDescription>();
_testSet = new TestSet();
_discoveryLock = new object();
}
private SingleMicrosoftTestPlatformRunner CreateRunner(int id = 0) =>
new(id, _testsByAssembly, _testDescriptions, _testSet, _discoveryLock, NullLogger.Instance);
[TestMethod, Timeout(1000)]
public async Task InitialTestAsync_CallsRunAssemblyTestsAsync_AndHandlesServerCreationFailure()
{
// Arrange - InitialTestAsync eventually calls RunAssemblyTestsAsync via RunAllTestsAsync
var project = new Mock<IProjectAndTests>();
var invalidAssembly = "/path/to/nonexistent.dll";
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { invalidAssembly });
using var runner = CreateRunner(0);
// Act - This will call RunAllTestsAsync -> ProcessSingleAssemblyAsync -> RunAssemblyTestsAsync
// RunAssemblyTestsAsync will handle the exception when GetOrCreateServerAsync fails
var result = await runner.InitialTestAsync(project.Object);
// Assert
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
result.FailingTests.ShouldNotBeNull();
result.Duration.ShouldBeGreaterThanOrEqualTo(TimeSpan.Zero);
}
[TestMethod, Timeout(1000)]
public async Task TestMultipleMutantsAsync_CallsRunAssemblyTestsAsync_WithNonExistentAssembly()
{
// Arrange
var project = new Mock<IProjectAndTests>();
var invalidAssembly = "/invalid/path/test.dll";
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { invalidAssembly });
var mutant = new Mock<IMutant>();
mutant.Setup(x => x.Id).Returns(1);
var mutants = new List<IMutant> { mutant.Object };
using var runner = CreateRunner(0);
// Act - Calls RunAllTestsAsync -> ProcessSingleAssemblyAsync -> RunAssemblyTestsAsync
var result = await runner.TestMultipleMutantsAsync(project.Object, null, mutants, null);
// Assert - RunAssemblyTestsAsync catches exceptions and returns TestRunResult
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
result.Messages.ShouldNotBeNull(); // Aggregate must handle null Messages without throwing
result.Duration.ShouldBeGreaterThanOrEqualTo(TimeSpan.Zero);
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsAsync_HandlesExceptionPath_WhenServerCreationFails()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { "/nonexistent.dll" });
using var runner = CreateRunner(0);
// Act - This exercises the exception handling in RunAssemblyTestsAsync
var result = await runner.InitialTestAsync(project.Object);
// Assert - RunAssemblyTestsAsync returns TestRunResult(false, ex.Message) on exception
result.ShouldNotBeNull();
var testRunResult = result as Stryker.TestRunner.Results.TestRunResult;
testRunResult.ShouldNotBeNull();
testRunResult.FailingTests.ShouldNotBeNull();
testRunResult.Messages.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
[DataRow("assembly-a.dll")]
[DataRow("assembly-b.dll")]
[DataRow("Some/Path/To/Assembly.dll")]
public void GetDiscoveredTests_ReturnsNull_WhenAssemblyNotRegistered(string assembly)
{
using var runner = CreateRunner();
var result = runner.GetDiscoveredTests(assembly);
result.ShouldBeNull();
}
[TestMethod, Timeout(1000)]
public void GetDiscoveredTests_ReturnsTests_WhenAssemblyIsRegistered()
{
var tests = new List<TestNode>
{
new("uid-1", "Test1", "test", "passed"),
new("uid-2", "Test2", "test", "failed")
};
_testsByAssembly["my-assembly.dll"] = tests;
using var runner = CreateRunner();
var result = runner.GetDiscoveredTests("my-assembly.dll");
result.ShouldNotBeNull();
result.Count.ShouldBe(2);
result[0].Uid.ShouldBe("uid-1");
result[1].Uid.ShouldBe("uid-2");
}
[TestMethod, Timeout(1000)]
public void GetDiscoveredTests_ReturnsEmptyList_WhenAssemblyHasNoTests()
{
_testsByAssembly["empty.dll"] = [];
using var runner = CreateRunner();
var result = runner.GetDiscoveredTests("empty.dll");
result.ShouldNotBeNull();
result.ShouldBeEmpty();
}
[TestMethod, Timeout(1000)]
public void CalculateAssemblyTimeout_ShouldNotAccumulate_WhenCalledMultipleTimes()
{
// Regression: RegisterInitialTestResult was called on every test run,
// causing InitialRunTime to grow unboundedly and inflating the timeout.
var test1 = new TestNode("uid-1", "Test1", "test", "passed");
var discoveredTests = new List<TestNode> { test1 };
var desc = new MtpTestDescription(test1);
desc.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(100)));
// Simulate a second run registering again (the bug)
desc.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(100)));
_testDescriptions["uid-1"] = desc;
int capturedEstimate = -1;
var timeoutCalc = new Mock<ITimeoutValueCalculator>();
timeoutCalc.Setup(x => x.CalculateTimeoutValue(It.IsAny<int>()))
.Callback<int>(ms => capturedEstimate = ms)
.Returns(500);
using var runner = CreateRunner();
runner.CalculateAssemblyTimeout(discoveredTests, timeoutCalc.Object, "test.dll");
// The estimated time should be 100ms (first registration only), not 200ms
capturedEstimate.ShouldBe(100);
}
[TestMethod, Timeout(1000)]
public void RegisterInitialTestResult_ShouldNotInflateRunTime_WhenCalledMultipleTimes()
{
// Regression: RegisterInitialTestResult was called on every mutation run,
// causing InitialRunTime to grow unboundedly and inflating the timeout.
var testNode = new TestNode("uid-1", "Test1", "test", "passed");
var desc = new MtpTestDescription(testNode);
desc.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(100)));
desc.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(100)));
desc.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(100)));
// Should stay at the first registered value, not accumulate
desc.InitialRunTime.TotalMilliseconds.ShouldBe(100);
}
[TestMethod, Timeout(1000)]
[DataRow(100, 500, 500)]
[DataRow(0, 500, 500)]
[DataRow(250, 1000, 1000)]
public void CalculateAssemblyTimeout_ReturnsExpectedTimeout(
int initialRunTimeMs, int calculatorReturns, int expectedMs)
{
var testNode = new TestNode("uid-1", "Test1", "test", "passed");
var discoveredTests = new List<TestNode> { testNode };
var desc = new MtpTestDescription(testNode);
desc.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(initialRunTimeMs)));
_testDescriptions["uid-1"] = desc;
var timeoutCalc = new Mock<ITimeoutValueCalculator>();
timeoutCalc.Setup(x => x.CalculateTimeoutValue(It.IsAny<int>()))
.Returns(calculatorReturns);
using var runner = CreateRunner();
var result = runner.CalculateAssemblyTimeout(discoveredTests, timeoutCalc.Object, "test.dll");
result.ShouldNotBeNull();
result.Value.TotalMilliseconds.ShouldBe(expectedMs);
}
[TestMethod, Timeout(1000)]
public void CalculateAssemblyTimeout_SumsMultipleTestDurations()
{
var test1 = new TestNode("uid-1", "Test1", "test", "passed");
var test2 = new TestNode("uid-2", "Test2", "test", "passed");
var discoveredTests = new List<TestNode> { test1, test2 };
var desc1 = new MtpTestDescription(test1);
desc1.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(100)));
_testDescriptions["uid-1"] = desc1;
var desc2 = new MtpTestDescription(test2);
desc2.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(200)));
_testDescriptions["uid-2"] = desc2;
int capturedEstimate = -1;
var timeoutCalc = new Mock<ITimeoutValueCalculator>();
timeoutCalc.Setup(x => x.CalculateTimeoutValue(It.IsAny<int>()))
.Callback<int>(ms => capturedEstimate = ms)
.Returns(999);
using var runner = CreateRunner();
runner.CalculateAssemblyTimeout(discoveredTests, timeoutCalc.Object, "test.dll");
capturedEstimate.ShouldBe(300);
}
[TestMethod, Timeout(1000)]
public void CalculateAssemblyTimeout_SkipsTestsWithoutDescription()
{
var test1 = new TestNode("uid-1", "Test1", "test", "passed");
var testWithoutDesc = new TestNode("uid-unknown", "Unknown", "test", "passed");
var discoveredTests = new List<TestNode> { test1, testWithoutDesc };
var desc1 = new MtpTestDescription(test1);
desc1.RegisterInitialTestResult(new MtpTestResult(TimeSpan.FromMilliseconds(150)));
_testDescriptions["uid-1"] = desc1;
int capturedEstimate = -1;
var timeoutCalc = new Mock<ITimeoutValueCalculator>();
timeoutCalc.Setup(x => x.CalculateTimeoutValue(It.IsAny<int>()))
.Callback<int>(ms => capturedEstimate = ms)
.Returns(777);
using var runner = CreateRunner();
runner.CalculateAssemblyTimeout(discoveredTests, timeoutCalc.Object, "test.dll");
capturedEstimate.ShouldBe(150);
}
[TestMethod, Timeout(1000)]
public async Task HandleAssemblyTimeoutAsync_AddsAllTestUidsToTimedOutList()
{
var tests = new List<TestNode>
{
new("uid-1", "Test1", "test", "passed"),
new("uid-2", "Test2", "test", "passed"),
new("uid-3", "Test3", "test", "passed")
};
var timedOutTests = new List<string>();
using var runner = CreateRunner();
await runner.HandleAssemblyTimeoutAsync("some-assembly.dll", tests, timedOutTests);
timedOutTests.ShouldBe(["uid-1", "uid-2", "uid-3"]);
}
[TestMethod, Timeout(1000)]
public async Task HandleAssemblyTimeoutAsync_AppendsToExistingTimedOutList()
{
var tests = new List<TestNode> { new("uid-new", "NewTest", "test", "passed") };
var timedOutTests = new List<string> { "uid-existing" };
using var runner = CreateRunner();
await runner.HandleAssemblyTimeoutAsync("assembly.dll", tests, timedOutTests);
timedOutTests.Count.ShouldBe(2);
timedOutTests.ShouldContain("uid-existing");
timedOutTests.ShouldContain("uid-new");
}
[TestMethod, Timeout(1000)]
public async Task HandleAssemblyTimeoutAsync_HandlesEmptyTestList()
{
var tests = new List<TestNode>();
var timedOutTests = new List<string>();
using var runner = CreateRunner();
await runner.HandleAssemblyTimeoutAsync("assembly.dll", tests, timedOutTests);
timedOutTests.ShouldBeEmpty();
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsInternalAsync_ReturnsFailedResult_WhenServerCreationFails()
{
using var runner = CreateRunner();
var (result, timedOut) = await runner.RunAssemblyTestsInternalAsync("/nonexistent/assembly.dll", null, null);
timedOut.ShouldBeFalse();
result.ShouldNotBeNull();
result.ResultMessage.ShouldNotBeNullOrEmpty();
}
[TestMethod, Timeout(1000)]
[DataRow("/path/a.dll")]
[DataRow("/another/path/b.dll")]
public async Task RunAssemblyTestsInternalAsync_CatchesException_AndReturnsResult(string assembly)
{
using var runner = CreateRunner();
var (result, timedOut) = await runner.RunAssemblyTestsInternalAsync(assembly, null, null);
timedOut.ShouldBeFalse();
result.ShouldBeOfType<TestRunResult>();
result.Duration.ShouldBe(TimeSpan.Zero);
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsInternalAsync_WithTimeout_StillReturnsResult_WhenServerFails()
{
using var runner = CreateRunner();
var timeout = TimeSpan.FromMilliseconds(100);
var (result, timedOut) = await runner.RunAssemblyTestsInternalAsync("/nonexistent.dll", null, timeout)!;
timedOut.ShouldBeFalse();
result.ShouldNotBeNull();
result.FailingTests.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsAsync_WithTimeout_DoesNotHangOnRealAssembly()
{
// Arrange - This test ensures we don't try to start real servers that would hang
var fakeAssemblyPath = "/path/to/fake/test.dll";
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { fakeAssemblyPath });
var mutant = new Mock<IMutant>();
mutant.Setup(x => x.Id).Returns(1);
var mutants = new List<IMutant> { mutant.Object };
var timeoutCalc = new Mock<ITimeoutValueCalculator>();
timeoutCalc.Setup(x => x.CalculateTimeoutValue(It.IsAny<int>())).Returns(1);
// Add discovered tests
var testNode = new TestNode("test1", "TestMethod1", "passed", "passed");
_testsByAssembly[fakeAssemblyPath] = new List<TestNode> { testNode };
_testDescriptions["test1"] = new MtpTestDescription(testNode);
using var runner = CreateRunner(0);
// Act - Should complete quickly without hanging, even though we have a timeout calculator
var result = await runner.TestMultipleMutantsAsync(project.Object, timeoutCalc.Object, mutants, null);
// Assert - The test completes without hanging (file doesn't exist so returns early)
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsAsync_RegistersTestResults_InTestDescriptions()
{
// Arrange
var testNode = new TestNode("test1", "TestMethod1", "passed", "passed");
var mtpTestDesc = new MtpTestDescription(testNode);
_testDescriptions["test1"] = mtpTestDesc;
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { "/nonexistent/assembly.dll" });
using var runner = CreateRunner(0);
// Act
var result = await runner.InitialTestAsync(project.Object);
// Assert - Test descriptions should still be in the dictionary
_testDescriptions.ShouldContainKey("test1");
result.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsAsync_CalculatesDuration()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { "/nonexistent.dll" });
using var runner = CreateRunner(0);
// Act
var startTime = DateTime.UtcNow;
var result = await runner.InitialTestAsync(project.Object);
var endTime = DateTime.UtcNow;
// Assert - Duration should be calculated
result.ShouldNotBeNull();
result.Duration.ShouldBeGreaterThanOrEqualTo(TimeSpan.Zero);
result.Duration.ShouldBeLessThan(endTime - startTime + TimeSpan.FromSeconds(2));
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsAsync_WithMultipleMutants_UsesNegativeOneMutantId()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { "/test.dll" });
var mutant1 = new Mock<IMutant>();
mutant1.Setup(x => x.Id).Returns(1);
var mutant2 = new Mock<IMutant>();
mutant2.Setup(x => x.Id).Returns(2);
var mutants = new List<IMutant> { mutant1.Object, mutant2.Object };
using var runner = CreateRunner(0);
// Act - With multiple mutants, mutantId should be -1 (no mutation)
var result = await runner.TestMultipleMutantsAsync(project.Object, null, mutants, null);
// Assert
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsAsync_WithSingleMutant_UsesMutantId()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { "/test.dll" });
var mutant = new Mock<IMutant>();
mutant.Setup(x => x.Id).Returns(42);
var mutants = new List<IMutant> { mutant.Object };
using var runner = CreateRunner(0);
// Act - With single mutant, that mutant's ID should be used
var result = await runner.TestMultipleMutantsAsync(project.Object, null, mutants, null);
// Assert
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task RunAssemblyTestsAsync_IncludesResultMessage_OnError()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { "/invalid/assembly.dll" });
using var runner = CreateRunner(0);
// Act
var result = await runner.InitialTestAsync(project.Object);
// Assert - Result message should be included on error
result.ShouldNotBeNull();
result.ResultMessage.ShouldNotBeNull();
}
[TestCleanup]
public void Cleanup()
{
// Clean up any temporary coverage files created during tests
for (int id = 1; id <= 20; id++)
{
var coverageFilePath = Path.Combine(Path.GetTempPath(), $"stryker-coverage-{id}.txt");
try
{
if (File.Exists(coverageFilePath))
{
File.Delete(coverageFilePath);
}
}
catch
{
// Ignore cleanup errors
}
}
}
[TestMethod, Timeout(1000)]
public async Task DiscoverTestsAsync_ShouldReturnFalse_WhenAssemblyNotFound()
{
// Arrange
using var runner = new SingleMicrosoftTestPlatformRunner(
0,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Act
var result = await runner.DiscoverTestsAsync("/nonexistent/assembly.dll");
// Assert
result.ShouldBeFalse();
}
[TestMethod, Timeout(1000)]
public async Task InitialTestAsync_ShouldReturnTestRunResult()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string> { "/nonexistent/assembly.dll" });
using var runner = new SingleMicrosoftTestPlatformRunner(
0,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Act
var result = await runner.InitialTestAsync(project.Object);
// Assert
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
result.FailingTests.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task TestMultipleMutantsAsync_ShouldReturnTestRunResult_WithNoAssemblies()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string>());
var mutant = new Mock<IMutant>();
mutant.Setup(x => x.Id).Returns(1);
var mutants = new List<IMutant> { mutant.Object };
using var runner = new SingleMicrosoftTestPlatformRunner(
0,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Act
var result = await runner.TestMultipleMutantsAsync(project.Object, null, mutants, null);
// Assert
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
result.FailingTests.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task TestMultipleMutantsAsync_ShouldUseCorrectMutantId_WhenSingleMutant()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string>());
var mutant = new Mock<IMutant>();
mutant.Setup(x => x.Id).Returns(42);
var mutants = new List<IMutant> { mutant.Object };
using var runner = new SingleMicrosoftTestPlatformRunner(
0,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Act
var result = await runner.TestMultipleMutantsAsync(project.Object, null, mutants, null);
// Assert
result.ShouldNotBeNull();
mutant.Verify(x => x.Id, Times.AtLeastOnce);
}
[TestMethod, Timeout(1000)]
public async Task TestMultipleMutantsAsync_ShouldUseNoMutationId_WhenMultipleMutants()
{
// Arrange
var project = new Mock<IProjectAndTests>();
project.Setup(x => x.GetTestAssemblies()).Returns(new List<string>());
var mutant1 = new Mock<IMutant>();
mutant1.Setup(x => x.Id).Returns(1);
var mutant2 = new Mock<IMutant>();
mutant2.Setup(x => x.Id).Returns(2);
var mutants = new List<IMutant> { mutant1.Object, mutant2.Object };
using var runner = new SingleMicrosoftTestPlatformRunner(
0,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Act
var result = await runner.TestMultipleMutantsAsync(project.Object, null, mutants, null);
// Assert
result.ShouldNotBeNull();
result.ExecutedTests.ShouldNotBeNull();
}
[TestMethod, Timeout(1000)]
public async Task Dispose_ShouldCleanUpResources()
{
// Arrange
var testableRunner = new TestableRunner(
123,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Verify mutant file was created
TestableRunner.MutantFilePath.ShouldNotBeNull();
var mutantFilePath = TestableRunner.MutantFilePath;
// Create the mutant file manually to test deletion
await File.WriteAllTextAsync(mutantFilePath, "-1");
File.Exists(mutantFilePath).ShouldBeTrue("Mutant file should exist before disposal");
// Act
testableRunner.Dispose();
// Assert
testableRunner.DisposedFlagWasSet.ShouldBeTrue("_disposed flag should be set to true");
testableRunner.DisposeLogicExecutedCount.ShouldBe(1, "Dispose logic should execute once on first call");
File.Exists(mutantFilePath).ShouldBeFalse("Mutant file should be deleted after disposal");
// Act - Second disposal (verify idempotency via _disposed flag check)
testableRunner.Dispose();
// Assert
testableRunner.DisposeLogicExecutedCount.ShouldBe(1, "Dispose logic should only execute once due to _disposed flag check preventing re-execution");
}
[TestMethod, Timeout(1000)]
public void SetCoverageMode_ShouldEnableCoverageMode()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
1,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Act
runner.SetCoverageMode(true);
// Assert
runner.IsCoverageModeEnabled.ShouldBeTrue();
}
[TestMethod, Timeout(1000)]
public void SetCoverageMode_ShouldDisableCoverageMode()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
2,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
runner.SetCoverageMode(true);
// Act
runner.SetCoverageMode(false);
// Assert
runner.IsCoverageModeEnabled.ShouldBeFalse();
}
[TestMethod, Timeout(1000)]
public void SetCoverageMode_ShouldBeIdempotent_WhenCalledWithTrue()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
3,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
runner.SetCoverageMode(true);
// Act - Call with same value
runner.SetCoverageMode(true);
// Assert - Should still be enabled and not throw any exceptions
runner.IsCoverageModeEnabled.ShouldBeTrue();
}
[TestMethod, Timeout(1000)]
public void SetCoverageMode_ShouldBeIdempotent_WhenCalledWithFalse()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
4,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Default is false, so calling it again should be idempotent
runner.IsCoverageModeEnabled.ShouldBeFalse();
// Act - Call with same value as default
runner.SetCoverageMode(false);
// Assert - Should still be disabled and not throw any exceptions
runner.IsCoverageModeEnabled.ShouldBeFalse();
}
[TestMethod, Timeout(1000)]
public void SetCoverageMode_ShouldDeleteCoverageFile_WhenEnabling()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
5,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Create a coverage file
File.WriteAllText(runner.CoverageFilePath, "1,2,3;4,5,6");
File.Exists(runner.CoverageFilePath).ShouldBeTrue();
// Act
runner.SetCoverageMode(true);
// Assert
File.Exists(runner.CoverageFilePath).ShouldBeFalse("Coverage file should be deleted when enabling coverage mode");
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldReturnEmptyLists_WhenFileDoesNotExist()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
6,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
// Ensure file doesn't exist
if (File.Exists(runner.CoverageFilePath))
{
File.Delete(runner.CoverageFilePath);
}
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBeEmpty();
staticMutants.ShouldBeEmpty();
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldReturnEmptyLists_WhenFileIsEmpty()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
7,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, "");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBeEmpty();
staticMutants.ShouldBeEmpty();
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldReturnEmptyLists_WhenFileContainsOnlyWhitespace()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
8,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, " \n\t ");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBeEmpty();
staticMutants.ShouldBeEmpty();
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldParseCoveredMutantsOnly()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
9,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, "1,2,3");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBe(new[] { 1, 2, 3 });
staticMutants.ShouldBeEmpty();
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldParseBothCoveredAndStaticMutants()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
10,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, "1,2,3;4,5,6");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBe(new[] { 1, 2, 3 });
staticMutants.ShouldBe(new[] { 4, 5, 6 });
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldHandleEmptyCoveredSection()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
11,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, ";4,5,6");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBeEmpty();
staticMutants.ShouldBe(new[] { 4, 5, 6 });
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldHandleEmptyStaticSection()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
12,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, "1,2,3;");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBe(new[] { 1, 2, 3 });
staticMutants.ShouldBeEmpty();
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldHandleDataWithSpaces()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
13,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, " 1 , 2 , 3 ; 4 , 5 , 6 ");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBe(new[] { 1, 2, 3 });
staticMutants.ShouldBe(new[] { 4, 5, 6 });
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldSkipInvalidNumbers()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
14,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, "1,invalid,3;4,bad,6");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBe(new[] { 1, 3 });
staticMutants.ShouldBe(new[] { 4, 6 });
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldHandleMixedValidAndInvalidData()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
15,
_testsByAssembly,
_testDescriptions,
_testSet,
_discoveryLock,
NullLogger.Instance);
File.WriteAllText(runner.CoverageFilePath, "1,,3,notanumber,5;,,7,xyz,9");
// Act
var (coveredMutants, staticMutants) = runner.ReadCoverageData();
// Assert
coveredMutants.ShouldBe(new[] { 1, 3, 5 });
staticMutants.ShouldBe(new[] { 7, 9 });
}
[TestMethod, Timeout(1000)]
public void ReadCoverageData_ShouldReturnEmptyLists_OnFileReadException()
{
// Arrange
using var runner = new TestableRunnerForCoverage(
16,
_testsByAssembly,