Пример #1
0
        /// <summary>
        /// Executes StatLight with the specified arguments.
        /// </summary>
        /// <param name="arguments">The arguments.</param>
        internal void ExecuteStatLight(TestCaseArgument arguments)
        {
            string executingAssemblyLocation = Assembly.GetExecutingAssembly().Location;
            string currentAssemblyPath       = Path.GetDirectoryName(executingAssemblyLocation);

            if (currentAssemblyPath == null)
            {
                throw new Exception($"Failed to get directory name for executing assembly location: {executingAssemblyLocation}");
            }

            if (this.isBeingDebugged)
            {
                TestRunOptions testRunOptions = arguments.TestRunOptions;
                ILogger        consoleLogger  = new ConsoleLogger(LogChatterLevels.Full);

                InputOptions inputOptions = new InputOptions()
                                            .SetDllPaths(new[] { testRunOptions.DllPath })
                                            .SetMethodsToTest(testRunOptions.MethodsToTest)
                                            .SetReportOutputFileType(testRunOptions.ReportOutputFileType)
                                            .SetReportOutputPath(testRunOptions.ReportOutputPath)
                                            .SetIsRequestingDebug(testRunOptions.Debug)
                                            .SetSettingsOverride(testRunOptions.OverriddenSettings);

                if (!string.IsNullOrEmpty(testRunOptions.QueryString))
                {
                    inputOptions.SetQueryString(testRunOptions.QueryString);
                }

                if (testRunOptions.UnitTestProviderType != UnitTestProviderType.Undefined)
                {
                    inputOptions.SetUnitTestProviderType(testRunOptions.UnitTestProviderType);
                }

                RunnerExecutionEngine commandLineExecutionEngine = BootStrapper
                                                                   .Initialize(inputOptions, consoleLogger)
                                                                   .Resolve <RunnerExecutionEngine>();

                commandLineExecutionEngine.Run();
            }
            else
            {
                // run statlight on the command line so that messages sent to the Console within the unit test are written to the Test output window
                string testAssembly = arguments.TestRunOptions.DllPath;

                StringBuilder argument = new StringBuilder();
                argument.Append("-d=");
                argument.Append(string.Concat("\"", testAssembly, "\""));
                argument.Append(" --MethodsToTest=");
                argument.Append("\"");
                for (int i = 0; i < arguments.TestRunOptions.MethodsToTest.Count; i++)
                {
                    argument.Append(arguments.TestRunOptions.MethodsToTest.ElementAt(i));
                    if (i != arguments.TestRunOptions.MethodsToTest.Count - 1)
                    {
                        argument.Append(";");
                    }
                }

                argument.Append("\"");

                string assemblyPath = Path.GetDirectoryName(testAssembly);
                if (assemblyPath == null)
                {
                    throw new Exception($"Failed to get directory name for assembly location: {testAssembly}");
                }

                string configurationFilePath = Path.Combine(assemblyPath, SilverlightUnitTestAdapter.Constants.ConfigurationFileName);
                if (File.Exists(configurationFilePath))
                {
                    Settings settings = Settings.Load(configurationFilePath);
                    if (settings.QueryString != null && settings.QueryString.Count > 0)
                    {
                        argument.Append(" --QueryString=");
                        argument.Append("\"");

                        NameValueCollection queryString = HttpUtility.ParseQueryString(string.Empty);

                        foreach (KeyValuePair <string, string> keyValuePair in settings.QueryString)
                        {
                            queryString.Add(keyValuePair.Key, keyValuePair.Value);
                        }

                        argument.Append(queryString.ToString());

                        argument.Append("\"");
                    }

                    if (settings.UnitTestProvider != UnitTestProviderType.Undefined)
                    {
                        argument.Append($" --OverrideTestProvider:{settings.UnitTestProvider}");
                    }

                    if (settings.Debug)
                    {
                        argument.Append(" --debug");
                    }

                    if (settings.OverriddenSettings != null && settings.OverriddenSettings.Count > 0)
                    {
                        foreach (KeyValuePair <string, string> keyValuePair in settings.OverriddenSettings)
                        {
                            argument.Append($" --OverrideSetting:{keyValuePair.Key}={keyValuePair.Value}");
                        }
                    }
                }

                argument.Append(" -r=");
                argument.Append(string.Concat("\"", arguments.TestRunOptions.ReportOutputPath));
                argument.Append("\"");
                argument.Append(" --ReportOutputFileType:");
                argument.Append("\"TRX");
                argument.Append("\"");

                using (Process process = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = Path.Combine(currentAssemblyPath, "StatLight.exe"),
                        Arguments = argument.ToString(),
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true
                    }
                })
                {
                    this.logger.SendMessage(TestMessageLevel.Informational, $"\"{process.StartInfo.FileName}\" {process.StartInfo.Arguments}");

                    process.OutputDataReceived += (sender, args) => this.LogInfo(args.Data);
                    process.ErrorDataReceived  += (sender, args) => this.LogInfo(args.Data);

                    process.Start();
                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    process.WaitForExit();

                    this.logger.SendMessage(TestMessageLevel.Informational, string.Concat(Localized.StatLightExitCodeMessage, process.ExitCode));
                }
            }
        }