Пример #1
0
        public void DiscoverTests(IEnumerable <string> sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink)
        {
            TestLog.Initialize(messageLogger);
            if (RegistryFailure)
            {
                TestLog.SendErrorMessage(ErrorMsg);
            }
            Info("discovering tests", "started");

            // Ensure any channels registered by other adapters are unregistered
            CleanUpRegisteredChannels();

            foreach (string sourceAssembly in sources)
            {
                TestLog.SendDebugMessage("Processing " + sourceAssembly);

                TestRunner    runner        = new TestDomain();
                var           package       = CreateTestPackage(sourceAssembly);
                TestConverter testConverter = null;
                try
                {
                    if (runner.Load(package))
                    {
                        testConverter = new TestConverter(TestLog, sourceAssembly);
                        int cases = ProcessTestCases(runner.Test, discoverySink, testConverter);
                        TestLog.SendDebugMessage(string.Format("Discovered {0} test cases", cases));
                    }
                    else
                    {
                        TestLog.NUnitLoadError(sourceAssembly);
                    }
                }
                catch (BadImageFormatException)
                {
                    // we skip the native c++ binaries that we don't support.
                    TestLog.AssemblyNotSupportedWarning(sourceAssembly);
                }

                catch (FileNotFoundException ex)
                {
                    // Probably from the GetExportedTypes in NUnit.core, attempting to find an assembly, not a problem if it is not NUnit here
                    TestLog.DependentAssemblyNotFoundWarning(ex.FileName, sourceAssembly);
                }
                catch (FileLoadException ex)
                {
                    // Attempts to load an invalid assembly, or an assembly with missing dependencies
                    TestLog.LoadingAssemblyFailedWarning(ex.FileName, sourceAssembly);
                }
                catch (UnsupportedFrameworkException ex)
                {
                    TestLog.UnsupportedFrameworkWarning(sourceAssembly);
                }
                catch (Exception ex)
                {
                    TestLog.SendErrorMessage("Exception thrown discovering tests in " + sourceAssembly, ex);
                }
                finally
                {
                    if (testConverter != null)
                    {
                        testConverter.Dispose();
                    }
                    runner.Unload();
                }
            }

            Info("discovering test", "finished");
        }
Пример #2
0
        private void RunAssembly(string assemblyName, IFrameworkHandle frameworkHandle)
        {
#if LAUNCHDEBUGGER
            if (!Debugger.IsAttached)
            {
                Debugger.Launch();
            }
#endif
            _testRunner = GetRunnerFor(assemblyName);

            try
            {
                var loadResult = _testRunner.Explore(TestFilter.Empty);

                if (loadResult.Name == "test-run")
                {
                    loadResult = loadResult.FirstChild;
                }

                if (loadResult.GetAttribute("runstate") == "Runnable")
                {
                    TestLog.SendInformationalMessage(string.Format("Loading tests from {0}", assemblyName));

                    var nunitTestCases = loadResult.SelectNodes("//test-case");

                    using (var testConverter = new TestConverter(TestLog, assemblyName))
                    {
                        var loadedTestCases = new List <TestCase>();

                        // As a side effect of calling TestConverter.ConvertTestCase,
                        // the converter's cache of all test cases is populated as well.
                        // All future calls to convert a test case may now use the cache.
                        foreach (XmlNode testNode in nunitTestCases)
                        {
                            loadedTestCases.Add(testConverter.ConvertTestCase(testNode));
                        }

                        // If we have a TFS Filter, convert it to an nunit filter
                        if (_tfsFilter != null && _tfsFilter.HasTfsFilterValue)
                        {
                            var filteredTestCases = _tfsFilter.CheckFilter(loadedTestCases);
                            var testCases         = filteredTestCases as TestCase[] ?? filteredTestCases.ToArray();
                            TestLog.SendInformationalMessage(string.Format("TFS Filter detected: LoadedTestCases {0}, Filterered Test Cases {1}", loadedTestCases.Count, testCases.Count()));
                            _nunitFilter = MakeTestFilter(testCases);
                        }

                        using (var listener = new NUnitEventListener(frameworkHandle, testConverter))
                        {
                            try
                            {
                                _testRunner.Run(listener, _nunitFilter);
                            }
                            catch (NullReferenceException)
                            {
                                // this happens during the run when CancelRun is called.
                                TestLog.SendDebugMessage("Nullref caught");
                            }
                        }
                    }
                }
                else
                {
                    TestLog.NUnitLoadError(assemblyName);
                }
            }
            catch (BadImageFormatException)
            {
                // we skip the native c++ binaries that we don't support.
                TestLog.AssemblyNotSupportedWarning(assemblyName);
            }
            catch (System.IO.FileNotFoundException ex)
            {
                // Probably from the GetExportedTypes in NUnit.core, attempting to find an assembly, not a problem if it is not NUnit here
                TestLog.DependentAssemblyNotFoundWarning(ex.FileName, assemblyName);
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ex.InnerException;
                }
                TestLog.SendErrorMessage("Exception thrown executing tests in " + assemblyName, ex);
            }
            _testRunner.Dispose();
        }
