Пример #1
0
        private static void SetParallelSettings(XmlReader reader, MSTestSettings settings)
        {
            reader.Read();
            if (!reader.IsEmptyElement)
            {
                // Read the first child.
                reader.Read();

                while (reader.NodeType == XmlNodeType.Element)
                {
                    string elementName = reader.Name.ToUpperInvariant();
                    switch (elementName)
                    {
                    case "WORKERS":
                    {
                        var value = reader.ReadInnerXml();
                        if (int.TryParse(value, out int parallelWorkers))
                        {
                            if (parallelWorkers == 0)
                            {
                                settings.ParallelizationWorkers = Environment.ProcessorCount;
                            }
                            else if (parallelWorkers > 0)
                            {
                                settings.ParallelizationWorkers = parallelWorkers;
                            }
                            else
                            {
                                throw new AdapterSettingsException(
                                          string.Format(
                                              CultureInfo.CurrentCulture,
                                              Resource.InvalidParallelWorkersValue,
                                              value));
                            }
                        }
                        else
                        {
                            throw new AdapterSettingsException(
                                      string.Format(
                                          CultureInfo.CurrentCulture,
                                          Resource.InvalidParallelWorkersValue,
                                          value));
                        }

                        break;
                    }

                    case "SCOPE":
                    {
                        var value = reader.ReadInnerXml();
                        if (Enum.TryParse(value, out ExecutionScope scope))
                        {
                            settings.ParallelizationScope = scope;
                        }
                        else
                        {
                            throw new AdapterSettingsException(
                                      string.Format(
                                          CultureInfo.CurrentCulture,
                                          Resource.InvalidParallelScopeValue,
                                          value,
                                          string.Join(", ", Enum.GetNames(typeof(ExecutionScope)))));
                        }

                        break;
                    }

                    default:
                    {
                        throw new AdapterSettingsException(
                                  string.Format(
                                      CultureInfo.CurrentCulture,
                                      Resource.InvalidSettingsXmlElement,
                                      ParallelizeSettingsName,
                                      reader.Name));
                    }
                    }
                }
            }

            // If any of these properties are not set, resort to the defaults.
            if (!settings.ParallelizationWorkers.HasValue)
            {
                settings.ParallelizationWorkers = Environment.ProcessorCount;
            }

            if (!settings.ParallelizationScope.HasValue)
            {
                settings.ParallelizationScope = ExecutionScope.ClassLevel;
            }
        }
Пример #2
0
        /// <summary>
        /// Convert the parameter xml to TestSettings
        /// </summary>
        /// <param name="reader">Reader to load the settings from.</param>
        /// <returns>An instance of the <see cref="MSTestSettings"/> class</returns>
        private static MSTestSettings ToSettings(XmlReader reader)
        {
            ValidateArg.NotNull <XmlReader>(reader, "reader");

            // Expected format of the xml is: -
            //
            // <MSTestV2>
            //     <CaptureTraceOutput>true</CaptureTraceOutput>
            //     <MapInconclusiveToFailed>false</MapInconclusiveToFailed>
            //     <EnableBaseClassTestMethodsFromOtherAssemblies>false</EnableBaseClassTestMethodsFromOtherAssemblies>
            //     <TestTimeout>5000<TestTimeout>
            //     <Parallelize>
            //        <Workers>4</Workers>
            //        <Scope>TestClass</Scope>
            //     </Parallelize>
            // </MSTestV2>
            //
            // (or)
            //
            // <MSTest>
            //     <ForcedLegacyMode>true</ForcedLegacyMode>
            //     <SettingsFile>..\..\Local.testsettings</SettingsFile>
            //     <CaptureTraceOutput>true</CaptureTraceOutput>
            // </MSTest>
            MSTestSettings settings = new MSTestSettings();

            // Read the first element in the section which is either "MSTest"/"MSTestV2"
            reader.ReadToNextElement();

            if (!reader.IsEmptyElement)
            {
                reader.Read();

                while (reader.NodeType == XmlNodeType.Element)
                {
                    bool   result;
                    string elementName = reader.Name.ToUpperInvariant();
                    switch (elementName)
                    {
                    case "CAPTURETRACEOUTPUT":
                    {
                        if (bool.TryParse(reader.ReadInnerXml(), out result))
                        {
                            settings.CaptureDebugTraces = result;
                        }

                        break;
                    }

                    case "ENABLEBASECLASSTESTMETHODSFROMOTHERASSEMBLIES":
                    {
                        if (bool.TryParse(reader.ReadInnerXml(), out result))
                        {
                            settings.EnableBaseClassTestMethodsFromOtherAssemblies = result;
                        }

                        break;
                    }

                    case "FORCEDLEGACYMODE":
                    {
                        if (bool.TryParse(reader.ReadInnerXml(), out result))
                        {
                            settings.ForcedLegacyMode = result;
                        }

                        break;
                    }

                    case "MAPINCONCLUSIVETOFAILED":
                    {
                        if (bool.TryParse(reader.ReadInnerXml(), out result))
                        {
                            settings.MapInconclusiveToFailed = result;
                        }

                        break;
                    }

                    case "SETTINGSFILE":
                    {
                        string fileName = reader.ReadInnerXml();

                        if (!string.IsNullOrEmpty(fileName))
                        {
                            settings.TestSettingsFile = fileName;
                        }

                        break;
                    }

                    case "PARALLELIZE":
                    {
                        SetParallelSettings(reader.ReadSubtree(), settings);
                        reader.SkipToNextElement();

                        break;
                    }

                    case "TESTTIMEOUT":
                    {
                        if (int.TryParse(reader.ReadInnerXml(), out int testTimeout) && testTimeout > 0)
                        {
                            settings.TestTimeout = testTimeout;
                        }

                        break;
                    }

                    default:
                    {
                        PlatformServiceProvider.Instance.SettingsProvider.Load(reader.ReadSubtree());
                        reader.SkipToNextElement();

                        break;
                    }
                    }
                }
            }

            return(settings);
        }