Exemplo n.º 1
0
 public VsTestRunner(
     StrykerOptions options,
     OptimizationFlags flags,
     ProjectInfo projectInfo,
     ICollection <TestCase> testCasesDiscovered,
     TestCoverageInfos mappingInfos,
     IFileSystem fileSystem        = null,
     IVsTestHelper helper          = null,
     ILogger logger                = null,
     IVsTestConsoleWrapper wrapper = null,
     Func <IDictionary <string, string>, int, IStrykerTestHostLauncher> hostBuilder = null)
 {
     _logger      = logger ?? ApplicationLogging.LoggerFactory.CreateLogger <VsTestRunner>();
     _fileSystem  = fileSystem ?? new FileSystem();
     _options     = options;
     _flags       = flags;
     _projectInfo = projectInfo;
     _hostBuilder = hostBuilder ?? ((dico, id) => new StrykerVsTestHostLauncher(dico, id));
     SetListOfTests(testCasesDiscovered);
     _ownHelper      = helper == null;
     _vsTestHelper   = helper ?? new VsTestHelper();
     CoverageMutants = mappingInfos ?? new TestCoverageInfos();
     _vsTestConsole  = wrapper ?? PrepareVsTestConsole();
     _id             = _count++;
     if (testCasesDiscovered != null)
     {
         _discoveredTests = testCasesDiscovered;
         DetectTestFramework(testCasesDiscovered);
     }
     InitializeVsTestConsole();
     _coverageEnvironment = new Dictionary <string, string>
     {
         { CoverageCollector.ModeEnvironmentVariable, flags.HasFlag(OptimizationFlags.UseEnvVariable) ? CoverageCollector.EnvMode : CoverageCollector.PipeMode }
     };
 }
Exemplo n.º 2
0
        public TestRunResult CaptureCoverage()
        {
            TestRunResult result;
            var           needCoverage = _flags.HasFlag(OptimizationFlags.CoverageBasedTest) || _flags.HasFlag(OptimizationFlags.SkipUncoveredMutants);

            if (needCoverage && _flags.HasFlag(OptimizationFlags.CaptureCoveragePerTest))
            {
                return(CaptureCoveragePerIsolatedTests());
            }
            var runner = TakeRunner();

            try
            {
                if (needCoverage)
                {
                    result    = runner.CaptureCoverage();
                    _coverage = runner.CoverageMutants;
                }
                else
                {
                    result = runner.RunAll(null, null);
                }
            }
            finally
            {
                ReturnRunner(runner);
            }
            return(result);
        }
Exemplo n.º 3
0
        public void Stryker_ShouldInvokeAllProcesses()
        {
            var initialisationMock      = new Mock <IInitialisationProcess>(MockBehavior.Strict);
            var mutationTestProcessMock = new Mock <IMutationTestProcess>(MockBehavior.Strict);
            var fileSystemMock          = new MockFileSystem();

            initialisationMock.Setup(x => x.Initialize(It.IsAny <StrykerOptions>())).Returns(new MutationTestInput()
            {
                ProjectInfo = new ProjectInfo()
                {
                    ProjectContents = new FolderComposite()
                    {
                        Name     = "ProjectRoot",
                        Children = new Collection <ProjectComponent>()
                        {
                            new FileLeaf()
                            {
                                Name    = "SomeFile.cs",
                                Mutants = new List <Mutant> {
                                    new Mutant {
                                        Id = 1
                                    }
                                }
                            }
                        }
                    }
                },
            });
            var options        = new StrykerOptions(basePath: "c:/test", fileSystem: fileSystemMock);
            var coveredMutants = new TestCoverageInfos();

            coveredMutants.DeclareMappingForATest(new TestDescription("1", "SomeTest"), new[] { 2, 3 }, new[] { 2 });
            var nbTests = 0;

            initialisationMock.Setup(x => x.InitialTest(It.IsAny <StrykerOptions>(), out nbTests)).Returns(0);
            initialisationMock.Setup(x => x.GetCoverage(It.IsAny <StrykerOptions>())).Returns(coveredMutants);

            mutationTestProcessMock.Setup(x => x.Mutate());
            mutationTestProcessMock.Setup(x => x.Test(It.IsAny <StrykerOptions>()))
            .Returns(new StrykerRunResult(It.IsAny <StrykerOptions>(), It.IsAny <decimal?>()));

            var target = new StrykerRunner(initialisationMock.Object, mutationTestProcessMock.Object, fileSystemMock);


            target.RunMutationTest(options);

            initialisationMock.Verify(x => x.Initialize(It.IsAny <StrykerOptions>()), Times.Once);
            mutationTestProcessMock.Verify(x => x.Mutate(), Times.Once);
            mutationTestProcessMock.Verify(x => x.Test(It.IsAny <StrykerOptions>()), Times.Once);
        }
