/// <inheritdoc />
        public Report Run(TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IProgressMonitor progressMonitor)
        {
            if (testPackage == null)
            {
                throw new ArgumentNullException("testPackageConfig");
            }
            if (testExplorationOptions == null)
            {
                throw new ArgumentNullException("testExplorationOptions");
            }
            if (testExecutionOptions == null)
            {
                throw new ArgumentNullException("testExecutionOptions");
            }
            if (progressMonitor == null)
            {
                throw new ArgumentNullException("progressMonitor");
            }

            ThrowIfDisposed();
            if (state != State.Initialized)
            {
                throw new InvalidOperationException("The test runner must be initialized before this operation is performed.");
            }

            testPackage            = testPackage.Copy();
            testExplorationOptions = testExplorationOptions.Copy();
            testExecutionOptions   = testExecutionOptions.Copy();
            GenericCollectionUtils.ForEach(testRunnerOptions.Properties, x => testPackage.AddProperty(x.Key, x.Value));

            using (progressMonitor.BeginTask("Running the tests.", 10))
            {
                Stopwatch stopwatch = Stopwatch.StartNew();
                Report    report    = new Report()
                {
                    TestPackage    = new TestPackageData(testPackage),
                    TestModel      = new TestModelData(),
                    TestPackageRun = new TestPackageRun()
                    {
                        StartTime = DateTime.Now
                    }
                };
                var reportLockBox = new LockBox <Report>(report);

                eventDispatcher.NotifyRunStarted(new RunStartedEventArgs(testPackage, testExplorationOptions,
                                                                         testExecutionOptions, reportLockBox));

                bool success;
                using (Listener listener = new Listener(eventDispatcher, tappedLogger, reportLockBox))
                {
                    try
                    {
                        ITestDriver testDriver = testFrameworkManager.GetTestDriver(
                            testPackage.CreateTestFrameworkSelector(), tappedLogger);

                        using (testIsolationContext.BeginBatch(progressMonitor.SetStatus))
                        {
                            testDriver.Run(testIsolationContext, testPackage, testExplorationOptions,
                                           testExecutionOptions, listener, progressMonitor.CreateSubProgressMonitor(10));
                        }

                        success = true;
                    }
                    catch (Exception ex)
                    {
                        success = false;

                        tappedLogger.Log(LogSeverity.Error,
                                         "A fatal exception occurred while running tests.  Possible causes include invalid test runner parameters and stack overflows.",
                                         ex);
                        report.TestModel.Annotations.Add(new AnnotationData(AnnotationType.Error,
                                                                            CodeLocation.Unknown, CodeReference.Unknown,
                                                                            "A fatal exception occurred while running tests.  See log for details.", null));
                    }
                    finally
                    {
                        report.TestPackageRun.EndTime             = DateTime.Now;
                        report.TestPackageRun.Statistics.Duration = stopwatch.Elapsed.TotalSeconds;
                    }
                }

                eventDispatcher.NotifyRunFinished(new RunFinishedEventArgs(success, report));

                return(report);
            }
        }
Exemple #2
0
        public IEnumerable <AutoTest.TestRunners.Shared.Results.TestResult> Run(RunSettings settings)
        {
            if (!_isInitialized)
            {
                return new AutoTest.TestRunners.Shared.Results.TestResult[] { getNotInitializedResult(settings) }
            }
            ;
            var tests      = settings.Assembly.Tests.ToList();
            var members    = settings.Assembly.Members.ToList();
            var namespaces = settings.Assembly.Namespaces.ToList();

            var runAll      = namespaces.Count == 0 && members.Count == 0 && tests.Count == 0;
            var steps       = new List <TestStepData>();
            var testResults = new List <AutoTest.TestRunners.Shared.Results.TestResult>();

            // Get a test isolation context.  Here we want to run tests in the same AppDomain.
            var testIsolationProvider = (ITestIsolationProvider)RuntimeAccessor.ServiceLocator.ResolveByComponentId("Gallio.LocalTestIsolationProvider");
            var testIsolationOptions  = new TestIsolationOptions();
            ITestIsolationContext testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, _logger);

            var testPackage = new TestPackage();

            testPackage.AddFile(new FileInfo(settings.Assembly.Assembly));
            testPackage.TestFrameworkFallbackMode = TestFrameworkFallbackMode.Strict;

            // Create some test exploration options.  Nothing interesting for you here, probably.
            var testExplorationOptions = new TestExplorationOptions();
            var messageSink            = new MessageConsumer()
                                         .Handle <TestStepStartedMessage>((message) =>
            {
                steps.Add(message.Step);
            })
                                         .Handle <TestStepFinishedMessage>(message =>
            {
                var test = steps.FirstOrDefault(x => x.Id.Equals(message.StepId) && x.IsTestCase);
                if (test == null)
                {
                    return;
                }
                var fixture = string.Format("{0}.{1}", test.CodeReference.NamespaceName, steps.First(x => x.Id.Equals(test.ParentId)).Name);
                testResults.Add(new AutoTest.TestRunners.Shared.Results.TestResult(
                                    "MbUnit",
                                    settings.Assembly.Assembly,
                                    fixture,
                                    message.Result.Duration.TotalMilliseconds,
                                    string.Format("{0}.{1}", fixture, test.Name),
                                    convertState(message.Result.Outcome.Status),
                                    message.Result.Outcome.DisplayName));
            });

            // Provide a progress monitor.
            var logProgressMonitorProvider = new LogProgressMonitorProvider(_logger);
            var options = new TestExecutionOptions();

            options.FilterSet = new Gallio.Model.Filters.FilterSet <ITestDescriptor>(new OrFilter <ITestDescriptor>(getTestFilter(namespaces, members, tests)));

            // Run the tests.
            logProgressMonitorProvider.Run((progressMonitor) =>
            {
                _testDriver.Run(testIsolationContext, testPackage, testExplorationOptions, options, messageSink, progressMonitor);
            });

            return(testResults);
        }