Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyExecutionManagerWithDataCollection.Initialize
ProxyExecutionManagerWithDataCollectionTests.cs
...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 }...
ProxyExecutionManagerWithDataCollection.cs
...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 }...
Check out the latest blogs from LambdaTest on this topic:
Most test automation tools just do test execution automation. Without test design involved in the whole test automation process, the test cases remain ad hoc and detect only simple bugs. This solution is just automation without real testing. In addition, test execution automation is very inefficient.
Sometimes, in our test code, we need to handle actions that apparently could not be done automatically. For example, some mouse actions such as context click, double click, drag and drop, mouse movements, and some special key down and key up actions. These specific actions could be crucial depending on the project context.
Traditional software testers must step up if they want to remain relevant in the Agile environment. Agile will most probably continue to be the leading form of the software development process in the coming years.
The events over the past few years have allowed the world to break the barriers of traditional ways of working. This has led to the emergence of a huge adoption of remote working and companies diversifying their workforce to a global reach. Even prior to this many organizations had already had operations and teams geographically dispersed.
Howdy testers! June has ended, and it’s time to give you a refresher on everything that happened at LambdaTest over the last month. We are thrilled to share that we are live with Cypress testing and that our very own LT Browser is free for all LambdaTest users. That’s not all, folks! We have also added a whole new range of browsers, devices & features to make testing more effortless than ever.
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!!