Beispiel #1
0
 /// <summary>
 /// Creates a sample runner.
 /// </summary>
 public SampleRunner()
 {
     testPackage = new TestPackage();
     filters = new List<Filter<ITestDescriptor>>();
     TestRunnerFactoryName = StandardTestRunnerFactoryNames.Local;
     testRunnerOptions = new TestRunnerOptions();
 }
Beispiel #2
0
        /// <summary>
        /// Creates a copy of the options.
        /// </summary>
        /// <returns>The copy.</returns>
        public TestRunnerOptions Copy()
        {
            TestRunnerOptions copy = new TestRunnerOptions();

            copy.properties.AddAll(properties);

            return(copy);
        }
Beispiel #3
0
 /// <summary>
 /// Creates a launcher with default options and no test assemblies specified.
 /// </summary>
 public TestLauncher()
 {
     filePatterns            = new List <string>();
     testProject             = new TestProject();
     testRunnerOptions       = new TestRunnerOptions();
     testExplorationOptions  = new TestExplorationOptions();
     testExecutionOptions    = new TestExecutionOptions();
     reportFormats           = new List <string>();
     reportFormatterOptions  = new ReportFormatterOptions();
     progressMonitorProvider = NullProgressMonitorProvider.Instance;
     logger = NullLogger.Instance;
 }
        private void RunAllTests(IRunContext runContext)
        {
            ITestRunnerManager runnerManager = RuntimeAccessor.ServiceLocator.Resolve<ITestRunnerManager>();
            var runner = runnerManager.CreateTestRunner(StandardTestRunnerFactoryNames.IsolatedAppDomain);
            runner.RegisterExtension(new RunContextExtension(runContext));

            ILogger logger = new RunContextLogger(runContext);
            TestRunnerOptions testRunnerOptions = new TestRunnerOptions();

            try
            {
                RunWithProgressMonitor(delegate(IProgressMonitor progressMonitor)
                {
                    runner.Initialize(testRunnerOptions, logger, progressMonitor);
                });

                if (isCanceled)
                    return;

                TestPackage testPackage = new TestPackage();
                testPackage.AddExcludedTestFrameworkId("MSTestAdapter.TestFramework");

                foreach (ITestElement testElement in runContext.RunConfig.TestElements)
                {
                    GallioTestElement gallioTestElement = testElement as GallioTestElement;
                    if (gallioTestElement != null)
                    {
                        testPackage.AddFile(new FileInfo(gallioTestElement.AssemblyPath));
                    }
                }

                TestExplorationOptions testExplorationOptions = new TestExplorationOptions();
                TestExecutionOptions testExecutionOptions = new TestExecutionOptions();

                List<Filter<string>> idFilters = new List<Filter<string>>();
                foreach (ITestElement includedTestElement in runContext.RunConfig.TestElements)
                {
                    GallioTestElement gallioTestElement = includedTestElement as GallioTestElement;
                    if (gallioTestElement != null)
                        idFilters.Add(new EqualityFilter<string>(gallioTestElement.GallioTestId));
                }

                testExecutionOptions.FilterSet = new FilterSet<ITestDescriptor>(new IdFilter<ITestDescriptor>(new OrFilter<string>(idFilters)));

                RunWithProgressMonitor(delegate(IProgressMonitor progressMonitor)
                {
                    runner.Run(testPackage, testExplorationOptions, testExecutionOptions, progressMonitor);
                });
            }
            finally
            {
                runner.Dispose(NullProgressMonitor.CreateInstance());
            }
        }
        /// <inheritdoc />
        public void Initialize(TestRunnerOptions testRunnerOptions, ILogger logger, IProgressMonitor progressMonitor)
        {
            if (testRunnerOptions == null)
                throw new ArgumentNullException("testRunnerOptions");
            if (logger == null)
                throw new ArgumentNullException("logger");
            if (progressMonitor == null)
                throw new ArgumentNullException("progressMonitor");

            ThrowIfDisposed();
            if (state != State.Created)
                throw new InvalidOperationException("The test runner has already been initialized.");

            testRunnerOptions = testRunnerOptions.Copy();

            this.testRunnerOptions = testRunnerOptions;
            tappedLogger = new TappedLogger(this, logger);

            int extensionCount = extensions.Count;
            using (progressMonitor.BeginTask("Initializing the test runner.", 1 + extensionCount))
            {
                foreach (ITestRunnerExtension extension in extensions)
                {
                    string extensionName = extension.GetType().Name; // TODO: improve me

                    progressMonitor.SetStatus(String.Format("Installing extension '{0}'.", extensionName));
                    try
                    {
                        // Note: We don't pass the tapped logger to the extensions because the
                        //       extensions frequently write to the console a bunch of information we
                        //       already have represented in the report.  We are more interested in what
                        //       the test driver has to tell us.
                        extension.Install(eventDispatcher, logger);
                        progressMonitor.Worked(1);
                    }
                    catch (Exception ex)
                    {
                        throw new RunnerException(String.Format("Failed to install extension '{0}'.", extensionName), ex);
                    }
                    progressMonitor.SetStatus("");
                }

                try
                {
                    UnhandledExceptionPolicy.ReportUnhandledException += OnUnhandledException;

                    eventDispatcher.NotifyInitializeStarted(new InitializeStartedEventArgs(testRunnerOptions));

                    progressMonitor.SetStatus("Initializing the test isolation context.");

                    TestIsolationOptions testIsolationOptions = new TestIsolationOptions();
                    GenericCollectionUtils.ForEach(testRunnerOptions.Properties, x => testIsolationOptions.AddProperty(x.Key, x.Value));
                    testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, tappedLogger);

                    progressMonitor.Worked(1);
                }
                catch (Exception ex)
                {
                    eventDispatcher.NotifyInitializeFinished(new InitializeFinishedEventArgs(false));

                    UnhandledExceptionPolicy.ReportUnhandledException -= OnUnhandledException;

                    throw new RunnerException("A fatal exception occurred while initializing the test isolation context.", ex);
                }

                state = State.Initialized;
                eventDispatcher.NotifyInitializeFinished(new InitializeFinishedEventArgs(true));
            }
        }
        /// <summary>
        /// Creates a copy of the options.
        /// </summary>
        /// <returns>The copy.</returns>
        public TestRunnerOptions Copy()
        {
            TestRunnerOptions copy = new TestRunnerOptions();
            copy.properties.AddAll(properties);

            return copy;
        }
        /// <inheritdoc />
        public void Initialize(TestRunnerOptions testRunnerOptions, ILogger logger, IProgressMonitor progressMonitor)
        {
            if (testRunnerOptions == null)
            {
                throw new ArgumentNullException("testRunnerOptions");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }
            if (progressMonitor == null)
            {
                throw new ArgumentNullException("progressMonitor");
            }

            ThrowIfDisposed();
            if (state != State.Created)
            {
                throw new InvalidOperationException("The test runner has already been initialized.");
            }

            testRunnerOptions = testRunnerOptions.Copy();

            this.testRunnerOptions = testRunnerOptions;
            tappedLogger           = new TappedLogger(this, logger);

            int extensionCount = extensions.Count;

            using (progressMonitor.BeginTask("Initializing the test runner.", 1 + extensionCount))
            {
                foreach (ITestRunnerExtension extension in extensions)
                {
                    string extensionName = extension.GetType().Name; // TODO: improve me

                    progressMonitor.SetStatus(String.Format("Installing extension '{0}'.", extensionName));
                    try
                    {
                        // Note: We don't pass the tapped logger to the extensions because the
                        //       extensions frequently write to the console a bunch of information we
                        //       already have represented in the report.  We are more interested in what
                        //       the test driver has to tell us.
                        extension.Install(eventDispatcher, logger);
                        progressMonitor.Worked(1);
                    }
                    catch (Exception ex)
                    {
                        throw new RunnerException(String.Format("Failed to install extension '{0}'.", extensionName), ex);
                    }
                    progressMonitor.SetStatus("");
                }

                try
                {
                    UnhandledExceptionPolicy.ReportUnhandledException += OnUnhandledException;

                    eventDispatcher.NotifyInitializeStarted(new InitializeStartedEventArgs(testRunnerOptions));

                    progressMonitor.SetStatus("Initializing the test isolation context.");

                    TestIsolationOptions testIsolationOptions = new TestIsolationOptions();
                    GenericCollectionUtils.ForEach(testRunnerOptions.Properties, x => testIsolationOptions.AddProperty(x.Key, x.Value));
                    testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, tappedLogger);

                    progressMonitor.Worked(1);
                }
                catch (Exception ex)
                {
                    eventDispatcher.NotifyInitializeFinished(new InitializeFinishedEventArgs(false));

                    UnhandledExceptionPolicy.ReportUnhandledException -= OnUnhandledException;

                    throw new RunnerException("A fatal exception occurred while initializing the test isolation context.", ex);
                }

                state = State.Initialized;
                eventDispatcher.NotifyInitializeFinished(new InitializeFinishedEventArgs(true));
            }
        }
        private FacadeTaskResult RunTests()
        {
            var logger = new FacadeLoggerWrapper(facadeLogger);
            var runner = TestRunnerUtils.CreateTestRunnerByName(StandardTestRunnerFactoryNames.IsolatedAppDomain);

            // Set parameters.
            var testPackage = new TestPackage();
            foreach (var assemblyLocation in assemblyLocations)
                testPackage.AddFile(new FileInfo(assemblyLocation));

            testPackage.ShadowCopy = facadeTaskExecutorConfiguration.ShadowCopy;

            if (facadeTaskExecutorConfiguration.AssemblyFolder != null)
            {
                testPackage.ApplicationBaseDirectory = new DirectoryInfo(facadeTaskExecutorConfiguration.AssemblyFolder);
                testPackage.WorkingDirectory = new DirectoryInfo(facadeTaskExecutorConfiguration.AssemblyFolder);
            }

            var testRunnerOptions = new TestRunnerOptions();

            var testExplorationOptions = new TestExplorationOptions();

            var filters = GenericCollectionUtils.ConvertAllToArray<string, Filter<string>>(explicitTestIds, 
                testId => new EqualityFilter<string>(testId));
            var filterSet = new FilterSet<ITestDescriptor>(new IdFilter<ITestDescriptor>(new OrFilter<string>(filters)));
            var testExecutionOptions = new TestExecutionOptions { FilterSet = filterSet };

            // Install the listeners.
            runner.Events.TestStepStarted += TestStepStarted;
            runner.Events.TestStepFinished += TestStepFinished;
            runner.Events.TestStepLifecyclePhaseChanged += TestStepLifecyclePhaseChanged;

            // Run the tests.
            try
            {
                try
                {
                    runner.Initialize(testRunnerOptions, logger, CreateProgressMonitor());
                    Report report = runner.Run(testPackage, testExplorationOptions, testExecutionOptions, CreateProgressMonitor());

                    if (sessionId != null)
                        SessionCache.SaveSerializedReport(sessionId, report);

                    return FacadeTaskResult.Success;
                }
                catch (Exception ex)
                {
                    if (sessionId != null)
                        SessionCache.ClearSerializedReport(sessionId);

                    logger.Log(LogSeverity.Error, "A fatal exception occurred during test execution.", ex);
                    return FacadeTaskResult.Exception;
                }
                finally
                {
                    SubmitFailureForRemainingPendingTasks();
                }
            }
            finally
            {
                runner.Dispose(CreateProgressMonitor());
            }
        }