// The Adapter is constructed using the default constructor.
        // We don't have any info to initialize it until one of the
        // ITestDiscovery or ITestExecutor methods is called. The
        // each Discover or Execute method must call this method.
        protected virtual void Initialize(IMessageLogger messageLogger)
        {
            Verbosity      = 0; // In case we throw below
            AdapterVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            try
            {
                var registry = RegistryCurrentUser.OpenRegistryCurrentUser(@"Software\nunit.org\VSAdapter");
                UseVsKeepEngineRunning = registry.Exist("UseVsKeepEngineRunning") && (registry.Read <int>("UseVsKeepEngineRunning") == 1);
                ShadowCopy             = registry.Exist("ShadowCopy") && (registry.Read <int>("ShadowCopy") == 1);
#if DEBUG && VERBOSE
                Verbosity = 1;
#else
                Verbosity = (registry.Exist("Verbosity")) ? registry.Read <int>("Verbosity") : 0;
#endif
            }
            catch (Exception e)
            {
                messageLogger.SendMessage(TestMessageLevel.Error, "Unable to access registry. Default settings will be used");
                messageLogger.SendMessage(TestMessageLevel.Error, e.ToString());
            }

            TestEngine = new TestEngine();
            TestLog    = new TestLogger(messageLogger, Verbosity);
        }
Пример #2
0
        /// <summary>
        /// The common constructor initializes NUnit services
        /// needed to load and run tests and sets some properties.
        /// </summary>
        protected NUnitTestAdapter()
        {
            ServiceManager.Services.AddService(new DomainManager());
            ServiceManager.Services.AddService(new ProjectService());

            ServiceManager.Services.InitializeServices();
            Verbosity       = 0;
            RegistryFailure = false;
            adapterVersion  = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            try
            {
                var registry = RegistryCurrentUser.OpenRegistryCurrentUser(@"Software\nunit.org\VSAdapter");
                UseVsKeepEngineRunning = registry.Exist("UseVsKeepEngineRunning") && (registry.Read <int>("UseVsKeepEngineRunning") == 1);
                ShadowCopy             = registry.Exist("ShadowCopy") && (registry.Read <int>("ShadowCopy") == 1);
                Verbosity = (registry.Exist("Verbosity")) ? registry.Read <int>("Verbosity") : 0;
            }
            catch (Exception e)
            {
                RegistryFailure = true;
                ErrorMsg        = e.ToString();
            }

            TestLog = new TestLogger(Verbosity);
        }
