How to use Initialize method of Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyExecutionManagerWithDataCollection class

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyExecutionManagerWithDataCollection.Initialize

ProxyExecutionManagerWithDataCollectionTests.cs

Source:ProxyExecutionManagerWithDataCollectionTests.cs Github

copy

Full Screen

...31 private Mock<IDataSerializer> mockDataSerializer;32 private Mock<IRequestData> mockRequestData;33 private Mock<IMetricsCollection> mockMetricsCollection;34 private Mock<IFileHelper> mockFileHelper;35 [TestInitialize]36 public void TestInit()37 {38 this.mockTestHostManager = new Mock<ITestRuntimeProvider>();39 this.mockRequestSender = new Mock<ITestRequestSender>();40 this.mockDataSerializer = new Mock<IDataSerializer>();41 this.mockRequestData = new Mock<IRequestData>();42 this.mockMetricsCollection = new Mock<IMetricsCollection>();43 this.mockFileHelper = new Mock<IFileHelper>();44 this.mockRequestData.Setup(rd => rd.MetricsCollection).Returns(this.mockMetricsCollection.Object);45 this.testExecutionManager = new ProxyExecutionManager(this.mockRequestData.Object, this.mockRequestSender.Object, this.mockTestHostManager.Object, this.mockDataSerializer.Object, this.mockFileHelper.Object);46 this.mockDataCollectionManager = new Mock<IProxyDataCollectionManager>();47 this.mockProcessHelper = new Mock<IProcessHelper>();48 this.proxyExecutionManager = new ProxyExecutionManagerWithDataCollection(this.mockRequestData.Object, this.mockRequestSender.Object, this.mockTestHostManager.Object, this.mockDataCollectionManager.Object);49 }50 [TestMethod]51 public void InitializeShouldInitializeDataCollectionProcessIfDataCollectionIsEnabled()52 {53 this.proxyExecutionManager.Initialize(false);54 mockDataCollectionManager.Verify(dc => dc.BeforeTestRunStart(It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<ITestMessageEventHandler>()), Times.Once);55 }56 [TestMethod]57 public void InitializeShouldThrowExceptionIfThrownByDataCollectionManager()58 {59 this.mockDataCollectionManager.Setup(x => x.Initialize()).Throws<Exception>();60 Assert.ThrowsException<Exception>(() =>61 {62 this.proxyExecutionManager.Initialize(false);63 });64 }65 [TestMethod]66 public void InitializeShouldCallAfterTestRunIfExceptionIsThrownWhileCreatingDataCollectionProcess()67 {68 mockDataCollectionManager.Setup(dc => dc.BeforeTestRunStart(It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<ITestMessageEventHandler>())).Throws(new Exception("MyException"));69 Assert.ThrowsException<Exception>(() =>70 {71 this.proxyExecutionManager.Initialize(false);72 });73 mockDataCollectionManager.Verify(dc => dc.BeforeTestRunStart(It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<ITestMessageEventHandler>()), Times.Once);74 mockDataCollectionManager.Verify(dc => dc.AfterTestRunEnd(It.IsAny<bool>(), It.IsAny<ITestMessageEventHandler>()), Times.Once);75 }76 [TestMethod]77 public void InitializeShouldSaveExceptionMessagesIfThrownByDataCollectionProcess()78 {79 var mockRequestSender = new Mock<IDataCollectionRequestSender>();80 var testSources = new List<string>() { "abc.dll", "efg.dll" };81 mockRequestSender.Setup(x => x.SendBeforeTestRunStartAndGetResult(string.Empty, testSources, It.IsAny<bool>(), It.IsAny<ITestMessageEventHandler>())).Throws(new Exception("MyException"));82 mockRequestSender.Setup(x => x.WaitForRequestHandlerConnection(It.IsAny<int>())).Returns(true);83 var mockDataCollectionLauncher = new Mock<IDataCollectionLauncher>();84 var proxyDataCollectonManager = new ProxyDataCollectionManager(this.mockRequestData.Object, string.Empty, testSources, mockRequestSender.Object, this.mockProcessHelper.Object, mockDataCollectionLauncher.Object);85 var proxyExecutionManager = new ProxyExecutionManagerWithDataCollection(this.mockRequestData.Object, this.mockRequestSender.Object, this.mockTestHostManager.Object, proxyDataCollectonManager);86 proxyExecutionManager.Initialize(false);87 Assert.IsNotNull(proxyExecutionManager.DataCollectionRunEventsHandler.Messages);88 Assert.AreEqual(TestMessageLevel.Error, proxyExecutionManager.DataCollectionRunEventsHandler.Messages[0].Item1);89 StringAssert.Contains(proxyExecutionManager.DataCollectionRunEventsHandler.Messages[0].Item2, "MyException");90 }91 [TestMethod]92 public void UpdateTestProcessStartInfoShouldUpdateDataCollectionPortArg()93 {94 this.mockDataCollectionManager.Setup(x => x.BeforeTestRunStart(It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<ITestMessageEventHandler>())).Returns(DataCollectionParameters.CreateDefaultParameterInstance());95 var testProcessStartInfo = new TestProcessStartInfo();96 testProcessStartInfo.Arguments = string.Empty;97 var proxyExecutionManager = new TestableProxyExecutionManagerWithDataCollection(this.mockRequestSender.Object, this.mockTestHostManager.Object, this.mockDataCollectionManager.Object);98 proxyExecutionManager.UpdateTestProcessStartInfoWrapper(testProcessStartInfo);99 Assert.IsTrue(testProcessStartInfo.Arguments.Contains("--datacollectionport 0"));100 }...

