static void HandShakeWithVsTestConsole() { // HandShake with vstest.console Console.WriteLine("=========== HandShake with vstest.console =========="); var message = communicationManager.ReceiveMessage(); if (message.MessageType == MessageType.SessionConnected) { // Version Check communicationManager.SendMessage(MessageType.VersionCheck); message = communicationManager.ReceiveMessage(); if (message.MessageType == MessageType.VersionCheck) { var version = JsonDataSerializer.Instance.DeserializePayload <int>(message); var success = version == 1; Console.WriteLine("Version Success: {0}", success); } } }
public static int Main(string[] args) { if (args == null || args.Length < 1) { Console.WriteLine("Please provide appropriate arguments. Arguments can be passed as following:"); Console.WriteLine("Microsoft.TestPlatform.Protocol.exe --testassembly:\"[assemblyPath]\" --operation:\"[RunAll|RunSelected|Discovery|DebugAll|DebugSelected]\" --framework:Framework45 --testadapterpath:\"[path]\""); Console.WriteLine("or Microsoft.TestPlatform.Protocol.exe -a:\"[assemblyPath]\" -o:\"[RunAll|RunSelected|Discovery|DebugAll|DebugSelected]\" -f:\".NETFramework,Version=v4.5.2\" -p:\"[path]\" \n"); return(1); } var executingLocation = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); // Default values var testAssembly = Path.Combine(executingLocation, "UnitTestProject.dll"); string testadapterPath = null; string operation = "Discovery"; string framework = String.Empty; var separator = new char[] { ':' }; foreach (var arg in args) { if (arg.StartsWith("-p:") || arg.StartsWith("--testadapterpath:")) { testadapterPath = arg.Split(separator, 2)[1]; } else if (arg.StartsWith("-a:") || arg.StartsWith("--testassembly:")) { testAssembly = arg.Split(separator, 2)[1]; } else if (arg.StartsWith("-o:") || arg.StartsWith("--operation:")) { operation = arg.Split(separator, 2)[1]; } else if (arg.StartsWith("-f:") || arg.StartsWith("--framework:")) { framework = arg.Split(separator, 2)[1]; } } if (framework.Equals(DesktopFramework, StringComparison.OrdinalIgnoreCase) && String.IsNullOrEmpty(testadapterPath)) { Console.WriteLine("Please specify the test adapter path for running tests targeting net45 plus frameworks"); return(0); } Console.WriteLine("TestAdapter Path : {0}", testadapterPath); Console.WriteLine("TestAssembly Path : {0}", testAssembly); Console.WriteLine("Operation : {0}", operation); var processManager = new RunnerProcessManager(); communicationManager = new SocketCommunicationManager(); // Start the server var port = communicationManager.HostServer(); // Start runner exe and wait for the connection string parentProcessIdArgs = string.Format(CultureInfo.InvariantCulture, PARENT_PROCESSID_ARGUMENT, Process.GetCurrentProcess().Id); string portArgs = string.Format(CultureInfo.InvariantCulture, PORT_ARGUMENT, port); processManager.StartProcess(new string[2] { parentProcessIdArgs, portArgs }); communicationManager.AcceptClientAsync().Wait(); communicationManager.WaitForClientConnection(Timeout.Infinite); HandShakeWithVsTestConsole(); string settingsXml = null; if (framework.Contains(DesktopFramework)) { settingsXml = @"<?xml version=""1.0"" encoding=""utf-8""?> <RunSettings> <RunConfiguration> <TargetFrameworkVersion>" + framework + @"</TargetFrameworkVersion> </RunConfiguration> </RunSettings>"; } // Intialize the extensions if (testadapterPath != null) { communicationManager.SendMessage(MessageType.ExtensionsInitialize, new List <string>() { testadapterPath }); } // Actual operation dynamic discoveredTestCases; switch (operation.ToLower()) { case "discovery": discoveredTestCases = DiscoverTests(testadapterPath, testAssembly, settingsXml); break; case "runselected": discoveredTestCases = DiscoverTests(testadapterPath, testAssembly, settingsXml); RunSelectedTests(discoveredTestCases, settingsXml); break; case "debugall": DebugAllTests(new List <string>() { testAssembly }, settingsXml); break; case "debugselected": discoveredTestCases = DiscoverTests(testadapterPath, testAssembly, settingsXml); DebugSelectedTests(discoveredTestCases, settingsXml); break; case "runall": default: RunAllTests(new List <string>() { testAssembly }, settingsXml); break; } return(0); }