Пример #3
0
        public void Load(string settingsXml)
        {
            if (string.IsNullOrEmpty(settingsXml))
            {
                settingsXml = "<RunSettings />";
            }

            // Visual Studio already gives a good error message if the .runsettings
            // file is poorly formed, so we don't need to do anything more.
            var doc = new XmlDocument();

            doc.LoadXml(settingsXml);

            var nunitNode = doc.SelectSingleNode("RunSettings/NUnit");

            Verbosity         = GetInnerTextAsInt(nunitNode, nameof(Verbosity), 0);
            _logger.Verbosity = Verbosity;

            var runConfiguration = doc.SelectSingleNode("RunSettings/RunConfiguration");

            MaxCpuCount              = GetInnerTextAsInt(runConfiguration, nameof(MaxCpuCount), -1);
            ResultsDirectory         = GetInnerTextWithLog(runConfiguration, nameof(ResultsDirectory));
            TargetPlatform           = GetInnerTextWithLog(runConfiguration, nameof(TargetPlatform));
            TargetFrameworkVersion   = GetInnerTextWithLog(runConfiguration, nameof(TargetFrameworkVersion));
            TestAdapterPaths         = GetInnerTextWithLog(runConfiguration, nameof(TestAdapterPaths));
            CollectSourceInformation = GetInnerTextAsBool(runConfiguration, nameof(CollectSourceInformation), true);
            DisableAppDomain         = GetInnerTextAsBool(runConfiguration, nameof(DisableAppDomain), false);
            DisableParallelization   = GetInnerTextAsBool(runConfiguration, nameof(DisableParallelization), false);
            DesignMode = GetInnerTextAsBool(runConfiguration, nameof(DesignMode), false);
            CollectDataForEachTestSeparately =
                GetInnerTextAsBool(runConfiguration, nameof(CollectDataForEachTestSeparately), false);

            TestProperties = new Dictionary <string, string>();
            foreach (XmlNode node in doc.SelectNodes("RunSettings/TestRunParameters/Parameter"))
            {
                var key   = node.GetAttribute("name");
                var value = node.GetAttribute("value");
                if (key != null && value != null)
                {
                    TestProperties.Add(key, value);
                }
            }

            // NUnit settings
            InternalTraceLevel = GetInnerTextWithLog(nunitNode, nameof(InternalTraceLevel), "Off", "Error", "Warning",
                                                     "Info", "Verbose", "Debug");
            WorkDirectory          = GetInnerTextWithLog(nunitNode, nameof(WorkDirectory));
            DefaultTimeout         = GetInnerTextAsInt(nunitNode, nameof(DefaultTimeout), 0);
            NumberOfTestWorkers    = GetInnerTextAsInt(nunitNode, nameof(NumberOfTestWorkers), -1);
            ShadowCopyFiles        = GetInnerTextAsBool(nunitNode, nameof(ShadowCopyFiles), false);
            UseVsKeepEngineRunning = GetInnerTextAsBool(nunitNode, nameof(UseVsKeepEngineRunning), false);
            BasePath       = GetInnerTextWithLog(nunitNode, nameof(BasePath));
            PrivateBinPath = GetInnerTextWithLog(nunitNode, nameof(PrivateBinPath));
            var testOutput = GetInnerTextWithLog(nunitNode, nameof(TestOutputXml));

            if (!string.IsNullOrEmpty(testOutput))
            {
                TestOutputXml = ValidatedPath(testOutput, nameof(TestOutputXml));
            }
            RandomSeed          = GetInnerTextAsNullableInt(nunitNode, nameof(RandomSeed));
            RandomSeedSpecified = RandomSeed.HasValue;
            if (!RandomSeedSpecified)
            {
                RandomSeed = new Random().Next();
            }
            DefaultTestNamePattern = GetInnerTextWithLog(nunitNode, nameof(DefaultTestNamePattern));

            DumpXmlTestDiscovery = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestDiscovery), false);
            DumpXmlTestResults   = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestResults), false);
            var vsTestCategoryType = GetInnerText(nunitNode, nameof(VsTestCategoryType), Verbosity > 0);

            if (vsTestCategoryType != null)
            {
                switch (vsTestCategoryType.ToLower())
                {
                case "nunit":
                    VsTestCategoryType = VsTestCategoryType.NUnit;
                    break;

                case "mstest":
                    VsTestCategoryType = VsTestCategoryType.MsTest;
                    break;

                default:
                    _logger.Warning(
                        $"Invalid value ({vsTestCategoryType}) for VsTestCategoryType, should be either NUnit or MsTest");
                    break;
                }
            }



