Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TestRequestSender.CheckVersionWithTestHost
TestRequestSenderTests.cs
Source:TestRequestSenderTests.cs  
...189            this.mockExecutionEventsHandler.Verify(eh => eh.HandleRawMessage(It.IsAny<string>()), Times.Never);190        }191        #region Version Check Tests192        [TestMethod]193        public void CheckVersionWithTestHostShouldSendHighestSupportedVersion()194        {195            this.SetupDeserializeMessage(MessageType.VersionCheck, 99);196            this.SetupRaiseMessageReceivedOnCheckVersion();197            this.SetupFakeCommunicationChannel();198            this.testRequestSender.CheckVersionWithTestHost();199            this.mockDataSerializer.Verify(ds => ds.SerializePayload(MessageType.VersionCheck, DUMMYPROTOCOLVERSION), Times.Once);200            this.mockChannel.Verify(mc => mc.Send(It.IsAny<string>()), Times.Once);201        }202        [TestMethod]203        public void CheckVersionWithTestHostShouldThrowIfTestHostVersionDoesNotMatch()204        {205            this.SetupDeserializeMessage(MessageType.ProtocolError, string.Empty);206            this.SetupRaiseMessageReceivedOnCheckVersion();207            this.SetupFakeCommunicationChannel();208            Assert.ThrowsException<TestPlatformException>(() => this.testRequestSender.CheckVersionWithTestHost());209        }210        [TestMethod]211        public void CheckVersionWithTestHostShouldThrowIfUnexpectedResponseIsReceived()212        {213            this.SetupDeserializeMessage(MessageType.TestCasesFound, string.Empty);214            this.SetupRaiseMessageReceivedOnCheckVersion();215            this.SetupFakeCommunicationChannel();216            Assert.ThrowsException<TestPlatformException>(() => this.testRequestSender.CheckVersionWithTestHost());217        }218        [TestMethod]219        public void CheckVersionWithTestHostShouldThrowIfProtocolNegotiationTimeouts()220        {221            Environment.SetEnvironmentVariable(EnvironmentHelper.VstestConnectionTimeout, "0");222            this.SetupFakeCommunicationChannel();223            var message = Assert.ThrowsException<TestPlatformException>(() => this.testRequestSender.CheckVersionWithTestHost()).Message;224            Assert.AreEqual(message, TestRequestSenderTests.TimoutErrorMessage);225        }226        #endregion227        #region Discovery Protocol Tests228        [TestMethod]229        public void InitializeDiscoveryShouldSendCommunicationMessageWithCorrectParameters()230        {231            this.SetupFakeCommunicationChannel();232            this.testRequestSender.InitializeDiscovery(this.pathToAdditionalExtensions);233            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.DiscoveryInitialize, this.pathToAdditionalExtensions, 1), Times.Once);234            this.mockChannel.Verify(mc => mc.Send(It.IsAny<string>()), Times.Once);235        }236        [TestMethod]237        public void InitializeDiscoveryShouldSendCommunicationMessageWithCorrectParametersWithVersion()238        {239            this.SetupFakeChannelWithVersionNegotiation(DUMMYNEGOTIATEDPROTOCOLVERSION);240            this.testRequestSender.InitializeDiscovery(this.pathToAdditionalExtensions);241            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.DiscoveryInitialize, this.pathToAdditionalExtensions, DUMMYNEGOTIATEDPROTOCOLVERSION), Times.Once);242        }243        [TestMethod]244        public void DiscoverTestsShouldSendStartDiscoveryMessageOnChannel()245        {246            this.SetupFakeCommunicationChannel();247            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);248            this.mockDataSerializer.Verify(249                s => s.SerializePayload(MessageType.StartDiscovery, It.IsAny<DiscoveryCriteria>(), DEFAULTPROTOCOLVERSION),250                Times.Once);251            this.mockChannel.Verify(mc => mc.Send(It.IsAny<string>()), Times.Once);252        }253        [TestMethod]254        public void DiscoverTestsShouldSendStartDiscoveryMessageOnChannelWithVersion()255        {256            this.SetupFakeChannelWithVersionNegotiation(DUMMYNEGOTIATEDPROTOCOLVERSION);257            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);258            this.mockDataSerializer.Verify(259                s => s.SerializePayload(MessageType.StartDiscovery, It.IsAny<DiscoveryCriteria>(), DUMMYNEGOTIATEDPROTOCOLVERSION),260                Times.Once);261        }262        [TestMethod]263        public void DiscoverTestsShouldNotifyRawMessageOnMessageReceived()264        {265            this.SetupDeserializeMessage(MessageType.TestMessage, new TestMessagePayload());266            this.SetupFakeCommunicationChannel();267            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);268            this.RaiseMessageReceivedEvent();269            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleRawMessage("DummyData"), Times.Once);270        }271        [TestMethod]272        public void DiscoverTestsShouldNotifyDiscoveredTestsOnTestCasesFoundMessageReceived()273        {274            this.SetupDeserializeMessage<IEnumerable<TestCase>>(MessageType.TestCasesFound, new TestCase[2]);275            this.SetupFakeCommunicationChannel();276            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);277            this.RaiseMessageReceivedEvent();278            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleDiscoveredTests(It.Is<IEnumerable<TestCase>>(t => t.Count() == 2)));279        }280        [TestMethod]281        public void DiscoverTestsShouldNotifyDiscoveryCompleteOnCompleteMessageReceived()282        {283            var completePayload = new DiscoveryCompletePayload { TotalTests = 10, IsAborted = false };284            this.SetupDeserializeMessage(MessageType.DiscoveryComplete, completePayload);285            this.SetupFakeCommunicationChannel();286            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);287            this.RaiseMessageReceivedEvent();288            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleDiscoveryComplete(It.Is<DiscoveryCompleteEventArgs>(dc => dc.IsAborted == false && dc.TotalCount == 10), null));289        }290        [TestMethod]291        public void DiscoverTestsShouldStopServerOnCompleteMessageReceived()292        {293            var completePayload = new DiscoveryCompletePayload { TotalTests = 10, IsAborted = false };294            this.SetupDeserializeMessage(MessageType.DiscoveryComplete, completePayload);295            this.SetupFakeCommunicationChannel();296            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);297            this.RaiseMessageReceivedEvent();298            this.mockServer.Verify(ms => ms.Stop());299        }300        [TestMethod]301        public void DiscoverTestShouldNotifyLogMessageOnTestMessageReceived()302        {303            var message = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = "Message1" };304            this.SetupDeserializeMessage(MessageType.TestMessage, message);305            this.SetupFakeCommunicationChannel();306            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);307            this.RaiseMessageReceivedEvent();308            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, "Message1"));309        }310        [TestMethod]311        public void DiscoverTestShouldNotifyLogMessageIfExceptionThrownOnMessageReceived()312        {313            this.SetupExceptionOnMessageReceived();314            this.SetupFakeCommunicationChannel();315            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);316            this.RaiseMessageReceivedEvent();317            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, It.Is<string>(s => s.Contains("Dummy Message"))));318            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleRawMessage("SerializedMessage"), Times.Once);319        }320        [TestMethod]321        public void DiscoverTestShouldNotifyDiscoveryCompleteIfExceptionThrownOnMessageReceived()322        {323            this.SetupExceptionOnMessageReceived();324            this.SetupFakeCommunicationChannel();325            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);326            this.RaiseMessageReceivedEvent();327            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleDiscoveryComplete(It.Is<DiscoveryCompleteEventArgs>(dc => dc.IsAborted == true && dc.TotalCount == -1), null));328        }329        [TestMethod]330        public void DiscoverTestsShouldNotAbortDiscoveryIfClientDisconnectedAndOperationIsComplete()331        {332            var completePayload = new DiscoveryCompletePayload { TotalTests = 10, IsAborted = false };333            this.SetupDeserializeMessage(MessageType.DiscoveryComplete, completePayload);334            this.SetupFakeCommunicationChannel();335            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);336            this.RaiseMessageReceivedEvent();   // Raise discovery complete337            this.RaiseClientDisconnectedEvent();338            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, It.IsAny<string>()), Times.Never);339            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleDiscoveryComplete(new DiscoveryCompleteEventArgs(-1, true), null), Times.Never);340        }341        [TestMethod]342        public void DiscoverTestShouldNotifyLogMessageIfClientDisconnected()343        {344            // Expect default error message since we've not set any client exit message345            var expectedErrorMessage = "Reason: Unable to communicate";346            this.SetupFakeCommunicationChannel();347            this.mockDataSerializer.Setup(ds => ds.SerializePayload(MessageType.TestMessage, It.Is<TestMessagePayload>(p => p.Message.Contains(expectedErrorMessage))))348                .Returns("Serialized error");349            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);350            this.RaiseClientDisconnectedEvent();351            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, It.Is<string>(s => s.Contains(expectedErrorMessage))));352            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleRawMessage(It.Is<string>(s => !string.IsNullOrEmpty(s) && s.Equals("Serialized error"))), Times.Once);353        }354        [TestMethod]355        public void DiscoverTestShouldNotifyLogMessageIfClientDisconnectedWithClientExit()356        {357            this.SetupFakeCommunicationChannel();358            this.mockDataSerializer.Setup(ds => ds.SerializePayload(MessageType.TestMessage, It.Is<TestMessagePayload>(p => p.Message.Contains("Dummy Stderr"))))359                .Returns("Serialized Stderr");360            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);361            this.testRequestSender.OnClientProcessExit("Dummy Stderr");362            this.RaiseClientDisconnectedEvent();363            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, It.Is<string>(s => s.Contains("Dummy Stderr"))), Times.Once);364            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleRawMessage(It.Is<string>(s => !string.IsNullOrEmpty(s) && s.Equals("Serialized Stderr"))), Times.Once);365        }366        [TestMethod]367        public void DiscoverTestShouldNotifyDiscoveryCompleteIfClientDisconnected()368        {369            this.SetupFakeCommunicationChannel();370            this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);371            this.RaiseClientDisconnectedEvent();372            this.mockDiscoveryEventsHandler.Verify(eh => eh.HandleDiscoveryComplete(It.Is<DiscoveryCompleteEventArgs>(dc => dc.IsAborted == true && dc.TotalCount == -1), null));373        }374        #endregion375        #region Execution Protocol Tests376        [TestMethod]377        public void InitializeExecutionShouldSendCommunicationMessageWithCorrectParameters()378        {379            this.SetupFakeCommunicationChannel();380            this.testRequestSender.InitializeExecution(this.pathToAdditionalExtensions);381            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.ExecutionInitialize, this.pathToAdditionalExtensions, 1), Times.Once);382            this.mockChannel.Verify(mc => mc.Send(It.IsAny<string>()), Times.Once);383        }384        [TestMethod]385        public void InitializeExecutionShouldSendCommunicationMessageWithCorrectParametersWithVersion()386        {387            this.SetupFakeChannelWithVersionNegotiation(DUMMYNEGOTIATEDPROTOCOLVERSION);388            this.testRequestSender.InitializeExecution(this.pathToAdditionalExtensions);389            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.ExecutionInitialize, this.pathToAdditionalExtensions, DUMMYNEGOTIATEDPROTOCOLVERSION), Times.Once);390        }391        [TestMethod]392        public void StartTestRunShouldSendStartTestExecutionWithSourcesOnChannel()393        {394            this.SetupFakeCommunicationChannel();395            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);396            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.StartTestExecutionWithSources, this.testRunCriteriaWithSources, DEFAULTPROTOCOLVERSION), Times.Once);397            this.mockChannel.Verify(mc => mc.Send(It.IsAny<string>()), Times.Once);398        }399        [TestMethod]400        public void StartTestRunShouldSendStartTestExecutionWithSourcesOnChannelWithVersion()401        {402            this.SetupFakeChannelWithVersionNegotiation(DUMMYNEGOTIATEDPROTOCOLVERSION);403            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);404            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.StartTestExecutionWithSources, this.testRunCriteriaWithSources, DUMMYNEGOTIATEDPROTOCOLVERSION), Times.Once);405        }406        [TestMethod]407        public void StartTestRunWithTestsShouldSendStartTestExecutionWithTestsOnChannel()408        {409            var runCriteria = new TestRunCriteriaWithTests(new TestCase[2], "runsettings", null, null);410            this.SetupFakeCommunicationChannel();411            this.testRequestSender.StartTestRun(runCriteria, this.mockExecutionEventsHandler.Object);412            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.StartTestExecutionWithTests, runCriteria, DEFAULTPROTOCOLVERSION), Times.Once);413            this.mockChannel.Verify(mc => mc.Send(It.IsAny<string>()), Times.Once);414        }415        [TestMethod]416        public void StartTestRunWithTestsShouldSendStartTestExecutionWithTestsOnChannelWithVersion()417        {418            var runCriteria = new TestRunCriteriaWithTests(new TestCase[2], "runsettings", null, null);419            this.SetupFakeChannelWithVersionNegotiation(DUMMYNEGOTIATEDPROTOCOLVERSION);420            this.testRequestSender.StartTestRun(runCriteria, this.mockExecutionEventsHandler.Object);421            this.mockDataSerializer.Verify(d => d.SerializePayload(MessageType.StartTestExecutionWithTests, runCriteria, DUMMYNEGOTIATEDPROTOCOLVERSION), Times.Once);422        }423        [TestMethod]424        public void StartTestRunShouldNotifyRawMessageOnMessageReceived()425        {426            this.SetupDeserializeMessage(MessageType.TestMessage, new TestMessagePayload());427            this.SetupFakeCommunicationChannel();428            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);429            this.RaiseMessageReceivedEvent();430            this.mockExecutionEventsHandler.Verify(eh => eh.HandleRawMessage("DummyData"), Times.Once);431        }432        [TestMethod]433        public void StartTestRunShouldNotifyTestRunStatsChangeOnRunStatsMessageReceived()434        {435            var testRunChangedArgs = new TestRunChangedEventArgs(436                null,437                new Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult[2],438                new TestCase[2]);439            this.SetupDeserializeMessage(MessageType.TestRunStatsChange, testRunChangedArgs);440            this.SetupFakeCommunicationChannel();441            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);442            this.RaiseMessageReceivedEvent();443            this.mockExecutionEventsHandler.Verify(eh => eh.HandleTestRunStatsChange(testRunChangedArgs), Times.Once);444        }445        [TestMethod]446        public void StartTestRunShouldNotifyExecutionCompleteOnRunCompleteMessageReceived()447        {448            var testRunCompletePayload = new TestRunCompletePayload449            {450                TestRunCompleteArgs = new TestRunCompleteEventArgs(null, false, false, null, null, TimeSpan.MaxValue),451                LastRunTests = new TestRunChangedEventArgs(null, null, null),452                RunAttachments = new List<AttachmentSet>()453            };454            this.SetupDeserializeMessage(MessageType.ExecutionComplete, testRunCompletePayload);455            this.SetupFakeCommunicationChannel();456            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);457            this.RaiseMessageReceivedEvent();458            this.mockExecutionEventsHandler.Verify(459                eh => eh.HandleTestRunComplete(460                    testRunCompletePayload.TestRunCompleteArgs,461                    testRunCompletePayload.LastRunTests,462                    testRunCompletePayload.RunAttachments,463                    It.IsAny<ICollection<string>>()),464                Times.Once);465        }466        [TestMethod]467        public void StartTestRunShouldStopServerOnRunCompleteMessageReceived()468        {469            var testRunCompletePayload = new TestRunCompletePayload470            {471                TestRunCompleteArgs = new TestRunCompleteEventArgs(null, false, false, null, null, TimeSpan.MaxValue),472                LastRunTests = new TestRunChangedEventArgs(null, null, null),473                RunAttachments = new List<AttachmentSet>()474            };475            this.SetupDeserializeMessage(MessageType.ExecutionComplete, testRunCompletePayload);476            this.SetupFakeCommunicationChannel();477            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);478            this.RaiseMessageReceivedEvent();479            this.mockServer.Verify(ms => ms.Stop());480        }481        [TestMethod]482        public void StartTestRunShouldNotifyLogMessageOnTestMessageReceived()483        {484            var testMessagePayload = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = "Dummy" };485            this.SetupDeserializeMessage(MessageType.TestMessage, testMessagePayload);486            this.SetupFakeCommunicationChannel();487            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);488            this.RaiseMessageReceivedEvent();489            this.mockExecutionEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, "Dummy"), Times.Once);490        }491        [TestMethod]492        public void StartTestRunShouldNotifyLaunchWithDebuggerOnMessageReceived()493        {494            var launchMessagePayload = new TestProcessStartInfo();495            this.SetupDeserializeMessage(MessageType.LaunchAdapterProcessWithDebuggerAttached, launchMessagePayload);496            this.SetupFakeCommunicationChannel();497            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);498            this.RaiseMessageReceivedEvent();499            this.mockExecutionEventsHandler.Verify(eh => eh.LaunchProcessWithDebuggerAttached(launchMessagePayload), Times.Once);500        }501        [TestMethod]502        public void StartTestRunShouldSendLaunchDebuggerAttachedCallbackOnMessageReceived()503        {504            var launchMessagePayload = new TestProcessStartInfo();505            this.SetupDeserializeMessage(MessageType.LaunchAdapterProcessWithDebuggerAttached, launchMessagePayload);506            this.SetupFakeCommunicationChannel();507            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);508            this.RaiseMessageReceivedEvent();509            this.mockDataSerializer.Verify(ds => ds.SerializePayload(MessageType.LaunchAdapterProcessWithDebuggerAttachedCallback, It.IsAny<int>(), 1), Times.Once);510            this.mockChannel.Verify(c => c.Send(It.IsAny<string>()), Times.AtLeastOnce);511        }512        [TestMethod]513        public void StartTestRunShouldSendLaunchDebuggerAttachedCallbackOnMessageReceivedWithVersion()514        {515            var launchMessagePayload = new TestProcessStartInfo();516            this.SetupFakeChannelWithVersionNegotiation(DUMMYNEGOTIATEDPROTOCOLVERSION);517            this.SetupDeserializeMessage(MessageType.LaunchAdapterProcessWithDebuggerAttached, launchMessagePayload);518            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);519            this.RaiseMessageReceivedEvent();520            this.mockDataSerializer.Verify(ds => ds.SerializePayload(MessageType.LaunchAdapterProcessWithDebuggerAttachedCallback, It.IsAny<int>(), DUMMYNEGOTIATEDPROTOCOLVERSION), Times.Once);521        }522        [TestMethod]523        public void StartTestRunShouldNotifyLogMessageIfExceptionIsThrownOnMessageReceived()524        {525            this.SetupExceptionOnMessageReceived();526            this.SetupFakeCommunicationChannel();527            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);528            this.RaiseMessageReceivedEvent();529            this.mockExecutionEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, It.Is<string>(s => s.Contains("Dummy Message"))), Times.Once);530            this.mockExecutionEventsHandler.Verify(eh => eh.HandleRawMessage("SerializedMessage"), Times.Once);531        }532        [TestMethod]533        public void StartTestRunShouldNotifyExecutionCompleteIfExceptionIsThrownOnMessageReceived()534        {535            this.SetupExceptionOnMessageReceived();536            this.SetupFakeCommunicationChannel();537            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);538            this.RaiseMessageReceivedEvent();539            this.mockExecutionEventsHandler.Verify(eh => eh.HandleTestRunComplete(It.Is<TestRunCompleteEventArgs>(t => t.IsAborted), null, null, null), Times.Once);540            this.mockExecutionEventsHandler.Verify(eh => eh.HandleRawMessage("SerializedAbortedPayload"), Times.Once);541        }542        [TestMethod]543        public void StartTestRunShouldNotNotifyExecutionCompleteIfClientDisconnectedAndOperationComplete()544        {545            var testRunCompletePayload = new TestRunCompletePayload546            {547                TestRunCompleteArgs = new TestRunCompleteEventArgs(null, false, false, null, null, TimeSpan.MaxValue),548                LastRunTests = new TestRunChangedEventArgs(null, null, null),549                RunAttachments = new List<AttachmentSet>()550            };551            this.SetupDeserializeMessage(MessageType.ExecutionComplete, testRunCompletePayload);552            this.SetupFakeCommunicationChannel();553            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);554            this.RaiseMessageReceivedEvent();   // Raise test run complete555            this.RaiseClientDisconnectedEvent();556            this.mockExecutionEventsHandler.Verify(eh => eh.HandleTestRunComplete(It.Is<TestRunCompleteEventArgs>(t => t.IsAborted), null, null, null), Times.Never);557            this.mockExecutionEventsHandler.Verify(eh => eh.HandleRawMessage("SerializedAbortedPayload"), Times.Never);558        }559        [TestMethod]560        public void StartTestRunShouldNotifyErrorLogMessageIfClientDisconnected()561        {562            this.SetupFakeCommunicationChannel();563            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);564            this.RaiseClientDisconnectedEvent();565            // Expect default error message since we've not set any client exit message566            var expectedErrorMessage = "Reason: Unable to communicate";567            this.mockExecutionEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, It.Is<string>(s => s.Contains(expectedErrorMessage))), Times.Once);568        }569        [TestMethod]570        public void StartTestRunShouldNotifyErrorLogMessageIfClientDisconnectedWithClientExit()571        {572            this.SetupFakeCommunicationChannel();573            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);574            this.testRequestSender.OnClientProcessExit("Dummy Stderr");575            this.RaiseClientDisconnectedEvent();576            var expectedErrorMessage = "Reason: Test host process crashed : Dummy Stderr";577            this.mockExecutionEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, It.Is<string>(s => s.Contains(expectedErrorMessage))), Times.Once);578        }579        [TestMethod]580        public void StartTestRunShouldNotifyExecutionCompleteIfClientDisconnected()581        {582            this.SetupOperationAbortedPayload();583            this.SetupFakeCommunicationChannel();584            this.testRequestSender.StartTestRun(this.testRunCriteriaWithSources, this.mockExecutionEventsHandler.Object);585            this.RaiseClientDisconnectedEvent();586            this.mockExecutionEventsHandler.Verify(eh => eh.HandleTestRunComplete(It.Is<TestRunCompleteEventArgs>(t => t.IsAborted), null, null, null), Times.Once);587            this.mockExecutionEventsHandler.Verify(eh => eh.HandleRawMessage("SerializedAbortedPayload"), Times.Once);588        }589        [TestMethod]590        public void SendTestRunCancelShouldSendCancelTestRunMessage()591        {592            this.SetupFakeCommunicationChannel();593            this.testRequestSender.SendTestRunCancel();594            this.mockDataSerializer.Verify(ds => ds.SerializeMessage(MessageType.CancelTestRun), Times.Once);595            this.mockChannel.Verify(c => c.Send(It.IsAny<string>()), Times.Once);596        }597        [TestMethod]598        public void SendTestRunAbortShouldSendAbortTestRunMessage()599        {600            this.SetupFakeCommunicationChannel();601            this.testRequestSender.SendTestRunAbort();602            this.mockDataSerializer.Verify(ds => ds.SerializeMessage(MessageType.AbortTestRun), Times.Once);603            this.mockChannel.Verify(c => c.Send(It.IsAny<string>()), Times.Once);604        }605        #endregion606        private string SetupFakeCommunicationChannel(string connectionArgs = "123")607        {608            this.connectionInfo = new TestHostConnectionInfo609            {610                Endpoint = IPAddress.Loopback + ":" + connectionArgs,611                Role = ConnectionRole.Client,612                Transport = Transport.Sockets613            };614            // Setup mock connected event and initialize communication channel615            this.mockServer.Setup(mc => mc.Start(this.connectionInfo.Endpoint))616                .Returns(this.connectionInfo.Endpoint)617                .Callback(() => this.mockServer.Raise(s => s.Connected += null, this.mockServer.Object, this.connectedEventArgs));618            return this.testRequestSender.InitializeCommunication().ToString();619        }620        private void SetupFakeChannelWithVersionNegotiation(int protocolVersion)621        {622            // Sends a check version message to setup the negotiated protocol version.623            // This method is only required in specific tests.624            this.SetupDeserializeMessage(MessageType.VersionCheck, DUMMYNEGOTIATEDPROTOCOLVERSION);625            this.SetupRaiseMessageReceivedOnCheckVersion();626            this.SetupFakeCommunicationChannel();627            this.testRequestSender.CheckVersionWithTestHost();628            this.ResetRaiseMessageReceivedOnCheckVersion();629        }630        private void RaiseMessageReceivedEvent()631        {632            this.mockChannel.Raise(633                c => c.MessageReceived += null,634                this.mockChannel.Object,635                new MessageReceivedEventArgs { Data = "DummyData" });636        }637        private void RaiseClientDisconnectedEvent()638        {639            this.mockServer.Raise(640                s => s.Disconnected += null,641                this.mockServer.Object,...CheckVersionWithTestHost
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Reflection;9using System.Text;10using System.Threading.Tasks;11{12    {13        static void Main(string[] args)14        {15            TestRequestSender sender = new TestRequestSender();16            sender.InitializeCommunication();17            Version version = sender.CheckVersionWithTestHost("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\Extensions\\TestPlatform\\vstest.console.exe", new TestProcessStartInfo());18            Console.WriteLine(version);19        }20    }21}CheckVersionWithTestHost
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10    {11        static void Main(string[] args)12        {13            TestRequestSender testRequestSender = new TestRequestSender();14            testRequestSender.InitializeCommunication();15            Version version = testRequestSender.CheckVersionWithTestHost();16            Console.WriteLine("Version of test host is {0}", version);17            testRequestSender.WaitForRequestHandlerConnection(10000);18            Console.WriteLine("Test host is listening for requests");19            testRequestSender.WaitForRequestHandlerConnection(10000);20            Console.WriteLine("Test host is listening for requests");21            Console.WriteLine("Press any key to exit");22            Console.ReadKey();23        }24    }25}26C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\TestPlatform>testhost.exe27C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\TestPlatform>testhost.exeCheckVersionWithTestHost
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;7using System.Threading;8using Microsoft.VisualStudio.TestPlatform.ObjectModel;9using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;10using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;11using System.Diagnostics;12using System.IO;13using System.Reflection;14{15    {16        static void Main(string[] args)17        {18            var testRequestSender = new TestRequestSender();19            testRequestSender.InitializeCommunication();20            var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);21            var testHostPath = Path.Combine(path, "TestHost", "testhost.x86.exe");22            var testHostProcess = new Process();23            testHostProcess.StartInfo.FileName = testHostPath;24            testHostProcess.Start();25            var testHostProcessId = testRequestSender.CheckVersionWithTestHost(10000, CancellationToken.None);26            Console.WriteLine("testHostProcessId: " + testHostProcessId);27            Console.ReadLine();28        }29    }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;37using System.Threading;38using Microsoft.VisualStudio.TestPlatform.ObjectModel;39using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;40using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;41using System.Diagnostics;42using System.IO;43using System.Reflection;44{45    {46        static void Main(string[] args)47        {48            var testRequestSender = new TestRequestSender();49            testRequestSender.InitializeCommunication();50            var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);51            var testHostPath = Path.Combine(path, "TestHost", "testhost.x86.exe");52            var testHostProcess = new Process();53            testHostProcess.StartInfo.FileName = testHostPath;54            testHostProcess.Start();55            var testHostProcessId = testRequestSender.CheckVersionWithTestHost(10000, CancellationToken.None);56            Console.WriteLine("testHostProcessId: " + testHostProcessId);CheckVersionWithTestHost
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8{9    {10        static void Main(string[] args)11        {12            TestRequestSender testRequestSender = new TestRequestSender();13            var version = testRequestSender.CheckVersionWithTestHost();14            Console.WriteLine("Version is: " + version);15            Console.ReadKey();16        }17    }18}CheckVersionWithTestHost
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;5using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;6using System;7using System.Collections.Generic;8using System.IO;9using System.Linq;10using System.Reflection;11using System.Text;12using System.Threading.Tasks;13{14    {15        static void Main(string[] args)16        {17            Assembly assembly = Assembly.LoadFrom("Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.dll");18            Type type = assembly.GetType("Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TestRequestSender");19            var instance = Activator.CreateInstance(type, new object[] { });20            var method = type.GetMethod("CheckVersionWithTestHost", BindingFlags.NonPublic | BindingFlags.Instance);21            var result = method.Invoke(instance, new object[] { 15, 0, 0, 0 });22            Console.WriteLine(result);23            Console.ReadKey();24        }25    }26}27using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;28using Microsoft.VisualStudio.TestPlatform.ObjectModel;29using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;30using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;31using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;32using System;33using System.Collections.Generic;34using System.IO;35using System.Linq;36using System.Reflection;37using System.Text;38using System.Threading.Tasks;39{40    {41        static void Main(string[] args)42        {43            Assembly assembly = Assembly.LoadFrom("Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.dll");44            Type type = assembly.GetType("Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TestRequestSender");45            var instance = Activator.CreateInstance(type, new object[] { });46            var method = type.GetMethod("CheckVersionWithTestHost", BindingFlags.NonPublic | BindingFlags.Instance);47            var result = method.Invoke(instance, new object[] { 14, 0, 0, 0 });48            Console.WriteLine(result);49            Console.ReadKey();50        }51    }52}53using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;CheckVersionWithTestHost
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;7using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;8using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;9using Microsoft.VisualStudio.TestPlatform.ObjectModel;10{11    {12        static void Main(string[] args)13        {14            ITestRequestSender requestSender = new TestRequestSender();15            requestSender.InitializeCommunication();16            Version testHostVersion = requestSender.CheckVersionWithTestHost();17            Console.WriteLine(testHostVersion.ToString());18            Console.ReadKey();19        }20    }21}CheckVersionWithTestHost
Using AI Code Generation
1{2    using System;3    using System.Collections.Generic;4    using System.Linq;5    using System.Text;6    using System.Threading.Tasks;7    using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;8    using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;9    using Microsoft.VisualStudio.TestPlatform.ObjectModel;10    using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;11    using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;12    using Microsoft.VisualStudio.TestPlatform.Utilities;13    using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;14    using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
