TestModel PopulateTestTree(Assembly assembly)
    {
      TestModel testModel = new TestModel();

      var testFrameworkManager = RuntimeAccessor.ServiceLocator.Resolve<ITestFrameworkManager>();
      var logger = new MarkupStreamLogger(TestLog.Default);

      var testFrameworkSelector = new TestFrameworkSelector()
      {
        Filter = testFrameworkHandle => testFrameworkHandle.Id == TestFrameworkHandle.Id,
        FallbackMode = TestFrameworkFallbackMode.Strict
      };

      ITestDriver testDriver = testFrameworkManager.GetTestDriver(testFrameworkSelector, logger);

      var testIsolationProvider = (ITestIsolationProvider)RuntimeAccessor.ServiceLocator.ResolveByComponentId("Gallio.LocalTestIsolationProvider");
      var testIsolationOptions = new TestIsolationOptions();
      using (ITestIsolationContext testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, logger))
      {
        var testPackage = new TestPackage();
        testPackage.AddFile(new FileInfo(AssemblyUtils.GetFriendlyAssemblyCodeBase(assembly)));
        var testExplorationOptions = new TestExplorationOptions();

        var messageSink = TestModelSerializer.CreateMessageSinkToPopulateTestModel(testModel);

        new LogProgressMonitorProvider(logger).Run(progressMonitor =>
        {
          testDriver.Explore(testIsolationContext, testPackage, testExplorationOptions,
            messageSink, progressMonitor);
        });
      }

      return testModel;
    }
Пример #2
0
        /// <summary>
        /// Creates a message sink that populates the test model as test and annotation
        /// discovered messages are published.
        /// </summary>
        /// <param name="testModel">The test model to populate.</param>
        /// <returns>The message sink.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="testModel"/> is null.</exception>
        public static IMessageSink CreateMessageSinkToPopulateTestModel(TestModel testModel)
        {
            if (testModel == null)
                throw new ArgumentNullException("testModel");

            return new MessageConsumer()
                .Handle<TestDiscoveredMessage>(message =>
                {
                    Test test = message.Test.ToTest();

                    if (message.ParentTestId != null)
                    {
                        Test parentTest = testModel.FindTest(message.ParentTestId);
                        if (parentTest == null)
                            throw new InvalidOperationException("The parent test is missing.");

                        parentTest.AddChild(test);
                    }
                    else
                    {
                        testModel.RootTest = test;
                    }
                })
                .Handle<AnnotationDiscoveredMessage>(message =>
                {
                    testModel.AddAnnotation(message.Annotation.ToAnnotation());
                });
        }
        public NUnitReflectiveTestExplorerEngine(TestModel testModel, IAssemblyInfo assembly)
        {
            this.testModel = testModel;
            this.assembly = assembly;

            nunitFixtures = new List<NUnit.Core.TestSuite>();
        }
Пример #4
0
        /// <summary>
        /// Copies the contents of a test model.
        /// </summary>
        /// <param name="source">The source test model.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="source"/> is null.</exception>
        public TestModelData(TestModel source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            rootTest = new TestData(source.RootTest);

            annotations = new List<AnnotationData>();
            foreach (Annotation annotation in source.Annotations)
                annotations.Add(new AnnotationData(annotation));
        }
        /// <inheritdoc />
        public ITestCommand BuildCommands(TestModel testModel, FilterSet<ITestDescriptor> filterSet, bool exactFilter, ITestContextManager contextManager)
        {
            if (testModel == null)
                throw new ArgumentNullException("testModel");
            if (filterSet == null)
                throw new ArgumentNullException("filterSet");
            if (contextManager == null)
                throw new ArgumentNullException("contextManager");

            var commands = new Dictionary<Test, ManagedTestCommand>();
            bool hasExplicitAncestor = ! filterSet.HasInclusionRules;
            ManagedTestCommand rootCommand = CreateFilteredClosure(commands, testModel.RootTest, filterSet, exactFilter,
                hasExplicitAncestor, contextManager);
            if (rootCommand == null)
                return null;

            var siblingDependencies = new MultiMap<ManagedTestCommand, ManagedTestCommand>();
            PopulateCommandDependencies(commands, siblingDependencies);

            SortChildren(rootCommand, siblingDependencies);
            return rootCommand;
        }