#if SUPPORT_REGISTRY_SETTINGS
            // Legacy (CTP) registry settings override defaults
            var registry = RegistryCurrentUser.OpenRegistryCurrentUser(@"Software\nunit.org\VSAdapter");
            if (registry.Exist("ShadowCopy") && (registry.Read <int>("ShadowCopy") == 1))
            {
                ShadowCopyFiles = true;
            }
            if (registry.Exist("Verbosity"))
            {
                Verbosity = registry.Read <int>("Verbosity");
            }
            if (registry.Exist("UseVsKeepEngineRunning") && (registry.Read <int>("UseVsKeepEngineRunning") == 1)
                UseVsKeepEngineRunning = true;
#endif

#if DEBUG && VERBOSE
            // Force Verbosity to 1 under Debug
            Verbosity = 1;
#endif

            var inProcDataCollectorNode =
                doc.SelectSingleNode("RunSettings/InProcDataCollectionRunSettings/InProcDataCollectors");
            InProcDataCollectorsAvailable = inProcDataCollectorNode != null &&
                                            inProcDataCollectorNode.SelectNodes("InProcDataCollector").Count > 0;

            // Older versions of VS do not pass the CollectDataForEachTestSeparately configuration together with the LiveUnitTesting collector.
            // However, the adapter is expected to run in CollectDataForEachTestSeparately mode.
            // As a result for backwards compatibility reasons enable CollectDataForEachTestSeparately mode whenever LiveUnitTesting collector is being used.
            var hasLiveUnitTestingDataCollector =
                inProcDataCollectorNode?.SelectSingleNode(
                    "InProcDataCollector[@uri='InProcDataCollector://Microsoft/LiveUnitTesting/1.0']") != null;

            // TestPlatform can opt-in to run tests one at a time so that the InProcDataCollectors can collect the data for each one of them separately.
            // In that case, we need to ensure that tests do not run in parallel and the test started/test ended events are sent synchronously.
            if (CollectDataForEachTestSeparately || hasLiveUnitTestingDataCollector)
            {
                NumberOfTestWorkers = 0;
                SynchronousEvents   = true;
                if (Verbosity >= 4)
                {
                    if (!InProcDataCollectorsAvailable)
                    {
                        _logger.Info(
                            "CollectDataForEachTestSeparately is set, which is used to make InProcDataCollectors collect data for each test separately. No InProcDataCollectors can be found, thus the tests will run slower unnecessarily.");
                    }
                }
            }

            // If DisableAppDomain settings is passed from the testplatform, set the DomainUsage to None.
            if (DisableAppDomain)
            {
                DomainUsage = "None";
            }

            // Update NumberOfTestWorkers based on the DisableParallelization and NumberOfTestWorkers from runsettings.
            UpdateNumberOfTestWorkers();


            string ValidatedPath(string path, string purpose)
            {
                try
                {
                    if (string.IsNullOrEmpty(WorkDirectory))
                    {
                        return(Path.GetFullPath(path));
                    }

                    if (Path.IsPathRooted(path))
                    {
                        return(Path.GetFullPath(path));
                    }
                    return(Path.GetFullPath(Path.Combine(WorkDirectory, path)));
                }
                catch (Exception)
                {
                    _logger.Error($"   Invalid path for {purpose}: {path}");
                    throw;
                }
            }
        }
Пример #4
0
 /// <summary>
 /// Used for test mocking only
 /// </summary>
 /// <param name="user"></param>
 public void SetCurrentUser(RegistryCurrentUser user)
 {
     currentUser = user;
 }
Пример #5
0
 public static RegistryCurrentUser OpenRegistryCurrentUser(string key)
 {
     return(currentUser ?? (currentUser = new RegistryCurrentUser(key)));
 }
