Пример #1
0
        public static IDumpXml CreateDump(string path, IGrouping <string, TestCase> testCases, IAdapterSettings settings)
        {
            if (!settings.DumpXmlTestResults)
            {
                return(null);
            }
            var    executionDumpXml = new DumpXml(path);
            string runningBy        = testCases == null
                ? "<RunningBy>Sources</RunningBy>"
                : "<RunningBy>TestCases</RunningBy>";

            executionDumpXml.AddString($"\n{runningBy}\n");
            return(executionDumpXml);
        }
Пример #2
0
        public void DiscoverTests(IEnumerable <string> sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink)
        {
#if LAUNCHDEBUGGER
            if (!Debugger.IsAttached)
            {
                Debugger.Launch();
            }
#endif
            Initialize(discoveryContext, messageLogger);



            TestLog.Info($"NUnit Adapter {AdapterVersion}: Test discovery starting");

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

            if (Settings.InProcDataCollectorsAvailable && sources.Count() > 1)
            {
                TestLog.Error("Unexpected to discover tests in multiple assemblies when InProcDataCollectors specified in run configuration.");
                Unload();
                return;
            }

            foreach (string sourceAssembly in sources)
            {
                var sourceAssemblyPath = Path.IsPathRooted(sourceAssembly) ? sourceAssembly : Path.Combine(Directory.GetCurrentDirectory(), sourceAssembly);
                TestLog.Debug("Processing " + sourceAssembly);
                ITestRunner runner = null;

                if (Settings.DumpXmlTestDiscovery)
                {
                    dumpXml = new Dump.DumpXml(sourceAssemblyPath);
                }

                try
                {
                    runner = GetRunnerFor(sourceAssemblyPath);

                    XmlNode topNode = runner.Explore(TestFilter.Empty);
#if !NETCOREAPP1_0
                    dumpXml?.AddString(topNode.AsString());
#endif
                    // Currently, this will always be the case but it might change
                    if (topNode.Name == "test-run")
                    {
                        topNode = topNode.FirstChild;
                    }

                    if (topNode.GetAttribute("runstate") == "Runnable")
                    {
                        var testConverter = new TestConverter(TestLog, sourceAssemblyPath, Settings.CollectSourceInformation);

                        int cases = ProcessTestCases(topNode, discoverySink, testConverter);

                        TestLog.Debug($"Discovered {cases} test cases");
                        // Only save if seed is not specified in runsettings
                        // This allows workaround in case there is no valid
                        // location in which the seed may be saved.
                        if (cases > 0 && !Settings.RandomSeedSpecified)
                        {
                            Settings.SaveRandomSeed(Path.GetDirectoryName(sourceAssemblyPath));
                        }
                    }
                    else
                    {
                        var msgNode = topNode.SelectSingleNode("properties/property[@name='_SKIPREASON']");
                        if (msgNode != null && (new[] { "contains no tests", "Has no TestFixtures" }).Any(msgNode.GetAttribute("value").Contains))
                        {
                            TestLog.Info("Assembly contains no NUnit 3.0 tests: " + sourceAssembly);
                        }
                        else
                        {
                            TestLog.Info("NUnit failed to load " + sourceAssembly);
                        }
                    }
                    dumpXml?.Dump4Discovery();
                }
                catch (BadImageFormatException)
                {
                    // we skip the native c++ binaries that we don't support.
                    TestLog.Warning("Assembly not supported: " + 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.Warning("Dependent Assembly " + ex.FileName + " of " + sourceAssembly + " not found. Can be ignored if not a NUnit project.");
                }
                catch (FileLoadException ex)
                {
                    // Attempts to load an invalid assembly, or an assembly with missing dependencies
                    TestLog.Warning("Assembly " + ex.FileName + " loaded through " + sourceAssembly + " failed. Assembly is ignored. Correct deployment of dependencies if this is an error.");
                }
                catch (TypeLoadException ex)
                {
                    if (ex.TypeName == "NUnit.Framework.Api.FrameworkController")
                    {
                        TestLog.Warning("   Skipping NUnit 2.x test assembly");
                    }
                    else
                    {
                        TestLog.Warning("Exception thrown discovering tests in " + sourceAssembly, ex);
                    }
                }
                catch (Exception ex)
                {
                    TestLog.Warning("Exception thrown discovering tests in " + sourceAssembly, ex);
                }
                finally
                {
                    if (runner != null)
                    {
                        if (runner.IsTestRunning)
                        {
                            runner.StopRun(true);
                        }

                        runner.Unload();
                        runner.Dispose();
                    }
                }
            }

            TestLog.Info($"NUnit Adapter {AdapterVersion}: Test discovery complete");

            Unload();
        }
Пример #3
0
        private void RunAssembly(string assemblyPath, TestFilter filter)
        {
#if LAUNCHDEBUGGER
            if (!Debugger.IsAttached)
            {
                Debugger.Launch();
            }
#endif

            var actionText    = Debugger.IsAttached ? "Debugging " : "Running ";
            var selectionText = filter == null || filter == TestFilter.Empty ? "all" : "selected";
            TestLog.Info(actionText + selectionText + " tests in " + assemblyPath);

            // No need to restore if the seed was in runsettings file
            if (!Settings.RandomSeedSpecified)
            {
                Settings.RestoreRandomSeed(Path.GetDirectoryName(assemblyPath));
            }
            DumpXml dumpXml = null;
            if (Settings.DumpXmlTestResults)
            {
                dumpXml = new Dump.DumpXml(assemblyPath);
            }

            try
            {
                _activeRunner = GetRunnerFor(assemblyPath);

                var loadResult = _activeRunner.Explore(TestFilter.Empty);
#if !NETCOREAPP1_0
                dumpXml?.AddString(loadResult.AsString());
#endif
                if (loadResult.Name == "test-run")
                {
                    loadResult = loadResult.FirstChild;
                }

                if (loadResult.GetAttribute("runstate") == "Runnable")
                {
                    var nunitTestCases = loadResult.SelectNodes("//test-case");

                    using (var testConverter = new TestConverter(TestLog, assemblyPath, Settings))
                    {
                        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));
                        }


                        TestLog.Info($"   NUnit3TestExecutor converted {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases");

                        // If we have a TFS Filter, convert it to an nunit filter
                        if (TfsFilter != null && !TfsFilter.IsEmpty)
                        {
                            // NOTE This overwrites filter used in call
                            var filterBuilder = CreateTestFilterBuilder();
                            filter = filterBuilder.ConvertTfsFilterToNUnitFilter(TfsFilter, loadedTestCases);
                        }

                        if (filter == NUnitTestFilterBuilder.NoTestsFound)
                        {
                            TestLog.Info("   Skipping assembly - no matching test cases found");
                            return;
                        }

                        using (var listener = new NUnitEventListener(FrameworkHandle, testConverter, dumpXml))
                        {
                            try
                            {
                                var results = _activeRunner.Run(listener, filter);
                                GenerateTestOutput(results, assemblyPath);
                            }
                            catch (NullReferenceException)
                            {
                                // this happens during the run when CancelRun is called.
                                TestLog.Debug("   Nullref caught");
                            }
                        }
                    }
                }
                else
                {
                    var msgNode = loadResult.SelectSingleNode("properties/property[@name='_SKIPREASON']");
                    if (msgNode != null && (new[] { "contains no tests", "Has no TestFixtures" }).Any(msgNode.GetAttribute("value").Contains))
                    {
                        TestLog.Info("   NUnit couldn't find any tests in " + assemblyPath);
                    }
                    else
                    {
                        TestLog.Info("   NUnit failed to load " + assemblyPath);
                    }
                }
            }
            catch (BadImageFormatException)
            {
                // we skip the native c++ binaries that we don't support.
                TestLog.Warning("   Assembly not supported: " + assemblyPath);
            }
            catch (NUnitEngineException e)
            {
                if (e.InnerException is BadImageFormatException)
                {
                    // we skip the native c++ binaries that we don't support.
                    TestLog.Warning("   Assembly not supported: " + assemblyPath);
                }
                throw;
            }
            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.Warning("   Dependent Assembly " + ex.FileName + " of " + assemblyPath + " not found. Can be ignored if not a NUnit project.");
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ex.InnerException;
                }
                TestLog.Warning("   Exception thrown executing tests in " + assemblyPath, ex);
            }
            finally
            {
#if !NETCOREAPP1_0
                dumpXml?.Dump4Execution();
#endif
                try
                {
                    _activeRunner?.Dispose();
                    _activeRunner = null;
                }
                catch (Exception ex)
                {
                    // can happen if CLR throws CannotUnloadAppDomainException, for example
                    // due to a long-lasting operation in a protected region (catch/finally clause).
                    if (ex is TargetInvocationException)
                    {
                        ex = ex.InnerException;
                    }
                    TestLog.Warning("   Exception thrown unloading tests from " + assemblyPath, ex);
                }
            }
        }