Exemple #1
0
 /// <summary>
 /// Records the start tests.
 /// </summary>
 /// <param name="argument">The argument.</param>
 /// <param name="frameworkHandle">The framework handle.</param>
 internal static void RecordStartTests(TestCaseArgument argument, IFrameworkHandle frameworkHandle)
 {
     foreach (TestCase testCase in argument.TestCases)
     {
         frameworkHandle.RecordStart(testCase);
     }
 }
Exemple #2
0
        /// <summary>
        /// Processes the results.
        /// </summary>
        /// <param name="tests">The tests.</param>
        /// <param name="argument">The argument.</param>
        /// <param name="frameworkHandle">The framework handle.</param>
        internal void ProcessResults(IEnumerable <TestCase> tests, TestCaseArgument argument, IFrameworkHandle frameworkHandle)
        {
            TrxSchemaReader reader  = new TrxSchemaReader(this.logger, tests);
            TestRunType     testRun = reader.Read(argument.TestRunOptions.ReportOutputPath);

            if (testRun == null)
            {
                return;
            }

            foreach (TrxResult result in reader.ProcessStatLightResult(testRun))
            {
                TestResult testResult = result.GetTestResult(this.logger);
                frameworkHandle.RecordResult(testResult);
                frameworkHandle.RecordEnd(result.TestCase, testResult.Outcome);
            }
        }
Exemple #3
0
        /// <summary>
        /// Gets the StatLight arguments.
        /// </summary>
        /// <param name="testCases">The test cases.</param>
        /// <returns>List&lt;TestCaseArgument&gt;.</returns>
        internal static List <TestCaseArgument> GetArguments(IEnumerable <TestCase> testCases)
        {
            List <TestCaseArgument> arguments = new List <TestCaseArgument>();
            Dictionary <string, List <TestCase> > testMethodsInAssemblies = GetTestMethodsByAssembly(testCases);

            if (testMethodsInAssemblies == null || testMethodsInAssemblies.Count <= 0)
            {
                return(arguments);
            }

            foreach (string assembly in testMethodsInAssemblies.Keys)
            {
                TestRunOptions testRunOptions = new TestRunOptions
                {
                    DllPath              = assembly,
                    MethodsToTest        = testMethodsInAssemblies[assembly].Select(m => m.FullyQualifiedName).ToList(),
                    ReportOutputFileType = ReportOutputFileType.TRX,
                    ReportOutputPath     = string.Concat(assembly, "_TestResult.xml")
                };

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

                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)
                    {
                        NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(string.Empty);

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

                        testRunOptions.QueryString = nameValueCollection.ToString();
                    }

                    if (settings.UnitTestProvider != UnitTestProviderType.Undefined)
                    {
                        testRunOptions.UnitTestProviderType = settings.UnitTestProvider;
                    }

                    testRunOptions.Debug = settings.Debug;

                    if (settings.OverriddenSettings != null && settings.OverriddenSettings.Count > 0)
                    {
                        testRunOptions.OverriddenSettings = settings.OverriddenSettings;
                    }
                }

                TestCaseArgument argumentInfo = new TestCaseArgument(testRunOptions, testMethodsInAssemblies[assembly]);
                arguments.Add(argumentInfo);
            }

            return(arguments);
        }
Exemple #4
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));
                }
            }
        }