Пример #3
0
        public void DiscoverTests(IEnumerable <string> sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink)
        {
#if LAUNCHDEBUGGER
            if (!Debugger.IsAttached)
            {
                Debugger.Launch();
            }
#endif
            Initialize(messageLogger);

            Info("discovering tests", "started");

            // Ensure any channels registered by other adapters are unregistered
            CleanUpRegisteredChannels();

            foreach (string sourceAssembly in sources)
            {
                TestLog.SendDebugMessage("Processing " + sourceAssembly);

                ITestRunner runner = null;

                try
                {
                    runner = GetRunnerFor(sourceAssembly);
                    try
                    {
                        XmlNode loadResult = runner.Load();

                        // Currently, this will always be the case but it might change
                        if (loadResult.Name == "test-run")
                        {
                            loadResult = loadResult.FirstChild;
                        }

                        if (loadResult.GetAttribute("runstate") == "Runnable")
                        {
                            XmlNode topNode = runner.Explore(TestFilter.Empty);

                            using (var testConverter = new TestConverter(TestLog, sourceAssembly))
                            {
                                int cases = ProcessTestCases(topNode, discoverySink, testConverter);
                                TestLog.SendDebugMessage(string.Format("Discovered {0} test cases", cases));
                            }
                        }
                        else
                        {
                            var msgNode = loadResult.SelectSingleNode("properties/property[@name='_SKIPREASON']");
                            if (msgNode != null && msgNode.GetAttribute("value").Contains("contains no tests"))
                            {
                                TestLog.SendWarningMessage("Assembly contains no NUnit 3.0 tests: " + sourceAssembly);
                            }
                            else
                            {
                                TestLog.NUnitLoadError(sourceAssembly);
                            }
                        }
                    }
                    catch (BadImageFormatException)
                    {
                        // we skip the native c++ binaries that we don't support.
                        TestLog.AssemblyNotSupportedWarning(sourceAssembly);
                    }
                    catch (FileNotFoundException ex)
                    {
                        // Either the NUnit framework was not referenced by the test assembly
                        // or some other error occured. Not a problem if not an NUnit assembly.
                        TestLog.DependentAssemblyNotFoundWarning(ex.FileName, sourceAssembly);
                    }
                    catch (FileLoadException ex)
                    {
                        // Attempts to load an invalid assembly, or an assembly with missing dependencies
                        TestLog.LoadingAssemblyFailedWarning(ex.FileName, sourceAssembly);
                    }
                    catch (TypeLoadException ex)
                    {
                        if (ex.TypeName == "NUnit.Framework.Api.FrameworkController")
                        {
                            TestLog.SendWarningMessage("   Skipping NUnit 2.x test assembly");
                        }
                        else
                        {
                            TestLog.SendErrorMessage("Exception thrown discovering tests in " + sourceAssembly, ex);
                        }
                    }
                    catch (Exception ex)
                    {
                        TestLog.SendErrorMessage("Exception thrown discovering tests in " + sourceAssembly, ex);
                    }
                    finally
                    {
                        if (runner.IsTestRunning)
                        {
                            runner.StopRun(true);
                        }
                    }
                }
                finally
                {
                    if (runner != null)
                    {
                        runner.Unload();
                        runner.Dispose();
                    }
                }
            }

            Info("discovering test", "finished");
            Unload();
        }
        public void DiscoverTests(IEnumerable <string> sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink)
        {
            TestLog.Initialize(messageLogger);
            if (RegistryFailure)
            {
                TestLog.SendErrorMessage(ErrorMsg);
            }
            Info("discovering tests", "started");

            // Ensure any channels registered by other adapters are unregistered
            CleanUpRegisteredChannels();

            // var initPath = Path.Combine(Environment.CurrentDirectory, "init.tml");

            var initPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NUnitTestAdapter");

            initPath = Path.Combine(initPath, "init.tml");

            var blackList = new List <string>();

            if (File.Exists(initPath))
            {
                var config = Toml.ReadFile <Configuration>(initPath);
                blackList = config.Blacklist.Select(b => b.ToLowerInvariant()).ToList();
            }


            foreach (string sourceAssembly in sources)
            {
                if (blackList.Contains(Path.GetFileName(sourceAssembly.ToLowerInvariant())))
                {
                    TestLog.SendDebugMessage("Ignore " + sourceAssembly);
                    continue;
                }

                TestLog.SendDebugMessage("Processing " + sourceAssembly);

                //config.Blacklist.Add(sourceAssembly);

                TestRunner    runner        = new TestDomain();
                var           package       = CreateTestPackage(sourceAssembly);
                TestConverter testConverter = null;
                try
                {
                    if (runner.Load(package))
                    {
                        testConverter = new TestConverter(TestLog, sourceAssembly);
                        int cases = ProcessTestCases(runner.Test, discoverySink, testConverter);
                        TestLog.SendDebugMessage(string.Format("Discovered {0} test cases", cases));
                    }
                    else
                    {
                        TestLog.NUnitLoadError(sourceAssembly);
                    }
                }
                catch (BadImageFormatException)
                {
                    // we skip the native c++ binaries that we don't support.
                    TestLog.AssemblyNotSupportedWarning(sourceAssembly);
                }

                catch (FileNotFoundException ex)
                {
                    // Probably from the GetExportedTypes in NUnit.core, attempting to find an assembly, not a problem if it is not NUnit here
                    TestLog.DependentAssemblyNotFoundWarning(ex.FileName, sourceAssembly);
                }
                catch (FileLoadException ex)
                {
                    // Attempts to load an invalid assembly, or an assembly with missing dependencies
                    TestLog.LoadingAssemblyFailedWarning(ex.FileName, sourceAssembly);
                }
                catch (UnsupportedFrameworkException ex)
                {
                    TestLog.UnsupportedFrameworkWarning(sourceAssembly);
                }
                catch (Exception ex)
                {
                    TestLog.SendErrorMessage("Exception thrown discovering tests in " + sourceAssembly, ex);
                }
                finally
                {
                    if (testConverter != null)
                    {
                        testConverter.Dispose();
                    }
                    runner.Unload();
                }
            }

            //Toml.WriteFile(config, @"test.tml");

            Info("discovering test", "finished");
        }