Пример #6
0
        public void Load(string settingsXml)
        {
            if (settingsXml == null)
            {
                throw new ArgumentNullException("settingsXml", "Load called with null XML string");
            }
            if (settingsXml == string.Empty)
            {
                throw new ArgumentException("settingsXml", "Load called with empty XML string");
            }

            // Visual Studio already gives a good error message if the .runsettings
            // file is poorly formed, so we don't need to do anything more.
            var doc = new XmlDocument();

            doc.LoadXml(settingsXml);

            var runConfiguration = doc.SelectSingleNode("RunSettings/RunConfiguration");

            MaxCpuCount            = GetInnerTextAsInt(runConfiguration, "MaxCpuCount", -1);
            ResultsDirectory       = GetInnerText(runConfiguration, "ResultsDirectory");
            TargetPlatform         = GetInnerText(runConfiguration, "TargetPlatform");
            TargetFrameworkVersion = GetInnerText(runConfiguration, "TargetFrameworkVersion");
            TestAdapterPaths       = GetInnerText(runConfiguration, "TestAdapterPaths");

            TestProperties = new Dictionary <string, string>();
            foreach (XmlNode node in doc.SelectNodes("RunSettings/TestRunParameters/Parameter"))
            {
                var key   = node.GetAttribute("name");
                var value = node.GetAttribute("value");
                if (key != null && value != null)
                {
                    TestProperties.Add(key, value);
                }
            }

            var nunitNode = doc.SelectSingleNode("RunSettings/NUnit");

            InternalTraceLevel     = GetInnerText(nunitNode, "InternalTraceLevel", "Off", "Error", "Warning", "Info", "Verbose", "Debug");
            WorkDirectory          = GetInnerText(nunitNode, "WorkDirectory");
            DefaultTimeout         = GetInnerTextAsInt(nunitNode, "DefaultTimeout", 0);
            NumberOfTestWorkers    = GetInnerTextAsInt(nunitNode, "NumberOfTestWorkers", -1);
            ShadowCopyFiles        = GetInnerTextAsBool(nunitNode, "ShadowCopyFiles");
            Verbosity              = GetInnerTextAsInt(nunitNode, "Verbosity", 0);
            UseVsKeepEngineRunning = GetInnerTextAsBool(nunitNode, "UseVsKeepEngineRunning");
            BasePath            = GetInnerText(nunitNode, "BasePath");
            PrivateBinPath      = GetInnerText(nunitNode, "PrivateBinPath");
            RandomSeed          = GetInnerTextAsNullableInt(nunitNode, "RandomSeed");
            RandomSeedSpecified = RandomSeed.HasValue;
            if (!RandomSeedSpecified)
            {
                RandomSeed = new Random().Next();
            }

#if SUPPORT_REGISTRY_SETTINGS
            // Legacy (CTP) registry settings override defaults
            var registry = RegistryCurrentUser.OpenRegistryCurrentUser(@"Software\nunit.org\VSAdapter");
            if (registry.Exist("ShadowCopy") && (registry.Read <int>("ShadowCopy") == 1))
            {
                ShadowCopyFiles = true;
            }
            if (registry.Exist("Verbosity"))
            {
                Verbosity = registry.Read <int>("Verbosity");
            }
            if (registry.Exist("UseVsKeepEngineRunning") && (registry.Read <int>("UseVsKeepEngineRunning") == 1)
                UseVsKeepEngineRunning = true;
#endif

#if DEBUG && VERBOSE
            // Force Verbosity to 1 under Debug
            Verbosity = 1;
#endif

            // If any in proc data collector will be instantiated by the TestPlatform run tests sequentially.
            var inProcDataCollectorNode = doc.SelectSingleNode("RunSettings/InProcDataCollectionRunSettings/InProcDataCollectors");
            InProcDataCollectorsAvailable = inProcDataCollectorNode != null && inProcDataCollectorNode.SelectNodes("InProcDataCollector").Count > 0;
            if (InProcDataCollectorsAvailable)
            {
                NumberOfTestWorkers = 0;
                DomainUsage         = "None";
                SynchronousEvents   = true;
            }
        }
Пример #7
0
        public void Load(string settingsXml)
        {
            if (string.IsNullOrEmpty(settingsXml))
            {
                settingsXml = "<RunSettings />";
            }

            // Visual Studio already gives a good error message if the .runsettings
            // file is poorly formed, so we don't need to do anything more.
            var doc = new XmlDocument();

            doc.LoadXml(settingsXml);

            var nunitNode = doc.SelectSingleNode("RunSettings/NUnit");

            Verbosity         = GetInnerTextAsInt(nunitNode, nameof(Verbosity), 0);
            _logger.Verbosity = Verbosity;

            var runConfiguration = doc.SelectSingleNode("RunSettings/RunConfiguration");

            MaxCpuCount              = GetInnerTextAsInt(runConfiguration, nameof(MaxCpuCount), -1);
            ResultsDirectory         = GetInnerTextWithLog(runConfiguration, nameof(ResultsDirectory));
            TargetPlatform           = GetInnerTextWithLog(runConfiguration, nameof(TargetPlatform));
            TargetFrameworkVersion   = GetInnerTextWithLog(runConfiguration, nameof(TargetFrameworkVersion));
            TestAdapterPaths         = GetInnerTextWithLog(runConfiguration, nameof(TestAdapterPaths));
            CollectSourceInformation = GetInnerTextAsBool(runConfiguration, nameof(CollectSourceInformation), true);
            DisableAppDomain         = GetInnerTextAsBool(runConfiguration, nameof(DisableAppDomain), false);
            DisableParallelization   = GetInnerTextAsBool(runConfiguration, nameof(DisableParallelization), false);
            DesignMode = GetInnerTextAsBool(runConfiguration, nameof(DesignMode), false);

            TestProperties = new Dictionary <string, string>();
            foreach (XmlNode node in doc.SelectNodes("RunSettings/TestRunParameters/Parameter"))
            {
                var key   = node.GetAttribute("name");
                var value = node.GetAttribute("value");
                if (key != null && value != null)
                {
                    TestProperties.Add(key, value);
                }
            }

            // NUnit settings
            InternalTraceLevel     = GetInnerTextWithLog(nunitNode, nameof(InternalTraceLevel), "Off", "Error", "Warning", "Info", "Verbose", "Debug");
            WorkDirectory          = GetInnerTextWithLog(nunitNode, nameof(WorkDirectory));
            DefaultTimeout         = GetInnerTextAsInt(nunitNode, nameof(DefaultTimeout), 0);
            NumberOfTestWorkers    = GetInnerTextAsInt(nunitNode, nameof(NumberOfTestWorkers), -1);
            ShadowCopyFiles        = GetInnerTextAsBool(nunitNode, nameof(ShadowCopyFiles), false);
            UseVsKeepEngineRunning = GetInnerTextAsBool(nunitNode, nameof(UseVsKeepEngineRunning), false);
            BasePath            = GetInnerTextWithLog(nunitNode, nameof(BasePath));
            PrivateBinPath      = GetInnerTextWithLog(nunitNode, nameof(PrivateBinPath));
            RandomSeed          = GetInnerTextAsNullableInt(nunitNode, nameof(RandomSeed));
            RandomSeedSpecified = RandomSeed.HasValue;
            if (!RandomSeedSpecified)
            {
                RandomSeed = new Random().Next();
            }
            DefaultTestNamePattern = GetInnerTextWithLog(nunitNode, nameof(DefaultTestNamePattern));

            DumpXmlTestDiscovery = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestDiscovery), false);
            DumpXmlTestResults   = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestResults), false);



