示例#1
0
        /// <inheritdoc />
        protected override object RunIsolatedTaskInHost <TIsolatedTask>(HostSetup hostSetup, StatusReporter statusReporter, object[] args)
        {
            NCoverTool tool = NCoverTool.GetInstance(version, hostSetup.ProcessorArchitecture);

            if (!tool.IsInstalled())
            {
                throw new TestIsolationException(string.Format("{0} does not appear to be installed.", tool.Name));
            }

            string ncoverArguments, ncoverCoverageFile;

            NCoverHost.GetNCoverProperties(hostSetup, out ncoverArguments, out ncoverCoverageFile);

            if (File.Exists(ncoverCoverageFile))
            {
                File.Delete(ncoverCoverageFile);
            }

            if (batch != null)
            {
                hostSetup = hostSetup.Copy();

                string tempCoverageFile = batch.Enlist(ncoverCoverageFile);
                NCoverHost.SetNCoverCoverageFile(hostSetup, tempCoverageFile);
            }

            return(base.RunIsolatedTaskInHost <TIsolatedTask>(hostSetup, statusReporter, args));
        }
        public void GeneratesNCoverCoverageWithCorrectOptionsAndMergesIfNecessary(string factoryName, NCoverVersion version,
                                                                                  string ncoverCoverageFile, bool includeLogArgument, bool runMultipleAssemblies)
        {
            string tempPath            = SpecialPathPolicy.For <NCoverTestIsolationProviderIntegrationTest>().GetTempDirectory().FullName;
            string coverageFilePath    = Path.Combine(tempPath, ncoverCoverageFile ?? "Coverage.xml");
            string coverageLogFilePath = Path.Combine(tempPath, "CoverageLog.txt");

            string ncoverArguments = includeLogArgument
                ? "//l \"" + coverageLogFilePath + "\"" + (version == NCoverVersion.V1 ? "" : " //ll Normal")
                : null;

            if (File.Exists(coverageFilePath))
            {
                File.Delete(coverageFilePath);
            }

            if (File.Exists(coverageLogFilePath))
            {
                File.Delete(coverageLogFilePath);
            }

            Type firstTestType  = typeof(SimpleTest);
            Type secondTestType = typeof(DummyTest);

            TestLauncher launcher = new TestLauncher();

            launcher.Logger = new MarkupStreamLogger(TestLog.Default);

            launcher.TestProject.TestPackage.AddFile(new FileInfo(AssemblyUtils.GetAssemblyLocalPath(firstTestType.Assembly)));
            if (runMultipleAssemblies)
            {
                launcher.TestProject.TestPackage.AddFile(new FileInfo(AssemblyUtils.GetAssemblyLocalPath(secondTestType.Assembly)));
            }

            launcher.TestProject.TestRunnerFactoryName = factoryName;
            launcher.TestExecutionOptions.FilterSet    = new FilterSet <ITestDescriptor>(
                new OrFilter <ITestDescriptor>(new[]
            {
                new TypeFilter <ITestDescriptor>(new EqualityFilter <string>(firstTestType.FullName), false),
                new TypeFilter <ITestDescriptor>(new EqualityFilter <string>(secondTestType.FullName), false),
            }));

            if (ncoverArguments != null)
            {
                launcher.TestRunnerOptions.AddProperty("NCoverArguments", ncoverArguments);
            }
            if (ncoverCoverageFile != null)
            {
                launcher.TestRunnerOptions.AddProperty("NCoverCoverageFile", ncoverCoverageFile);
            }

            TestLauncherResult result;

            using (new CurrentDirectorySwitcher(tempPath))
                result = launcher.Run();

            NCoverTool tool = NCoverTool.GetInstance(version, ProcessorArchitecture.MSIL);

            if (!tool.IsInstalled())
            {
                Assert.AreEqual(ResultCode.Failure, result.ResultCode);

                var logEntries = result.Report.LogEntries;
                Assert.AreEqual(1, logEntries.Count, "Expected to find a log entry.");
                Assert.AreEqual(LogSeverity.Error, logEntries[0].Severity);
                Assert.Contains(logEntries[0].Details, tool.Name + " does not appear to be installed.");
            }
            else
            {
                int passed = runMultipleAssemblies ? 2 : 1;
                int failed = 1;

                Assert.AreEqual(passed + failed, result.Statistics.RunCount);
                Assert.AreEqual(passed, result.Statistics.PassedCount);
                Assert.AreEqual(failed, result.Statistics.FailedCount);
                Assert.AreEqual(0, result.Statistics.InconclusiveCount);
                Assert.AreEqual(0, result.Statistics.SkippedCount);

                Assert.IsTrue(File.Exists(coverageFilePath),
                              "The NCover runner should have written its coverage log to '{0}'.", coverageFilePath);

                string coverageXml = File.ReadAllText(coverageFilePath);
                File.Delete(coverageFilePath);

                Assert.Contains(coverageXml, firstTestType.Name,
                                "Expected the coverage log to include information about the test method that we actually ran.\n"
                                + "In NCover v3, there is now a list of excluded 'system assemblies' in NCover.Console.exe.config which "
                                + "specifies MbUnit and Gallio by default.  For this test to run, the file must be edited such that "
                                + "these entries are removed.");

                if (runMultipleAssemblies)
                {
                    Assert.Contains(coverageXml, secondTestType.Name,
                                    "Expected the coverage log to include information about the test method that we actually ran.\n"
                                    + "In NCover v3, there is now a list of excluded 'system assemblies' in NCover.Console.exe.config which "
                                    + "specifies MbUnit and Gallio by default.  For this test to run, the file must be edited such that "
                                    + "these entries are removed.");
                }

                if (ncoverArguments != null && ncoverArguments.Contains("/l"))
                {
                    Assert.IsTrue(File.Exists(coverageLogFilePath),
                                  "Should have created a coverage log file since the /l argument was specified.");
                    File.Delete(coverageLogFilePath);
                }
            }
        }