public virtual int StartTestRun()

in src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyExecutionManager.cs [151:266]


    public virtual int StartTestRun(TestRunCriteria testRunCriteria, ITestRunEventsHandler eventHandler)
    {
        if (_proxyOperationManager == null)
        {
            // In case we have an active test session, we always prefer the already
            // created proxies instead of the ones that need to be created on the spot.
            var sources = testRunCriteria.HasSpecificTests
                ? TestSourcesUtility.GetSources(testRunCriteria.Tests)
                : testRunCriteria.Sources;

            _proxyOperationManager = _proxyOperationManagerCreator(
                sources.First(),
                this);

            _testHostManager = _proxyOperationManager.TestHostManager;
        }

        _baseTestRunEventsHandler = eventHandler;
        try
        {
            if (EqtTrace.IsVerboseEnabled)
            {
                EqtTrace.Verbose("ProxyExecutionManager: Test host is always Lazy initialize.");
            }

            var testSources = new List<string>(
                testRunCriteria.HasSpecificSources
                ? testRunCriteria.Sources
                // If the test execution is with a test filter, group them by sources.
                : testRunCriteria.Tests.GroupBy(tc => tc.Source).Select(g => g.Key));

            _isCommunicationEstablished = _proxyOperationManager.SetupChannel(
                testSources,
                testRunCriteria.TestRunSettings);

            if (_isCommunicationEstablished)
            {
                _proxyOperationManager.CancellationTokenSource.Token.ThrowTestPlatformExceptionIfCancellationRequested();

                InitializeExtensions(testSources);

                // This code should be in sync with InProcessProxyExecutionManager.StartTestRun
                // execution context.
                var executionContext = new TestExecutionContext(
                    testRunCriteria.FrequencyOfRunStatsChangeEvent,
                    testRunCriteria.RunStatsChangeEventTimeout,
                    inIsolation: false,
                    keepAlive: testRunCriteria.KeepAlive,
                    isDataCollectionEnabled: false,
                    areTestCaseLevelEventsRequired: false,
                    hasTestRun: true,
                    // Debugging should happen if there's a custom test host launcher present
                    // and is in debugging mode, or if the debugging is enabled in case the
                    // test session info is present.
                    isDebug:
                        (testRunCriteria.TestHostLauncher != null && testRunCriteria.TestHostLauncher.IsDebug)
                        || _debugEnabledForTestSession,
                    testCaseFilter: testRunCriteria.TestCaseFilter,
                    filterOptions: testRunCriteria.FilterOptions);

                // This is workaround for the bug https://github.com/Microsoft/vstest/issues/970
                var runsettings = _proxyOperationManager.RemoveNodesFromRunsettingsIfRequired(
                    testRunCriteria.TestRunSettings,
                    (testMessageLevel, message) => { LogMessage(testMessageLevel, message); });

                if (testRunCriteria.HasSpecificSources)
                {
                    var runRequest = testRunCriteria.CreateTestRunCriteriaForSources(
                        _testHostManager,
                        runsettings,
                        executionContext,
                        testSources);
                    _proxyOperationManager.RequestSender.StartTestRun(runRequest, this);
                }
                else
                {
                    var runRequest = testRunCriteria.CreateTestRunCriteriaForTests(
                        _testHostManager,
                        runsettings,
                        executionContext,
                        testSources);
                    _proxyOperationManager.RequestSender.StartTestRun(runRequest, this);
                }
            }
        }
        catch (Exception exception)
        {
            EqtTrace.Error("ProxyExecutionManager.StartTestRun: Failed to start test run: {0}", exception);

            // Log error message to design mode and CLI.
            // TestPlatformException is expected exception, log only the message.
            // For other exceptions, log the stacktrace as well.
            var errorMessage = exception is TestPlatformException ? exception.Message : exception.ToString();
            var testMessagePayload = new TestMessagePayload()
            {
                MessageLevel = TestMessageLevel.Error,
                Message = errorMessage
            };
            HandleRawMessage(_dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload));
            LogMessage(TestMessageLevel.Error, errorMessage);

            // Send a run complete to caller. Similar logic is also used in
            // ParallelProxyExecutionManager.StartTestRunOnConcurrentManager.
            //
            // Aborted is `true`: in case of parallel run (or non shared host), an aborted
            // message ensures another execution manager created to replace the current one.
            // This will help if the current execution manager is aborted due to irreparable
            // error and the test host is lost as well.
            var completeArgs = new TestRunCompleteEventArgs(null, false, true, null, new Collection<AttachmentSet>(), new Collection<InvokedDataCollector>(), TimeSpan.Zero);
            var testRunCompletePayload = new TestRunCompletePayload { TestRunCompleteArgs = completeArgs };
            HandleRawMessage(_dataSerializer.SerializePayload(MessageType.ExecutionComplete, testRunCompletePayload));
            HandleTestRunComplete(completeArgs, null, null, null);
        }

        return 0;
    }