Best Vstest code snippet using Microsoft.TestPlatform.Protocol.DiscoveryRequestPayload
DesignModeClient.cs
Source:DesignModeClient.cs
...146 break;147 }148 case MessageType.StartDiscovery:149 {150 var discoveryPayload = this.dataSerializer.DeserializePayload<DiscoveryRequestPayload>(message); 151 this.StartDiscovery(discoveryPayload, testRequestManager);152 break;153 }154 case MessageType.GetTestRunnerProcessStartInfoForRunAll:155 case MessageType.GetTestRunnerProcessStartInfoForRunSelected:156 {157 var testRunPayload =158 this.communicationManager.DeserializePayload<TestRunRequestPayload>(159 message);160 this.StartTestRun(testRunPayload, testRequestManager, skipTestHostLaunch: true);161 break;162 }163 case MessageType.TestRunAllSourcesWithDefaultHost:164 case MessageType.TestRunSelectedTestCasesDefaultHost:165 {166 var testRunPayload =167 this.communicationManager.DeserializePayload<TestRunRequestPayload>(168 message);169 this.StartTestRun(testRunPayload, testRequestManager, skipTestHostLaunch: false);170 break;171 }172 case MessageType.CancelTestRun:173 {174 testRequestManager.CancelTestRun();175 break;176 }177 case MessageType.AbortTestRun:178 {179 testRequestManager.AbortTestRun();180 break;181 }182 case MessageType.CustomTestHostLaunchCallback:183 {184 this.onAckMessageReceived?.Invoke(message);185 break;186 }187 case MessageType.SessionEnd:188 {189 EqtTrace.Info("DesignModeClient: Session End message received from server. Closing the connection.");190 isSessionEnd = true;191 this.Dispose();192 break;193 }194 default:195 {196 EqtTrace.Info("DesignModeClient: Invalid Message received: {0}", message);197 break;198 }199 }200 }201 catch (Exception ex)202 {203 EqtTrace.Error("DesignModeClient: Error processing request: {0}", ex);204 isSessionEnd = true;205 this.Dispose();206 }207 }208 while (!isSessionEnd);209 }210 /// <summary>211 /// Send a custom host launch message to IDE212 /// </summary>213 /// <param name="testProcessStartInfo">214 /// The test Process Start Info.215 /// </param>216 /// <param name="cancellationToken">217 /// The cancellation token.218 /// </param>219 /// <returns>220 /// The <see cref="int"/>.221 /// </returns>222 public int LaunchCustomHost(TestProcessStartInfo testProcessStartInfo, CancellationToken cancellationToken)223 {224 lock (ackLockObject)225 {226 var waitHandle = new AutoResetEvent(false);227 Message ackMessage = null;228 this.onAckMessageReceived = (ackRawMessage) =>229 {230 ackMessage = ackRawMessage;231 waitHandle.Set();232 };233 this.communicationManager.SendMessage(MessageType.CustomTestHostLaunch, testProcessStartInfo);234 // LifeCycle of the TP through DesignModeClient is maintained by the IDEs or user-facing-clients like LUTs, who call TestPlatform235 // TP is handing over the control of launch to these IDEs and so, TP has to wait indefinite236 // Even if TP has a timeout here, there is no way TP can abort or stop the thread/task that is hung in IDE or LUT237 // Even if TP can abort the API somehow, TP is essentially putting IDEs or Clients in inconsistent state without having info on238 // Since the IDEs own user-UI-experience here, TP will let the custom host launch as much time as IDEs define it for their users239 WaitHandle.WaitAny(new WaitHandle[] { waitHandle, cancellationToken.WaitHandle });240 cancellationToken.ThrowTestPlatformExceptionIfCancellationRequested();241 this.onAckMessageReceived = null;242 var ackPayload = this.dataSerializer.DeserializePayload<CustomHostLaunchAckPayload>(ackMessage);243 if (ackPayload.HostProcessId > 0)244 {245 return ackPayload.HostProcessId;246 }247 else248 {249 throw new TestPlatformException(ackPayload.ErrorMessage);250 }251 }252 }253 /// <summary>254 /// Send the raw messages to IDE255 /// </summary>256 /// <param name="rawMessage"></param>257 public void SendRawMessage(string rawMessage)258 {259 this.communicationManager.SendRawMessage(rawMessage);260 }261 /// <inheritdoc />262 public void SendTestMessage(TestMessageLevel level, string message)263 {264 var payload = new TestMessagePayload { MessageLevel = level, Message = message };265 this.communicationManager.SendMessage(MessageType.TestMessage, payload);266 }267 private void StartTestRun(TestRunRequestPayload testRunPayload, ITestRequestManager testRequestManager, bool skipTestHostLaunch)268 {269 Task.Run(270 delegate271 {272 try273 {274 testRequestManager.ResetOptions();275 var customLauncher = skipTestHostLaunch ?276 DesignModeTestHostLauncherFactory.GetCustomHostLauncherForTestRun(this, testRunPayload) : null;277 testRequestManager.RunTests(testRunPayload, customLauncher, new DesignModeTestEventsRegistrar(this), this.protocolConfig);278 }279 catch (Exception ex)280 {281 EqtTrace.Error("DesignModeClient: Exception in StartTestRun: " + ex);282 var testMessagePayload = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = ex.ToString() };283 this.communicationManager.SendMessage(MessageType.TestMessage, testMessagePayload);284 var runCompletePayload = new TestRunCompletePayload()285 {286 TestRunCompleteArgs = new TestRunCompleteEventArgs(null, false, true, ex, null, TimeSpan.MinValue),287 LastRunTests = null288 };289 // Send run complete to translation layer290 this.communicationManager.SendMessage(MessageType.ExecutionComplete, runCompletePayload);291 }292 });293 }294 private void StartDiscovery(DiscoveryRequestPayload discoveryRequestPayload, ITestRequestManager testRequestManager)295 {296 Task.Run(297 delegate298 {299 try300 {301 testRequestManager.ResetOptions();302 testRequestManager.DiscoverTests(discoveryRequestPayload, new DesignModeTestEventsRegistrar(this), this.protocolConfig);303 }304 catch (Exception ex)305 {306 EqtTrace.Error("DesignModeClient: Exception in StartDiscovery: " + ex);307 var testMessagePayload = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = ex.ToString() };308 this.communicationManager.SendMessage(MessageType.TestMessage, testMessagePayload);...
DiscoveryRequestPayload.cs
Source:DiscoveryRequestPayload.cs
...4{5 using System.Collections.Generic;6 using System.Runtime.Serialization;7 /// <summary>8 /// Class used to define the DiscoveryRequestPayload sent by the Vstest.console translation layers into design mode9 /// </summary>10 public class DiscoveryRequestPayload11 {12 /// <summary>13 /// Settings used for the discovery request.14 /// </summary>15 [DataMember]16 public IEnumerable<string> Sources { get; set; }17 /// <summary>18 /// Settings used for the discovery request.19 /// </summary>20 [DataMember]21 public string RunSettings { get; set; }22 }23}...
DiscoveryRequestPayload
Using AI Code Generation
1using Microsoft.TestPlatform.CommunicationUtilities;2using Microsoft.TestPlatform.CommunicationUtilities.Interfaces;3using Microsoft.TestPlatform.CommunicationUtilities.ObjectModel;4using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;5using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;6using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;9using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;10using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.ClientProtocol;11using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.TesthostProtocol;12using System;13using System.Collections.Generic;14using System.Diagnostics;15using System.IO;16using System.Linq;17using System.Reflection;18using System.Threading;19using System.Threading.Tasks;20{21 {22 public static void Main(string[] args)23 {24 var dllPath = args[0];25 var source = args[1];26 var discoverySink = new DiscoverySink();27 {28 Sources = new List<string> { source },29 };30 {31 };32 var requestSender = new SocketClient();33 requestSender.Initialize(protocolConfig);34 var discoveryRequestHandler = new DiscoveryRequestHandler(requestSender, discoverySink);35 discoveryRequestHandler.DiscoverTests(discoveryRequestPayload);36 discoverySink.Done.WaitOne();37 foreach (var testCase in discoverySink.DiscoveredTests)38 {39 Console.WriteLine(testCase.FullyQualifiedName);40 }41 }42 }43 {44 public List<TestCase> DiscoveredTests { get; set; } = new List<TestCase>();45 public AutoResetEvent Done { get; set; } = new AutoResetEvent(false);46 public void HandleDiscoveredTests(IEnumerable<TestCase> discoveredTests)47 {48 DiscoveredTests.AddRange(discoveredTests);49 }50 public void HandleDiscoveryComplete(int totalTests, IEnumerable<TestCase> lastChunk, bool isAborted)51 {52 Done.Set();53 }54 public void HandleRawMessage(string rawMessage)55 {56 }57 }58}
DiscoveryRequestPayload
Using AI Code Generation
1using Microsoft.TestPlatform.CommunicationUtilities;2using Microsoft.TestPlatform.CommunicationUtilities.ObjectModel;3using System;4using System.Collections.Generic;5using System.IO;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 var discoveryRequestPayload = new DiscoveryRequestPayload();14 discoveryRequestPayload.DiscoverTests = true;15 discoveryRequestPayload.Sources = new List<string>() { "C:\\temp\\tests.dll" };16 discoveryRequestPayload.RunSettings = File.ReadAllText("C:\\temp\\runsettings.runsettings");17 var discoveryRequest = new DiscoveryRequest(discoveryRequestPayload);18 var discoveryRequestJson = discoveryRequest.ToJson();19 File.WriteAllText("C:\\temp\\discovery.json", discoveryRequestJson);20 }21 }22}23using Microsoft.TestPlatform.CommunicationUtilities;24using Microsoft.TestPlatform.CommunicationUtilities.ObjectModel;25using System;26using System.Collections.Generic;27using System.IO;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31{32 {33 static void Main(string[] args)34 {35 var json = File.ReadAllText("C:\\temp\\discovery.json");36 var discoveryRequest = DiscoveryRequest.FromJson(json);37 Console.WriteLine("DiscoverTests: " + discoveryRequest.DiscoverTests);38 Console.WriteLine("RunSettings: " + discoveryRequest.RunSettings);39 Console.WriteLine("Sources: " + string.Join(",", discoveryRequest.Sources));40 Console.ReadLine();41 }42 }43}
DiscoveryRequestPayload
Using AI Code Generation
1var discoveryRequestPayload = new DiscoveryRequestPayload();2discoveryRequestPayload.Sources = new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" };3discoveryRequestPayload.DiscoverySettings = new DiscoverySettings();4discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria = new DiscoveryCriteria();5discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.TestPlatformVersion = "15.0";6discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap = new Dictionary<string, IEnumerable<string>>();7discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter1", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });8discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter2", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });9discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter3", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });10discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter4", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });11discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter5", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });12discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter6", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });13discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter7", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });14discoveryRequestPayload.DiscoverySettings.DiscoveryCriteria.AdapterSourceMap.Add("TestAdapter8", new List<string>() { "C:\\Users\\abc\\Desktop\\TestProject1\\bin\\Debug\\TestProject1.dll" });
DiscoveryRequestPayload
Using AI Code Generation
1using Microsoft.TestPlatform.Protocol;2using Microsoft.TestPlatform.CommunicationUtilities;3using System;4{5 {6 static void Main(string[] args)7 {8 DiscoveryRequestPayload discoveryRequestPayload = new DiscoveryRequestPayload();9 discoveryRequestPayload.Sources = new List<string>() { "C:\\Users\\user\\Desktop\\test\\test.dll" };10 discoveryRequestPayload.DiscoveryCriteria = new DiscoveryCriteria();11 discoveryRequestPayload.DiscoveryCriteria.FrequencyOfRunStatsChangeEvent = 1;12 discoveryRequestPayload.DiscoveryCriteria.RunStatsChangeEventTimeout = 1;13 discoveryRequestPayload.DiscoveryCriteria.RunSettings = new TestPlatform.ObjectModel.RunSettings();14 discoveryRequestPayload.DiscoveryCriteria.RunSettings.SettingsXml = "<RunSettings><RunConfiguration><TargetFrameworkVersion>Framework45</TargetFrameworkVersion></RunConfiguration></RunSettings>";15 discoveryRequestPayload.DiscoveryCriteria.RunSettings.IsDesignMode = false;16 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration = new TestPlatform.ObjectModel.RunConfiguration();17 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetFrameworkVersion = TestPlatform.ObjectModel.FrameworkVersion.Framework45;18 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetPlatform = TestPlatform.ObjectModel.TargetPlatform.AnyCpu;19 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetDevice = TestPlatform.ObjectModel.TargetDevice.Default;20 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetFramework = TestPlatform.ObjectModel.Framework.DefaultFramework;21 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetPlatformArchitecture = TestPlatform.ObjectModel.TargetPlatformArchitecture.Default;22 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetRuntime = TestPlatform.ObjectModel.TargetRuntime.Default;23 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetFrameworkVersion = TestPlatform.ObjectModel.FrameworkVersion.Framework45;24 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetDevice = TestPlatform.ObjectModel.TargetDevice.Default;25 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetFramework = TestPlatform.ObjectModel.Framework.DefaultFramework;26 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetPlatformArchitecture = TestPlatform.ObjectModel.TargetPlatformArchitecture.Default;27 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetRuntime = TestPlatform.ObjectModel.TargetRuntime.Default;28 discoveryRequestPayload.DiscoveryCriteria.RunSettings.ActiveRunConfiguration.TargetFrameworkVersion = TestPlatform.ObjectModel.FrameworkVersion.Framework45;
DiscoveryRequestPayload
Using AI Code Generation
1public class Program {2 static void Main(string[] args) {3 var payload = new DiscoveryRequestPayload { RunSettings = @"<RunSettings></RunSettings>" };4 var payloadString = payload.ToXml();5 Console.WriteLine(payloadString);6 }7}8public class Program {9 static void Main(string[] args) {10 var payload = new DiscoveryRequestPayload { RunSettings = @"<RunSettings></RunSettings>" };11 var payloadString = payload.ToXml();12 Console.WriteLine(payloadString);13 }14}15public class Program {16 static void Main(string[] args) {17 var payload = new DiscoveryRequestPayload { RunSettings = @"<RunSettings></RunSettings>" };18 var payloadString = payload.ToXml();19 Console.WriteLine(payloadString);20 }21}22public class Program {23 static void Main(string[] args) {24 var payload = new DiscoveryRequestPayload { RunSettings = @"<RunSettings></RunSettings>" };25 var payloadString = payload.ToXml();26 Console.WriteLine(payloadString);27 }28}29public class Program {30 static void Main(string[] args) {31 var payload = new DiscoveryRequestPayload { RunSettings = @"<RunSettings></RunSettings>" };32 var payloadString = payload.ToXml();33 Console.WriteLine(payloadString);34 }35}36public class Program {37 static void Main(string[] args) {38 var payload = new DiscoveryRequestPayload { RunSettings = @"<RunSettings></RunSettings>" };39 var payloadString = payload.ToXml();40 Console.WriteLine(payloadString);41 }42}43public class Program {44 static void Main(string[] args) {45 var payload = new DiscoveryRequestPayload { RunSettings = @"<RunSettings></RunSettings>" };46 var payloadString = payload.ToXml();47 Console.WriteLine(payloadString);48 }49}
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!!