Пример #6
0
        /// <summary>
        /// Publishes the contents of the test model to a message sink as test
        /// and annotation discovered messages.
        /// </summary>
        /// <param name="testModel">The test model to publish.</param>
        /// <param name="messageSink">The message sink.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="testModel"/>
        /// or <paramref name="messageSink"/> is null.</exception>
        public static void PublishTestModel(TestModel testModel, IMessageSink messageSink)
        {
            if (testModel == null)
                throw new ArgumentNullException("testModel");
            if (messageSink == null)
                throw new ArgumentNullException("messageSink");

            foreach (Annotation annotation in testModel.Annotations)
            {
                messageSink.Publish(new AnnotationDiscoveredMessage()
                {
                    Annotation = new AnnotationData(annotation)
                });
            }

            foreach (Test test in testModel.AllTests)
            {
                messageSink.Publish(new TestDiscoveredMessage()
                {
                    Test = new TestData(test, true),
                    ParentTestId = test.Parent != null ? test.Parent.Id : null
                });
            }
        }
Пример #7
0
        /// <inheritdoc />
        protected sealed override object RunImpl(object[] args)
        {
            TestPackage = (TestPackage)args[0];
            TestExplorationOptions = (TestExplorationOptions)args[1];
            TestExecutionOptions = (TestExecutionOptions)args[2];
            MessageSink = (IMessageSink)args[3];
            var progressMonitor = (IProgressMonitor)args[4];
            Logger = (ILogger)args[5];
            var fileInfo = (FileInfo)args[6];
            TestModel = new TestModel();

            using (var subProgressMonitor = progressMonitor.CreateSubProgressMonitor(1))
            {
                if (!subProgressMonitor.IsCanceled)
                {
                    using (var repository = new UnmanagedTestRepository(fileInfo.FullName))
                    {
                        if (repository.IsValid)
                        {
                            Execute(repository, subProgressMonitor); 
                        }
                    }
                }
            }

            return null;
        }
Пример #8
0
        private List<Test> getTests(string assembly)
        {
            var testIsolationProvider = (ITestIsolationProvider)RuntimeAccessor.ServiceLocator.ResolveByComponentId("Gallio.LocalTestIsolationProvider");
            var testIsolationOptions = new TestIsolationOptions();
            ITestIsolationContext testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, _logger);

            // Create a test package.
            // You can set a whole bunch of options here.
            var testPackage = new TestPackage();
            testPackage.AddFile(new FileInfo(assembly));
            testPackage.TestFrameworkFallbackMode = TestFrameworkFallbackMode.Strict;

            var testExplorationOptions = new TestExplorationOptions();

            // This query you can answer by exploring tests and looking at their metadata for "TestsOn".
            // You can explore tests using the Explore method of TestDriver. It sends a bunch of
            // messages to an IMessageSink just like Run.  Just scan these messages for tests that
            // match the pattern you like.

            // Alternately, you can build a TestModel once up front and traverse the resulting test tree
            // to find what you need.  The Explore method of testDriver is just like Run, except that
            // it does not run the tests.
            // TestModelSerializer is a little helper we can use to build up a TestModel from test messages on the fly.
            // The TestModel is just a tree of test metadata.  Pretty straightforward.
            TestModel testModel = new TestModel();
            IMessageSink messageSink = TestModelSerializer.CreateMessageSinkToPopulateTestModel(testModel);

            var logProgressMonitorProvider = new LogProgressMonitorProvider(_logger);

            logProgressMonitorProvider.Run((progressMonitor) =>
            {
                _testDriver.Explore(testIsolationContext, testPackage, testExplorationOptions, messageSink, progressMonitor);
            });

            return testModel.AllTests.Where(x => x.IsTestCase).ToList();
        }
 public MbUnit2NativeTestExplorerEngine(TestModel testModel, Assembly assembly)
 {
     this.testModel = testModel;
     this.assembly = assembly;
 }
