public bool TryOpenCoverageReport(string reportPath)
 {
     try
     {
         _report = GenericExtensions.ParseXml <CoverageSession>(reportPath);
         CoverageUpdated?.Invoke(this, EventArgs.Empty);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Example #2
0
        protected override TestReport RunTests(TestItem testItem, bool isCovering, bool isDebugging)
        {
            List <TestResult> testResults = new List <TestResult>();

            try
            {
                var sessionId       = _sessionId++;
                var outputDirectory = _storageController.CreateTestRunDirectory();

                var testMethods = testItem
                                  .Flatten(p => p.Children)
                                  .OfType <TestMethod>()
                                  .Where(p => p.Case != null)
                                  .ToArray();

                var testExecutionTasks = testMethods
                                         .GroupBy(p => p.TestAdapterName)
                                         .Distinct()
                                         .Select(p => new TestExecutionTask()
                {
                    TestCases          = p.Where(q => q.Case != null).Select(q => q.Case).ToArray(),
                    TestAdapterOptions = _testAdapterRepository.Adapters[p.Key]
                                         .GetLoadingOptions()
                                         .Do(q => q.IsRedirectingAssemblies = _options.IsRedirectingFrameworkAssemblies)
                })
                                         .ToArray();

                var testMethodsById = testMethods.ToDictionary(p => p.Case.Id);

                IHostProcessInfo hostProcessInfo = null;

                var solution = testItem.GetParent <TestSolution>();
                if (isCovering)
                {
                    var openCoverOptions = new OpenCoverOptions()
                    {
                        CodeAssemblies                = solution.CodeAssemblies,
                        TestAssemblies                = solution.TestAssemblies,
                        CoverageReportPath            = Path.Combine(outputDirectory, "coverageReport.xml"),
                        IsCoveringByTest              = _options.IsCoveringByTest,
                        IsIncludingSolutionAssemblies = _options.IsIncludingSolutionAssemblies,
                        IsExcludingTestAssemblies     = _options.IsExcludingTestAssemblies,
                        IsMergingByHash               = _options.IsMergingByHash,
                        IsSkippingAutoProps           = _options.IsSkippingAutoProps,
                        ExcludeAttributes             = _options.ExcludeAttributes,
                        ExcludeDirectories            = _options.ExcludeDirectories,
                        ExcludeFiles          = _options.ExcludeFiles,
                        Filters               = _options.Filters,
                        IsVisitorCountLimited = _options.IsVisitorCountLimited,
                        VisitorCountLimit     = _options.VisitorCountLimit
                    };
                    hostProcessInfo = new OpenCoverProcessInfo(openCoverOptions);
                }

                _executionProcess = ExecutionProcess.Create(AdapterExtensions.GetTestPlatformAssemblyPaths(_options.TestAdapterMode), hostProcessInfo, _options);
                _executionProcess.MessageReceived += (o, e) => OnTestLogAdded(e.Value);
                _executionProcess.TestStarted     += (o, e) =>
                {
                    var testMethod = testMethodsById.TryGetValue(e.Value.Id);
                    if (testMethod != null)
                    {
                        OnTestStarted(testMethod);
                    }
                };
                _executionProcess.TestResult += (o, e) =>
                {
                    var testMethod = testMethodsById.TryGetValue(e.Value.TestCase.Id);
                    if (testMethod != null)
                    {
                        var testResult = e.Value.ToTestResult(testMethod, sessionId);
                        testResults.Add(testResult);
                        OnTestExecuted(testResult);
                    }
                };
                _executionProcess.OutputReceived += (o, e) => OnTestLogAdded(e.Value);

                if (isDebugging)
                {
                    var debuggerAttachedEvent = new ManualResetEvent(false);
                    _executionProcess.DebuggerAttached += (o, e) => debuggerAttachedEvent.Set();

                    OnTestLogAdded(Resources.DebuggerAttaching);
                    if (_editorContext.AttachToProcess(_executionProcess.ProcessId) &&
                        debuggerAttachedEvent.WaitOne(TimeSpan.FromSeconds(_options.DebuggerTimeout)))
                    {
                        OnTestLogAdded(Resources.DebuggerAttached);
                        OnDebuggingStarted();
                    }
                    else
                    {
                        OnTestLogAdded(Resources.DebuggerFailedToAttach);
                    }
                }

                var options = new TestExecutionOptions()
                {
                    RunSettingsPath = _ioProvider.GetAbsolutePath(_options.TestSettings),
                    ApartmentState  = _options.TestApartmentState,
                    OutputPath      = outputDirectory,
                    SolutionPath    = solution.FilePath
                };

                _executionProcess.RunTests(testExecutionTasks, options);

                if (!_isAborting)
                {
                    if (isDebugging)
                    {
                        _editorContext.DetachFromProcess(_executionProcess.ProcessId);
                    }

                    OnTestLogAdded(Resources.ShuttingDown);
                    if (!_executionProcess.TryShutdown())
                    {
                        OnTestLogAdded(Resources.ShutdownFailed);
                    }
                    if (isCovering)
                    {
                        OnTestLogAdded(Resources.GeneratingCoverageReport);
                    }
                }

                _executionProcess.WaitForExit();

                if (_isAborting)
                {
                    return(null);
                }

                if (isCovering)
                {
                    var coverageReportPath = (hostProcessInfo as OpenCoverProcessInfo).CoverageReportPath;
                    if (System.IO.File.Exists(coverageReportPath))
                    {
                        var coverageReport = GenericExtensions.ParseXml <CoverageSession>(coverageReportPath);
                        return(new TestReport(testResults, coverageReport));
                    }
                }
                else
                {
                    return(new TestReport(testResults, null));
                }
            }
            catch (RemoteException exception) when(exception.RemoteReason != null)
            {
                _telemetryManager.UploadExceptionAsync(exception.RemoteReason);
                throw;
            }
            finally
            {
                if (_executionProcess != null)
                {
                    _executionProcess.Dispose();
                    _executionProcess = null;
                }
            }

            return(null);
        }