Exemplo n.º 4
0
        public void Optimize(TestCoverageInfos coveredMutants)
        {
            if (coveredMutants == null)
            {
                return;
            }
            var covered = new HashSet <int>(coveredMutants.CoveredMutants);

            if (covered.Count == 0)
            {
                _logger.LogDebug("No mutant is covered by any test, no optimization done.");
                return;
            }
            _logger.LogDebug("Optimize test runs according to coverage info.");
            var report    = new StringBuilder();
            var nonTested = _input.ProjectInfo.ProjectContents.Mutants.Where(x =>
                                                                             x.ResultStatus == MutantStatus.NotRun && !covered.Contains(x.Id)).ToList();
            const MutantStatus mutantResultStatus = MutantStatus.Survived;

            foreach (var mutant in nonTested)
            {
                mutant.ResultStatus = mutantResultStatus;
            }

            foreach (var mutant in _input.ProjectInfo.ProjectContents.Mutants)
            {
                var tests = coveredMutants.GetTests <object>(mutant.Id);
                if (tests == null)
                {
                    continue;
                }
                mutant.CoveringTest = tests.Select(x => x.ToString()).ToList();
            }

            report.AppendJoin(',', nonTested.Select(x => x.Id));
            _logger.LogInformation(nonTested.Count == 0
                ? "Congratulations, all mutants are covered by tests!"
                : $"{nonTested.Count} mutants are not reached by any tests and will survive! (Marked as {mutantResultStatus}).");
        }
Exemplo n.º 5
0
 public VsTestRunner(StrykerOptions options,
                     OptimizationFlags flags,
                     ProjectInfo projectInfo,
                     ICollection <TestCase> testCasesDiscovered,
                     TestCoverageInfos mappingInfos,
                     IFileSystem fileSystem = null,
                     VsTestHelper helper    = null)
 {
     _fileSystem  = fileSystem ?? new FileSystem();
     _options     = options;
     _flags       = flags;
     _projectInfo = projectInfo;
     SetListOfTests(testCasesDiscovered);
     _ownHelper      = helper == null;
     _vsTestHelper   = helper ?? new VsTestHelper();
     CoverageMutants = mappingInfos ?? new TestCoverageInfos();
     _vsTestConsole  = PrepareVsTestConsole();
     _id             = _count++;
     InitializeVsTestConsole();
     _coverageEnvironment = new Dictionary <string, string>
     {
         { CoverageCollector.ModeEnvironmentVariable, flags.HasFlag(OptimizationFlags.UseEnvVariable) ? CoverageCollector.EnvMode : CoverageCollector.PipeMode }
     };
 }
Exemplo n.º 6
0
        public void MutationTestProcess_ShouldNotCallExecutorForNotCoveredMutants()
        {
            var mutant = new Mutant {
                Id = 1, ResultStatus = MutantStatus.Survived
            };
            var otherMutant = new Mutant {
                Id = 2, ResultStatus = MutantStatus.NotRun
            };
            var basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test");
            var input    = new MutationTestInput()
            {
                ProjectInfo = new ProjectInfo()
                {
                    TestProjectAnalyzerResult = new ProjectAnalyzerResult(null, null)
                    {
                        AssemblyPath = "/bin/Debug/netcoreapp2.1/TestName.dll",
                        Properties   = new Dictionary <string, string>()
                        {
                            { "TargetDir", "/bin/Debug/netcoreapp2.1" },
                            { "TargetFileName", "TestName.dll" }
                        }
                    },
                    ProjectUnderTestAnalyzerResult = new ProjectAnalyzerResult(null, null)
                    {
                        AssemblyPath = "/bin/Debug/netcoreapp2.1/TestName.dll",
                        Properties   = new Dictionary <string, string>()
                        {
                            { "TargetDir", "/bin/Debug/netcoreapp2.1" },
                            { "TargetFileName", "TestName.dll" }
                        }
                    },
                    ProjectContents = new FolderComposite()
                    {
                        Name     = "ProjectRoot",
                        Children = new Collection <ProjectComponent>()
                        {
                            new FileLeaf()
                            {
                                Name    = "SomeFile.cs",
                                Mutants = new Collection <Mutant>()
                                {
                                    mutant, otherMutant
                                }
                            }
                        }
                    },
                },
                AssemblyReferences = new ReferenceProvider().GetReferencedAssemblies()
            };
            var reporterMock = new Mock <IReporter>(MockBehavior.Strict);

            reporterMock.Setup(x => x.OnMutantTested(It.IsAny <Mutant>()));
            reporterMock.Setup(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>()));
            reporterMock.Setup(x => x.OnStartMutantTestRun(It.IsAny <IList <Mutant> >()));

            var executorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict);

            executorMock.SetupGet(x => x.TestRunner).Returns(Mock.Of <ITestRunner>());
            executorMock.Setup(x => x.Test(It.IsAny <Mutant>(), It.IsAny <int>()));

            var options = new StrykerOptions(fileSystem: new MockFileSystem(), basePath: basePath);

            var target = new MutationTestProcess(input,
                                                 reporterMock.Object,
                                                 executorMock.Object);
            var coverage = new TestCoverageInfos();

            coverage.DeclareMappingForATest("toto", new [] { 2 });
            target.Optimize(coverage);

            target.Test(options);

            reporterMock.Verify(x => x.OnStartMutantTestRun(It.Is <IList <Mutant> >(y => y.Count == 1)), Times.Once);
            executorMock.Verify(x => x.Test(otherMutant, It.IsAny <int>()), Times.Once);
            reporterMock.Verify(x => x.OnMutantTested(otherMutant), Times.Once);
            reporterMock.Verify(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>()), Times.Once);
        }