Пример #10
0
        private ITestCommand GenerateCommandTree(TestModel testModel, TestExecutionOptions testExecutionOptions, ITestContextManager testContextManager)
        {
            ITestCommandFactory testCommandFactory = CreateTestCommandFactory();

            ITestCommand rootCommand = testCommandFactory.BuildCommands(testModel, testExecutionOptions.FilterSet,
                testExecutionOptions.ExactFilter, testContextManager);
            return rootCommand;
        }
Пример #11
0
        private void RunTestModel(TestModel testModel, IMessageSink messageSink, IProgressMonitor progressMonitor, double totalWork)
        {
            double workUnitsPerStep = totalWork / (1 + testModel.RootTest.Children.Count);
            TestStep rootTestStep = new TestStep(testModel.RootTest, null);

            messageSink.Publish(new TestStepStartedMessage()
            {
                Step = new TestStepData(rootTestStep)
            });

            foreach (Test test in testModel.RootTest.Children)
            {
                if (progressMonitor.IsCanceled)
                    return;

                TestStep testStep = new TestStep(test, rootTestStep);

                messageSink.Publish(new TestStepStartedMessage()
                {
                    Step = new TestStepData(testStep)
                });

                messageSink.Publish(new TestStepLogStreamWriteMessage()
                {
                    StepId = testStep.Id,
                    StreamName = MarkupStreamNames.Warnings,
                    Text = FallbackExplanation
                });

                messageSink.Publish(new TestStepFinishedMessage()
                {
                    StepId = testStep.Id,
                    Result = new TestResult(TestOutcome.Ignored)
                });

                progressMonitor.Worked(workUnitsPerStep);
            }

            messageSink.Publish(new TestStepFinishedMessage()
            {
                StepId = rootTestStep.Id,
                Result = new TestResult(TestOutcome.Skipped)
            });

            progressMonitor.Worked(workUnitsPerStep);
        }
Пример #12
0
        private TestModel PublishTestModelFromFiles(IEnumerable<FileInfo> files, IMessageSink messageSink, IProgressMonitor progressMonitor)
        {
            TestModel testModel = new TestModel();

            foreach (FileInfo file in files)
            {
                if (progressMonitor.IsCanceled)
                    return null;

                messageSink.Publish(new AnnotationDiscoveredMessage()
                {
                    Annotation = new AnnotationData(AnnotationType.Warning,
                        new CodeLocation(file.FullName, 0, 0),
                        CodeReference.Unknown,
                        string.Format("File '{0}' is not supported by any installed test framework.  It will be ignored.", file.Name), null)
                });

                Test fileTest = CreateTest(file.Name, null);
                fileTest.Metadata.Add(MetadataKeys.File, file.FullName);
                testModel.RootTest.AddChild(fileTest);

                progressMonitor.Worked(1);
            }

            TestModelSerializer.PublishTestModel(testModel, messageSink);
            return testModel;
        }
Пример #13
0
        private TestModel PublishTestModelFromCodeElements(IEnumerable<ICodeElementInfo> codeElements, IMessageSink messageSink, IProgressMonitor progressMonitor)
        {
            TestModel testModel = new TestModel();
            var tests = new Dictionary<ICodeElementInfo, Test>();

            foreach (ICodeElementInfo codeElement in codeElements)
            {
                if (progressMonitor.IsCanceled)
                    return null;

                testModel.AddAnnotation(new Annotation(AnnotationType.Warning, codeElement, FallbackExplanation));

                IAssemblyInfo assembly = ReflectionUtils.GetAssembly(codeElement);
                if (assembly != null)
                {
                    Test assemblyTest;
                    if (!tests.TryGetValue(assembly, out assemblyTest))
                    {
                        assemblyTest = CreateTest(Path.GetFileName(assembly.Path), assembly);
                        testModel.RootTest.AddChild(assemblyTest);
                        tests.Add(assembly, assemblyTest);

                        ITypeInfo type = ReflectionUtils.GetType(codeElement);
                        if (type != null)
                        {
                            Test typeTest;
                            if (!tests.TryGetValue(type, out typeTest))
                            {
                                typeTest = CreateTest(type.Name, type);
                                assemblyTest.AddChild(typeTest);
                                tests.Add(type, typeTest);
                            }
                        }
                    }
                }

                progressMonitor.Worked(1);
            }

            TestModelSerializer.PublishTestModel(testModel, messageSink);
            return testModel;
        }
