Exemple #1
0
        public NUnitResults Run(ITestEventListener listener, TestFilter filter)
        {
            var timing  = new TimingLogger(settings, logger);
            var results = new NUnitResults(Runner.Run(listener, filter));

            timing.LogTime("Execution engine run time ");
            return(results);
        }
Exemple #2
0
        public NUnitResults Explore(TestFilter filter)
        {
            var timing  = new TimingLogger(settings, logger);
            var results = new NUnitResults(Runner.Explore(filter));

            timing.LogTime("Execution engine discovery time ");
            return(results);
        }
Exemple #3
0
        /// <summary>
        /// Called by the VisualStudio IDE when selected tests are to be run. Never called from TFS Build, except (at least 2022, probably also 2019) when vstest.console uses /test: then this is being used.
        /// </summary>
        /// <param name="tests">The tests to be run.</param>
        /// <param name="runContext">The RunContext.</param>
        /// <param name="frameworkHandle">The FrameworkHandle.</param>
        public void RunTests(IEnumerable <TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
        {
            Initialize(runContext, frameworkHandle);
            CheckIfDebug();
            InitializeForExecution(runContext, frameworkHandle);
            RunType = RunType.Ide;
            TestLog.Debug("RunTests by IEnumerable<TestCase>. RunType = Ide");
            var timing = new TimingLogger(Settings, TestLog);

            Debug.Assert(NUnitEngineAdapter != null, "NUnitEngineAdapter is null");
            Debug.Assert(NUnitEngineAdapter.EngineEnabled, "NUnitEngineAdapter TestEngine is null");
            var assemblyGroups = tests.GroupBy(tc => tc.Source);

            if (IsInProcDataCollectorsSpecifiedWithMultipleAssemblies(assemblyGroups))
            {
                TestLog.Error(
                    "Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration.");
                Unload();
                return;
            }

            foreach (var assemblyGroup in assemblyGroups)
            {
                var assemblytiming = new TimingLogger(Settings, TestLog);
                try
                {
                    string assemblyName = assemblyGroup.Key;
                    string assemblyPath = Path.IsPathRooted(assemblyName)
                        ? assemblyName
                        : Path.Combine(Directory.GetCurrentDirectory(), assemblyName);

                    var filterBuilder = CreateTestFilterBuilder();
                    var filter        = filterBuilder.FilterByList(assemblyGroup);

                    RunAssembly(assemblyPath, assemblyGroup, filter);
                }
                catch (Exception ex)
                {
                    if (ex is TargetInvocationException)
                    {
                        ex = ex.InnerException;
                    }

                    TestLog.Warning("Exception thrown executing tests", ex);
                }

                assemblytiming.LogTime($"Executing {assemblyGroup.Key} time ");
            }

            timing.LogTime("Total execution time");
            TestLog.Info($"NUnit Adapter {AdapterVersion}: Test execution complete");
            Unload();
        }
Exemple #4
0
        public NUnitResults Run(ITestEventListener listener, TestFilter filter)
        {
            var timing  = new TimingLogger(settings, logger);
            var results = new NUnitResults(Runner.Run(listener, filter));

            timing.LogTime($"Execution engine run time with filter length {filter.Text.Length}");
            if (filter.Text.Length < 300)
            {
                logger.Debug($"Filter: {filter.Text}");
            }
            return(results);
        }
Exemple #5
0
        public NUnitResults Explore(TestFilter filter)
        {
            var timing  = new TimingLogger(settings, logger);
            var results = new NUnitResults(Runner.Explore(filter));

            timing.LogTime($"Execution engine discovery time with filter length {filter.Text.Length}");
            if (filter.Text.Length < 300)
            {
                logger.Debug($"Filter: {filter.Text}");
            }

            return(results);
        }
        public IList <TestCase> Convert(NUnitResults discoveryResults, string assemblyPath)
        {
            if (discoveryResults == null)
            {
                return(new List <TestCase>());
            }
            AssemblyPath = assemblyPath;
            var timing = new TimingLogger(Settings, TestLog);

            if (Settings.DiscoveryMethod != DiscoveryMethod.Legacy)
            {
                TestRun = ConvertXml(discoveryResults);
            }

            var nunitTestCases = discoveryResults.TestCases();

            // 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.

            if (Settings.DiscoveryMethod == DiscoveryMethod.Legacy)
            {
                converterForXml = new TestConverterForXml(TestLog, AssemblyPath, Settings);
                foreach (XmlNode testNode in nunitTestCases)
                {
                    loadedTestCases.Add(converterForXml.ConvertTestCase(new NUnitEventTestCase(testNode)));
                }
                TestLog.Info(
                    $"   NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Legacy discovery mode");
            }
            else
            {
                converter = new TestConverter(TestLog, AssemblyPath, Settings, this);
                var isExplicit = TestRun.IsExplicit;
                var testCases  = RunnableTestCases(isExplicit);
                foreach (var testNode in testCases)
                {
                    loadedTestCases.Add(converter.ConvertTestCase(testNode));
                }
                var msg = isExplicit ? "Explicit run" : "Non-Explicit run";
                TestLog.Info(
                    $"   NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Current Discovery mode, {msg}");
            }

            timing.LogTime("Converting test cases ");
            return(loadedTestCases);

            IEnumerable <NUnitDiscoveryTestCase> RunnableTestCases(bool isExplicit)
            {
                IEnumerable <NUnitDiscoveryTestCase> result;

                if (isExplicit || !Settings.DesignMode)
                {
                    result = TestRun.TestAssembly.AllTestCases;
                }
                else
                {
                    result = TestRun.TestAssembly.RunnableTestCases;
                }
                return(result);
            }
        }
        public void DiscoverTests(IEnumerable <string> sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink)
        {
            Initialize(discoveryContext, messageLogger);
            CheckIfDebug();
            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)
            {
                string sourceAssemblyPath = Path.IsPathRooted(sourceAssembly) ? sourceAssembly : Path.Combine(Directory.GetCurrentDirectory(), sourceAssembly);
                TestLog.Debug("Processing " + sourceAssembly);
                if (Settings.DumpXmlTestDiscovery)
                {
                    dumpXml = new DumpXml(sourceAssemblyPath);
                }

                try
                {
                    var package = CreateTestPackage(sourceAssemblyPath, null);
                    NUnitEngineAdapter.CreateRunner(package);
                    var results = NUnitEngineAdapter.Explore();
                    dumpXml?.AddString(results.AsString());

                    if (results.IsRunnable)
                    {
                        int cases;
                        using (var testConverter = new TestConverterForXml(TestLog, sourceAssemblyPath, Settings))
                        {
                            var timing = new TimingLogger(Settings, TestLog);
                            cases = ProcessTestCases(results, discoverySink, testConverter);
                            timing.LogTime("Discovery/Processing/Converting:");
                        }

                        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
                    {
                        if (results.HasNoNUnitTests)
                        {
                            if (Settings.Verbosity > 0)
                            {
                                TestLog.Info("Assembly contains no NUnit 3.0 tests: " + sourceAssembly);
                            }
                        }
                        else
                        {
                            TestLog.Info("NUnit failed to load " + sourceAssembly);
                        }
                    }
                }
                catch (NUnitEngineException e)
                {
                    if (e.InnerException is BadImageFormatException)
                    {
                        // we skip the native c++ binaries that we don't support.
                        TestLog.Warning("Assembly not supported: " + sourceAssembly);
                    }
                    else
                    {
                        TestLog.Warning("Exception thrown discovering tests in " + sourceAssembly, e);
                    }
                }
                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 occurred. Not a problem if not an NUnit assembly.
                    TestLog.Warning("Dependent Assembly " + ex.FileName + " of " + sourceAssembly + " not found. Can be ignored if not an 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
                {
                    dumpXml?.DumpForDiscovery();
                    NUnitEngineAdapter?.CloseRunner();
                }
            }

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

            Unload();
        }