Esempio n. 1
0
        public TestCase[] DiscoverTests(TestDiscoveryTask[] discoveryTasks, string runSettingsPath)
        {
            _monitor.RecordMessage(TestMessageLevel.Informational, Resources.Branding);

            Thread.CurrentThread.Name         = "Test discoverer";
            Thread.CurrentThread.IsBackground = true;

            _monitor.RecordMessage(TestMessageLevel.Informational, $"> Discovering tests...");
            _monitor.RecordMessage(TestMessageLevel.Informational, $"| Runner version is {Assembly.GetExecutingAssembly().GetName().Version}.");
            _monitor.RecordMessage(TestMessageLevel.Informational, $"| We are on {(Environment.Is64BitProcess ? "x64" : "x86")} platform.");
            if (!string.IsNullOrEmpty(runSettingsPath))
            {
                _monitor.RecordMessage(TestMessageLevel.Informational, $"| Using run settings: {runSettingsPath}.");
            }

            try
            {
                var runSettings = new RunSettings(string.IsNullOrEmpty(runSettingsPath) ? null : File.ReadAllText(runSettingsPath));
                var context     = new TestDiscoveryContext(_monitor, runSettings);

                foreach (var discoveryTask in discoveryTasks)
                {
                    NativeServices.ExecuteWithFileRedirection(discoveryTask.TestAdapterOptions, () =>
                    {
                        var testDiscoverers = LoadDiscoverers(discoveryTask.TestAdapterOptions.AssemblyPath);

                        foreach (var testDiscoverer in testDiscoverers)
                        {
                            _monitor.RecordMessage(TestMessageLevel.Informational, $">> Running discoverer: {testDiscoverer.GetType().FullName}...");
                            foreach (var testSourcePath in discoveryTask.TestAssemblyPaths)
                            {
                                _monitor.RecordMessage(TestMessageLevel.Informational, $"|| Checking {testSourcePath}...");
                                try
                                {
                                    testDiscoverer.DiscoverTests(new[] { testSourcePath }, context, context, context);
                                }
                                catch (Exception e)
                                {
                                    _monitor.RecordMessage(TestMessageLevel.Warning, e.GetDescription());
                                }
                            }
                            _monitor.RecordMessage(TestMessageLevel.Informational, $"<< Discoverer finished.");
                        }
                    }, (level, message) => _monitor.RecordMessage(level, "| " + message));
                }

                _monitor.RecordMessage(TestMessageLevel.Informational, $"< Test discovery finished.");
                return(context.TestCases.Convert());
            }
            catch (Exception e)
            {
                _monitor.RecordMessage(TestMessageLevel.Error, $"< Could not discover tests!\r\n{e.GetDescription().PadLinesLeft("< ")}");
                return(new TestCase[0]);
            }
        }
        private void RunTestsWorker(TestExecutionTask[] executionTasks, TestExecutionOptions options)
        {
            Thread.CurrentThread.Name         = "Test executor";
            Thread.CurrentThread.IsBackground = true;

            _monitor.RecordMessage(TestMessageLevel.Informational, $"> Executing tests...");
            _monitor.RecordMessage(TestMessageLevel.Informational, $"| Runner version is {Assembly.GetExecutingAssembly().GetName().Version}.");
            _monitor.RecordMessage(TestMessageLevel.Informational, $"| We are on {(Environment.Is64BitProcess ? "x64" : "x86")} platform.");
            if (!string.IsNullOrEmpty(options.RunSettingsPath))
            {
                _monitor.RecordMessage(TestMessageLevel.Informational, $"| Using run settings  {options.RunSettingsPath}.");
            }

            try
            {
                var context = new TestExecutionContext(_monitor, options);
                foreach (var executionTask in executionTasks)
                {
                    NativeServices.ExecuteWithFileRedirection(executionTask.TestAdapterOptions, () =>
                    {
                        if (TryLoadExecutor(executionTask.TestAdapterOptions.AssemblyPath, executionTask.TestAdapterOptions.ExtensionUri, out var testExecutor))
                        {
                            _monitor.RecordMessage(TestMessageLevel.Informational, $">> Running executor: {testExecutor.GetType().FullName}...");
                            foreach (var testCaseGroup in executionTask.TestCases.GroupBy(p => p.Source))
                            {
                                _monitor.RecordMessage(TestMessageLevel.Informational, $"|| Processing tests in: {testCaseGroup.Key}...");
                                context.TestRunDirectory     = Path.GetDirectoryName(testCaseGroup.Key);
                                Environment.CurrentDirectory = context.TestRunDirectory;
                                testExecutor.RunTests(testCaseGroup.Convert(), context, context);
                            }
                            _monitor.RecordMessage(TestMessageLevel.Informational, $"<< Executor finished.");
                        }
                        else
                        {
                            foreach (var testCase in executionTask.TestCases)
                            {
                                var testResult = new Common.Models.TestResult()
                                {
                                    TestCase     = testCase,
                                    ErrorMessage = "Test executor is not loaded.",
                                    Outcome      = Common.Models.TestOutcome.Skipped
                                };
                                _monitor.RecordResult(testResult);
                            }
                        }
                    }, (level, message) => _monitor.RecordMessage(level, "| " + message));
                }
                _monitor.RecordMessage(TestMessageLevel.Informational, $"< Test execution finished.");
            }
            catch (Exception e)
            {
                _monitor.RecordMessage(TestMessageLevel.Error, $"< Could not execute tests.\r\n{e.GetDescription().PadLinesLeft("< ")}");
            }
        }