Пример #14
0
        /// <summary>
        /// Called automatically when isolation task is ready to run.
        /// </summary>
        protected override object RunImpl(object[] args)
        {
            file_ = (FileInfo)args[0];
            architecture_ = (string)args[1];
            configuration_ = (string)args[2];
            testExecutionOptions_ = (TestExecutionOptions)args[3];
            logger_ = (ILogger)args[4];
            progressMonitor_ = (IProgressMonitor)args[5];

            using (QueuedMessageSink sink = new QueuedMessageSink((IMessageSink)args[6]))
            {
                messageSink_ = sink;
                testModel_ = new TestModel();
                currentTest_ = testModel_.RootTest;
                currentTestSuite_ = testModel_.RootTest;

                using (progressMonitor_.BeginTask("Processing " + file_.Name, 100))
                {
                    // Expect native DLL to be reachable in subdirectory relative to the current assembly path.
                    string assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    string suffix = Path.Combine(architecture_, configuration_);

                    // Make sure we can find the right version of Boost.Test DLL.
                    if (!SetDllDirectory(Path.Combine(assemblyDir, suffix)))
                    {
                        Logger.Log(
                            LogSeverity.Error,
                            String.Format(
                                "Failed to adjust DLL directory search path: {0}",
                                new Win32Exception(Marshal.GetLastWin32Error()).Message
                            )
                        );
                        return null;
                    }

                    // Try loading native bridge DLL.
                    IntPtr hModule = LoadLibrary(Path.Combine(assemblyDir, Path.Combine(suffix, NativeDllName)));

                    if (hModule == IntPtr.Zero)
                    {
                        Logger.Log(
                            LogSeverity.Error,
                            String.Format(
                                "Failed to load native DLL to communicate with Boost.Test: {0}",
                                new Win32Exception(Marshal.GetLastWin32Error()).Message
                            )
                        );
                        return null;
                    }

                    try
                    {
                        // Make sure we allow loading additional DLLs
                        // from the same folder our testable DLL is located in.
                        if (!SetDllDirectory(File.DirectoryName))
                        {
                            Logger.Log(
                                LogSeverity.Error,
                                String.Format(
                                    "Failed to adjust DLL directory search path: {0}",
                                    new Win32Exception(Marshal.GetLastWin32Error()).Message
                                )
                            );
                            return null;
                        }

                        progressMonitor_.Worked(14);

                        // Retrieve pointer to function in native bridge DLL that is required to
                        // perform our task.
                        IntPtr bridgeFunc = GetProcAddress(hModule, BridgeFunctionName);

                        if (bridgeFunc == IntPtr.Zero)
                        {
                            Logger.Log(
                                LogSeverity.Error,
                                String.Format(
                                    "Failed to retrieve entry point {0} in Boost.Test interface: {1}",
                                    BridgeFunctionName,
                                    new Win32Exception(Marshal.GetLastWin32Error()).Message
                                )
                            );
                            return null;
                        }

                        progressMonitor_.Worked(1);

                        // Perform the task.
                        Execute(bridgeFunc, progressMonitor_.CreateSubProgressMonitor(80));
                    }
                    finally
                    {
                        FreeLibrary(hModule);
                        progressMonitor_.Worked(5);
                    }
                }
            }

            return null;
        }