Exemple #1
0
        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 CsharpFolderComposite();

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

            folder.AddRange(new Collection <IProjectComponent>
            {
                new CsharpFileLeaf
                {
                    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);
        }
Exemple #2
0
        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 <ITimeoutValueCalculator>(), null, null))
            .Returns(new TestRunResult(true));     // testrun is successful
            testRunnerMock.Setup(x => x.DiscoverTests()).Returns(new TestSet());
            testRunnerMock.Setup(x => x.Dispose());
            var projectContents = new CsharpFolderComposite();

            projectContents.Add(new CsharpFileLeaf());
            var folder = new CsharpFolderComposite();

            folder.AddRange(new Collection <IProjectComponent>
            {
                new CsharpFileLeaf()
            });
            inputFileResolverMock.Setup(x => x.ResolveInput(It.IsAny <StrykerOptions>()))
            .Returns(new ProjectInfo(new MockFileSystem())
            {
                ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(
                    references: new string[0]).Object,
                TestProjectAnalyzerResults = new List <IAnalyzerResult> {
                    TestHelper.SetupProjectAnalyzerResult(
                        projectFilePath: "C://Example/Dir/ProjectFolder",
                        targetFramework: "netcoreapp2.1").Object
                },
                ProjectContents = folder
            });
            initialTestProcessMock.Setup(x => x.InitialTest(It.IsAny <StrykerOptions>(), It.IsAny <ITestRunner>())).Returns(new InitialTestRun(new TestRunResult(true), new TimeoutValueCalculator(1)));
            initialBuildProcessMock.Setup(x => x.InitialBuild(It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <string>(), null));
            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
            {
                ProjectName    = "TheProjectName",
                ProjectVersion = "TheProjectVersion"
            };

            var result = target.Initialize(options);

            inputFileResolverMock.Verify(x => x.ResolveInput(It.IsAny <StrykerOptions>()), Times.Once);
        }
Exemple #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();

                IFolderComposite rootComponent = new CsharpFolderComposite();
                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);
            }
        }