public void InitialisationProcess_ShouldCallNeededResolvers()
        {
            var testRunnerMock                = new Mock <ITestRunner>(MockBehavior.Strict);
            var inputFileResolverMock         = new Mock <IInputFileResolver>(MockBehavior.Strict);
            var initialBuildProcessMock       = new Mock <IInitialBuildProcess>(MockBehavior.Strict);
            var initialTestProcessMock        = new Mock <IInitialTestProcess>(MockBehavior.Strict);
            var assemblyReferenceResolverMock = new Mock <IAssemblyReferenceResolver>(MockBehavior.Strict);

            testRunnerMock.Setup(x => x.RunAll(It.IsAny <int>(), null, null))
            .Returns(new TestRunResult(true));     // testrun is successful
            testRunnerMock.Setup(x => x.DiscoverNumberOfTests()).Returns(999);
            testRunnerMock.Setup(x => x.Dispose());
            var projectContents = new FolderComposite();

            projectContents.Add(new FileLeaf
            {
                Name = "SomeFile.cs"
            });
            var folder = new FolderComposite
            {
                Name = "ProjectRoot"
            };

            folder.AddRange(new Collection <IProjectComponent>
            {
                new FileLeaf
                {
                    Name = "SomeFile.cs"
                }
            });
            inputFileResolverMock.Setup(x => x.ResolveInput(It.IsAny <IStrykerOptions>()))
            .Returns(new ProjectInfo
            {
                ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(
                    references: new string[0]).Object,
                TestProjectAnalyzerResults = new List <IAnalyzerResult> {
                    TestHelper.SetupProjectAnalyzerResult(
                        projectFilePath: "C://Example/Dir/ProjectFolder").Object
                },
                ProjectContents = folder
            });
            initialTestProcessMock.Setup(x => x.InitialTest(It.IsAny <ITestRunner>())).Returns(999);
            initialBuildProcessMock.Setup(x => x.InitialBuild(It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <string>()));
            assemblyReferenceResolverMock.Setup(x => x.LoadProjectReferences(It.IsAny <string[]>()))
            .Returns(Enumerable.Empty <PortableExecutableReference>());

            var target = new InitialisationProcess(
                inputFileResolverMock.Object,
                initialBuildProcessMock.Object,
                initialTestProcessMock.Object,
                testRunnerMock.Object,
                assemblyReferenceResolverMock.Object);

            var options = new StrykerOptions();

            var result = target.Initialize(options);

            inputFileResolverMock.Verify(x => x.ResolveInput(It.IsAny <IStrykerOptions>()), Times.Once);
        }
Exemplo n.º 2
0
 /// <summary>
 /// In the case of multiple projects we wrap them inside a wrapper root component. Otherwise the only project root will be the root component.
 /// </summary>
 /// <param name="projectComponents">A list of all project root components</param>
 /// <param name="options">The current stryker options</param>
 /// <returns>The root folder component</returns>
 private IProjectComponent AddRootFolderIfMultiProject(IEnumerable <IProjectComponent> projectComponents, StrykerOptions options)
 {
     if (projectComponents.Count() > 1)
     {
         var rootComponent = new FolderComposite
         {
             FullPath = options.BasePath // in case of a solution run the basepath will be where the solution file is
         };
         rootComponent.AddRange(projectComponents);
         return(rootComponent);
     }
     else
     {
         return(projectComponents.FirstOrDefault());
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Starts a mutation test run
        /// </summary>
        /// <exception cref="StrykerInputException">For managed exceptions</exception>
        /// <param name="options">The user options</param>
        /// <param name="initialLogMessages">
        /// Allows to pass log messages that occured before the mutation test.
        /// The messages will be written to the logger after it was configured.
        /// </param>
        public StrykerRunResult RunMutationTest(StrykerOptions options, IEnumerable <LogMessage> initialLogMessages = null)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var reporters = _reporterFactory.Create(options);

            SetupLogging(options, initialLogMessages);

            try
            {
                _mutationTestProcesses = _projectOrchestrator.MutateProjects(options, reporters).ToList();

                IParentComponent rootComponent = new FolderComposite();
                rootComponent.AddRange(_mutationTestProcesses.Select(x => x.Input.ProjectInfo.ProjectContents));

                _logger.LogInformation("{0} mutants ready for test", rootComponent.Mutants.Count());

                AnalyseCoverage(options);
                var readOnlyInputComponent = rootComponent.ToReadOnlyInputComponent();
                reporters.OnMutantsCreated(readOnlyInputComponent);

                var allMutants    = rootComponent.Mutants.ToList();
                var mutantsNotRun = allMutants.Where(x => x.ResultStatus == MutantStatus.NotRun).ToList();

                if (!mutantsNotRun.Any())
                {
                    if (allMutants.Any(x => x.ResultStatus == MutantStatus.Ignored))
                    {
                        _logger.LogWarning("It looks like all mutants with tests were excluded. Try a re-run with less exclusion!");
                    }
                    if (allMutants.Any(x => x.ResultStatus == MutantStatus.NoCoverage))
                    {
                        _logger.LogWarning("It looks like all non-excluded mutants are not covered by a test. Go add some tests!");
                    }
                    if (!allMutants.Any())
                    {
                        _logger.LogWarning("It\'s a mutant-free world, nothing to test.");
                    }
                    return(new StrykerRunResult(options, double.NaN));
                }

                reporters.OnStartMutantTestRun(mutantsNotRun);

                foreach (var project in _mutationTestProcesses)
                {
                    // test mutations
                    project.Test(project.Input.ProjectInfo.ProjectContents.Mutants.Where(x => x.ResultStatus == MutantStatus.NotRun).ToList());
                }

                reporters.OnAllMutantsTested(readOnlyInputComponent);

                return(new StrykerRunResult(options, readOnlyInputComponent.GetMutationScore()));
            }
            catch (Exception ex) when(!(ex is StrykerInputException))
            {
                _logger.LogError(ex, "An error occurred during the mutation test run ");
                throw;
            }
            finally
            {
                // log duration
                stopwatch.Stop();
                _logger.LogInformation("Time Elapsed {0}", stopwatch.Elapsed);
            }
        }