Esempio n. 1
0
        public void ThatPathIsCorrectlyParsedInDiscoveryPhase(string path, string expected)
        {
            var file = Substitute.For <IFile>();
            var sut  = new DumpXml(path, file);

            sut.AddString("whatever");
            sut.DumpForDiscovery();
            file.Received().WriteAllText(Arg.Is <string>(o => o.StartsWith(expected, StringComparison.OrdinalIgnoreCase)), Arg.Any <string>());
        }
Esempio n. 2
0
        public void ThatWritingClearsBuffer()
        {
            string res  = "";
            var    file = Substitute.For <IFile>();

            file.WriteAllText(Arg.Any <string>(), Arg.Do <string>(o => res = o));
            var sut     = new DumpXml("whatever", file);
            var string1 = "something";

            sut.AddString(string1);
            sut.DumpForDiscovery();
            Assert.That(res, Does.Contain(string1));
            var string2 = "new string";

            sut.AddString(string2);
            sut.DumpForDiscovery();
            Assert.That(res, Does.Contain(string2));
            Assert.That(res, Does.Not.Contain(string1));
        }
Esempio n. 3
0
        public void ThatEmptyContainsHeaders()
        {
            string res  = "";
            var    file = Substitute.For <IFile>();

            file.WriteAllText(Arg.Any <string>(), Arg.Do <string>(o => res = o));
            var sut = new DumpXml("Whatever", file);

            sut.DumpForDiscovery();
            Assert.That(res, Does.Contain("NUnitXml"));
            var sarray = res.Split('\n');

            Assert.That(sarray.Length, Is.GreaterThanOrEqualTo(3));
        }
        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();
        }