#if SUPPORT_REGISTRY_SETTINGS
            // Legacy (CTP) registry settings override defaults
            var registry = RegistryCurrentUser.OpenRegistryCurrentUser(@"Software\nunit.org\VSAdapter");
            if (registry.Exist("ShadowCopy") && (registry.Read <int>("ShadowCopy") == 1))
            {
                ShadowCopyFiles = true;
            }
            if (registry.Exist("Verbosity"))
            {
                Verbosity = registry.Read <int>("Verbosity");
            }
            if (registry.Exist("UseVsKeepEngineRunning") && (registry.Read <int>("UseVsKeepEngineRunning") == 1)
                UseVsKeepEngineRunning = true;
#endif

#if DEBUG && VERBOSE
            // Force Verbosity to 1 under Debug
            Verbosity = 1;
#endif

            // If any in proc data collector will be instantiated by the TestPlatform run tests sequentially.
            var inProcDataCollectorNode = doc.SelectSingleNode("RunSettings/InProcDataCollectionRunSettings/InProcDataCollectors");
            InProcDataCollectorsAvailable = inProcDataCollectorNode != null && inProcDataCollectorNode.SelectNodes("InProcDataCollector").Count > 0;
            if (InProcDataCollectorsAvailable)
            {
                NumberOfTestWorkers = 0;
                DomainUsage         = "None";
                SynchronousEvents   = true;
                if (Verbosity >= 4)
                {
                    _logger.Info($"InProcDataCollectors are available: turning off Parallel, DomainUsage=None, SynchronousEvents=true");
                }
            }

            // If DisableAppDomain settings is passed from the testplatform, set the DomainUsage to None.
            if (DisableAppDomain)
            {
                DomainUsage = "None";
            }

            // Update NumberOfTestWorkers based on the DisableParallelization and NumberOfTestWorkers from runsettings.
            UpdateNumberOfTestWorkers();
        }