Full Screen

Full Screen

ProxyExecutionManagerWithDataCollection.cs

Source:ProxyExecutionManagerWithDataCollection.cs Github

copy

Full Screen

...19 private IDictionary<string, string> dataCollectionEnvironmentVariables;20 private int dataCollectionPort;21 private IRequestData requestData;22 /// <summary>23 /// Initializes a new instance of the <see cref="ProxyExecutionManagerWithDataCollection"/> class. 24 /// </summary>25 /// <param name="requestSender">26 /// Test request sender instance.27 /// </param>28 /// <param name="testHostManager">29 /// Test host manager for this operation.30 /// </param>31 /// <param name="proxyDataCollectionManager">32 /// The proxy Data Collection Manager.33 /// </param>34 /// <param name="requestData">35 /// The request data for providing execution services and data.36 /// </param>37 public ProxyExecutionManagerWithDataCollection(IRequestData requestData, ITestRequestSender requestSender, ITestRuntimeProvider testHostManager, IProxyDataCollectionManager proxyDataCollectionManager)38 : base(requestData, requestSender, testHostManager)39 {40 this.ProxyDataCollectionManager = proxyDataCollectionManager;41 this.DataCollectionRunEventsHandler = new DataCollectionRunEventsHandler();42 this.requestData = requestData;43 this.dataCollectionEnvironmentVariables = new Dictionary<string, string>();44 }45 /// <summary>46 /// Gets the data collection run events handler.47 /// </summary>48 internal DataCollectionRunEventsHandler DataCollectionRunEventsHandler49 {50 get; private set;51 }52 /// <summary>53 /// Gets the proxy data collection manager.54 /// </summary>55 internal IProxyDataCollectionManager ProxyDataCollectionManager56 {57 get; private set;58 }59 /// <summary>60 /// Ensure that the Execution component of engine is ready for execution usually by loading extensions.61 /// </summary>62 public override void Initialize()63 {64 this.ProxyDataCollectionManager.Initialize();65 try66 {67 var dataCollectionParameters = this.ProxyDataCollectionManager.BeforeTestRunStart(68 resetDataCollectors: true,69 isRunStartingNow: true,70 runEventsHandler: this.DataCollectionRunEventsHandler);71 if (dataCollectionParameters != null)72 {73 this.dataCollectionEnvironmentVariables = dataCollectionParameters.EnvironmentVariables;74 this.dataCollectionPort = dataCollectionParameters.DataCollectionEventsPort;75 }76 }77 catch (Exception)78 {79 // On failure in calling BeforeTestRunStart, call AfterTestRunEnd to end DataCollectionProcess80 this.ProxyDataCollectionManager.AfterTestRunEnd(isCanceled: true, runEventsHandler: this.DataCollectionRunEventsHandler);81 throw;82 }83 base.Initialize();84 }85 /// <summary>86 /// Starts the test run87 /// </summary>88 /// <param name="testRunCriteria"> The settings/options for the test run. </param>89 /// <param name="eventHandler"> EventHandler for handling execution events from Engine. </param>90 /// <returns> The process id of the runner executing tests. </returns>91 public override int StartTestRun(TestRunCriteria testRunCriteria, ITestRunEventsHandler eventHandler)92 {93 var currentEventHandler = eventHandler;94 if (this.ProxyDataCollectionManager != null)95 {96 currentEventHandler = new DataCollectionTestRunEventsHandler(eventHandler, this.ProxyDataCollectionManager);97 }98 // Log all the messages that are reported while initializing DataCollectionClient99 if (this.DataCollectionRunEventsHandler.Messages.Count > 0)100 {101 foreach (var message in this.DataCollectionRunEventsHandler.Messages)102 {103 currentEventHandler.HandleLogMessage(message.Item1, message.Item2);104 }105 this.DataCollectionRunEventsHandler.Messages.Clear();106 }107 return base.StartTestRun(testRunCriteria, currentEventHandler);108 }109 /// <inheritdoc/>110 public override void Cancel()111 {112 try113 {114 this.ProxyDataCollectionManager.AfterTestRunEnd(isCanceled: true, runEventsHandler: this.DataCollectionRunEventsHandler);115 }116 finally117 {118 base.Cancel();119 }120 }121 /// <inheritdoc />122 protected override TestProcessStartInfo UpdateTestProcessStartInfo(TestProcessStartInfo testProcessStartInfo)123 {124 if (testProcessStartInfo.EnvironmentVariables == null)125 {126 testProcessStartInfo.EnvironmentVariables = this.dataCollectionEnvironmentVariables;127 }128 else129 {130 foreach (var kvp in this.dataCollectionEnvironmentVariables)131 {132 testProcessStartInfo.EnvironmentVariables[kvp.Key] = kvp.Value;133 }134 }135 // Update Telemetry Opt in status because by default in Test Host Telemetry is opted out136 var telemetryOptedIn = this.requestData.IsTelemetryOptedIn ? "true" : "false";137 testProcessStartInfo.Arguments += " --datacollectionport " + this.dataCollectionPort138 + " --telemetryoptedin " + telemetryOptedIn;139 return testProcessStartInfo;140 }141 }142 /// <summary>143 /// Handles Log events and stores them in list. Messages in the list will be logged after test execution begins.144 /// </summary>145 internal class DataCollectionRunEventsHandler : ITestMessageEventHandler146 {147 /// <summary>148 /// Initializes a new instance of the <see cref="DataCollectionRunEventsHandler"/> class. 149 /// </summary>150 public DataCollectionRunEventsHandler()151 {152 this.Messages = new List<Tuple<TestMessageLevel, string>>();153 }154 /// <summary>155 /// Gets the cached messages.156 /// </summary>157 public List<Tuple<TestMessageLevel, string>> Messages { get; private set; }158 /// <inheritdoc />159 public void HandleLogMessage(TestMessageLevel level, string message)160 {161 this.Messages.Add(new Tuple<TestMessageLevel, string>(level, message));162 }...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Vstest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful