Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.InProcessProxyDiscoveryManager
InProcessProxyDiscoveryManagerTests.cs
Source:InProcessProxyDiscoveryManagerTests.cs
...15 using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;16 using Microsoft.VisualStudio.TestTools.UnitTesting;17 using Moq;18 [TestClass]19 public class InProcessProxyDiscoveryManagerTests20 {21 private Mock<ITestHostManagerFactory> mockTestHostManagerFactory;22 private InProcessProxyDiscoveryManager inProcessProxyDiscoveryManager;23 private Mock<IDiscoveryManager> mockDiscoveryManager;24 private Mock<ITestRuntimeProvider> mockTestHostManager;25 public InProcessProxyDiscoveryManagerTests()26 {27 this.mockTestHostManagerFactory = new Mock<ITestHostManagerFactory>();28 this.mockDiscoveryManager = new Mock<IDiscoveryManager>();29 this.mockTestHostManager = new Mock<ITestRuntimeProvider>();30 this.mockTestHostManagerFactory.Setup(o => o.GetDiscoveryManager()).Returns(this.mockDiscoveryManager.Object);31 this.inProcessProxyDiscoveryManager = new InProcessProxyDiscoveryManager(this.mockTestHostManager.Object, this.mockTestHostManagerFactory.Object);32 }33 [TestCleanup]34 public void TestCleanup()35 {36 this.mockDiscoveryManager = null;37 this.mockTestHostManagerFactory = null;38 this.inProcessProxyDiscoveryManager = null;39 this.mockTestHostManager = null;40 }41 [TestMethod]42 public void DiscoverTestsShouldCallInitialize()43 {44 var manualResetEvent = new ManualResetEvent(false);45 this.mockDiscoveryManager.Setup(o => o.Initialize(Enumerable.Empty<string>(), null)).Callback(46 () => manualResetEvent.Set());47 var discoveryCriteria = new DiscoveryCriteria(new[] { "test.dll" }, 1, string.Empty);48 this.inProcessProxyDiscoveryManager.DiscoverTests(discoveryCriteria, null);49 Assert.IsTrue(manualResetEvent.WaitOne(5000), "DiscoverTests should call Initialize");50 }51 [TestMethod]52 public void DiscoverTestsShouldUpdateTestPluginCacheWithExtensionsReturnByTestHost()53 {54 var manualResetEvent = new ManualResetEvent(false);55 this.mockDiscoveryManager.Setup(o => o.Initialize(Enumerable.Empty<string>(), null)).Callback(56 () => manualResetEvent.Set());57 var path = Path.Combine(Path.GetTempPath(), "DiscoveryDummy.dll");58 this.mockTestHostManager.Setup(o => o.GetTestPlatformExtensions(It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>())).Returns(new List<string> { path });59 var expectedResult = TestPluginCache.Instance.GetExtensionPaths(string.Empty);60 expectedResult.Add(path);61 var discoveryCriteria = new DiscoveryCriteria(new[] { "test.dll" }, 1, string.Empty);62 this.inProcessProxyDiscoveryManager.DiscoverTests(discoveryCriteria, null);63 Assert.IsTrue(manualResetEvent.WaitOne(5000), "DiscoverTests should call Initialize");64 CollectionAssert.AreEquivalent(expectedResult, TestPluginCache.Instance.GetExtensionPaths(string.Empty));65 }66 [TestMethod]67 public void DiscoverTestsShouldCallDiscoveryManagerDiscoverTests()68 {69 var discoveryCriteria = new DiscoveryCriteria(new[] { "test.dll" }, 1, string.Empty);70 var mockTestDiscoveryEventsHandler = new Mock<ITestDiscoveryEventsHandler2>();71 var manualResetEvent = new ManualResetEvent(false);72 this.mockDiscoveryManager.Setup(o => o.DiscoverTests(discoveryCriteria, mockTestDiscoveryEventsHandler.Object)).Callback(73 () => manualResetEvent.Set());74 this.inProcessProxyDiscoveryManager.DiscoverTests(discoveryCriteria, mockTestDiscoveryEventsHandler.Object);75 Assert.IsTrue(manualResetEvent.WaitOne(5000), "IDiscoveryManager.DiscoverTests should get called");76 }77 [TestMethod]78 public void DiscoverTestsShouldCatchExceptionAndCallHandleDiscoveryComplete()79 {80 var discoveryCriteria = new DiscoveryCriteria(new[] { "test.dll" }, 1, string.Empty);81 var mockTestDiscoveryEventsHandler = new Mock<ITestDiscoveryEventsHandler2>();82 var manualResetEvent = new ManualResetEvent(false);83 this.mockDiscoveryManager.Setup(o => o.DiscoverTests(discoveryCriteria, mockTestDiscoveryEventsHandler.Object)).Callback(84 () => throw new Exception());85 mockTestDiscoveryEventsHandler.Setup(o => o.HandleDiscoveryComplete(It.IsAny<DiscoveryCompleteEventArgs>(), It.IsAny<IEnumerable<TestCase>>())).Callback(86 () => manualResetEvent.Set());87 this.inProcessProxyDiscoveryManager.DiscoverTests(discoveryCriteria, mockTestDiscoveryEventsHandler.Object);88 Assert.IsTrue(manualResetEvent.WaitOne(5000), "ITestDiscoveryEventsHandler.HandleDiscoveryComplete should get called");89 }90 [TestMethod]91 public void AbortShouldCallDiscoveryManagerAbort()92 {93 var manualResetEvent = new ManualResetEvent(false);94 this.mockDiscoveryManager.Setup(o => o.Abort()).Callback(95 () => manualResetEvent.Set());96 this.inProcessProxyDiscoveryManager.Abort();97 Assert.IsTrue(manualResetEvent.WaitOne(5000), "IDiscoveryManager.Abort should get called");98 }99 [TestMethod]100 public void DiscoverTestRunShouldAllowRuntimeProviderToUpdateAdapterSource()101 {102 var inputSources = new List<string> { "test.dll" };103 var discoveryCriteria = new DiscoveryCriteria(inputSources, 1, string.Empty);104 var mockTestDiscoveryEventsHandler = new Mock<ITestDiscoveryEventsHandler2>();105 var manualResetEvent = new ManualResetEvent(false);106 this.mockTestHostManager.Setup(hm => hm.GetTestSources(discoveryCriteria.Sources)).Returns(discoveryCriteria.Sources);107 this.mockDiscoveryManager.Setup(o => o.DiscoverTests(discoveryCriteria, mockTestDiscoveryEventsHandler.Object)).Callback(108 () => manualResetEvent.Set());109 this.inProcessProxyDiscoveryManager = new InProcessProxyDiscoveryManager(this.mockTestHostManager.Object, this.mockTestHostManagerFactory.Object);110 this.inProcessProxyDiscoveryManager.DiscoverTests(discoveryCriteria, mockTestDiscoveryEventsHandler.Object);111 Assert.IsTrue(manualResetEvent.WaitOne(5000), "IDiscoveryManager.DiscoverTests should get called");112 this.mockTestHostManager.Verify(hm => hm.GetTestSources(inputSources), Times.Once);113 }114 [TestMethod]115 public void DiscoverTestRunShouldUpdateTestSourcesIfSourceDiffersFromTestHostManagerSource()116 {117 var actualSources = new List<string> { "actualSource.dll" };118 var inputSource = new List<string> { "inputPackage.appxrecipe" };119 var discoveryCriteria = new DiscoveryCriteria(inputSource, 1, string.Empty);120 var mockTestDiscoveryEventsHandler = new Mock<ITestDiscoveryEventsHandler2>();121 var manualResetEvent = new ManualResetEvent(false);122 this.mockTestHostManager.Setup(hm => hm.GetTestSources(discoveryCriteria.Sources)).Returns(actualSources);123 this.mockDiscoveryManager.Setup(o => o.DiscoverTests(discoveryCriteria, mockTestDiscoveryEventsHandler.Object)).Callback(...
InProcessProxyDiscoveryManager.cs
Source:InProcessProxyDiscoveryManager.cs
...12 using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;13 using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.TesthostProtocol;14 using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;15 using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;16 internal class InProcessProxyDiscoveryManager : IProxyDiscoveryManager17 {18 private ITestHostManagerFactory testHostManagerFactory;19 private IDiscoveryManager discoveryManager;20 private ITestRuntimeProvider testHostManager;21 public bool IsInitialized { get; private set; } = false;22 /// <summary>23 /// Initializes a new instance of the <see cref="InProcessProxyDiscoveryManager"/> class.24 /// </summary>25 /// <param name="testHostManager">26 /// The test Host Manager.27 /// </param>28 /// <param name="testHostManagerFactory">29 /// Manager factory30 /// </param>31 public InProcessProxyDiscoveryManager(ITestRuntimeProvider testHostManager, ITestHostManagerFactory testHostManagerFactory)32 {33 this.testHostManager = testHostManager;34 this.testHostManagerFactory = testHostManagerFactory;35 this.discoveryManager = this.testHostManagerFactory.GetDiscoveryManager();36 }37 /// <summary>38 /// Initializes test discovery.39 /// <param name="skipDefaultAdapters">Skip default adapters flag.</param>40 /// </summary>41 public void Initialize(bool skipDefaultAdapters)42 {43 }44 /// <summary>45 /// Discovers tests46 /// </summary>47 /// <param name="discoveryCriteria">Settings, parameters for the discovery request</param>48 /// <param name="eventHandler">EventHandler for handling discovery events from Engine</param>49 public void DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)50 {51 Task.Run(() =>52 {53 try54 {55 // Initialize extension before discovery56 this.InitializeExtensions(discoveryCriteria.Sources);57 discoveryCriteria.UpdateDiscoveryCriteria(testHostManager);58 this.discoveryManager.DiscoverTests(discoveryCriteria, eventHandler);59 }60 catch (Exception exception)61 {62 EqtTrace.Error("InProcessProxyDiscoveryManager.DiscoverTests: Failed to discover tests: {0}", exception);63 // Send a discovery complete to caller.64 eventHandler.HandleLogMessage(TestMessageLevel.Error, exception.ToString());65 var discoveryCompeleteEventsArg = new DiscoveryCompleteEventArgs(-1, true);66 eventHandler.HandleDiscoveryComplete(discoveryCompeleteEventsArg, Enumerable.Empty<TestCase>());67 }68 });69 }70 /// <summary>71 /// Closes the current test operation.72 /// This function is of no use in this context as we are not creating any testhost73 /// </summary>74 public void Close()75 {76 }...
InProcessProxyDiscoveryManager
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10{11 {12 static void Main(string[] args)13 {14 var proxyDiscoveryManager = new InProcessProxyDiscoveryManager();15 var discoveryCriteria = new DiscoveryCriteria(new List<string> { "C:\\temp\\test.dll" }, 1, null);16 var discoveryEvents = new DiscoveryEvents();17 var discoveryRequest = proxyDiscoveryManager.CreateDiscoveryRequest(discoveryCriteria, discoveryEvents);18 discoveryRequest.DiscoverAsync().Wait();19 Console.ReadLine();20 }21 }22}23using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;24using Microsoft.VisualStudio.TestPlatform.ObjectModel;25using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;26using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32{33 {34 static void Main(string[] args)35 {36 var proxyExecutionManager = new InProcessProxyExecutionManager();37 var executionCriteria = new ExecutionCriteria(new List<string> { "C:\\temp\\test.dll" }, 1, null, null);38 var executionEvents = new ExecutionEvents();39 var executionRequest = proxyExecutionManager.CreateTestRunRequest(executionCriteria, executionEvents);40 executionRequest.ExecuteAsync().Wait();41 Console.ReadLine();42 }43 }44}45using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;46using Microsoft.VisualStudio.TestPlatform.ObjectModel;47using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;48using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Threading.Tasks;54{55 {56 static void Main(string[] args)57 {58 var proxyExecutionManager = new InProcessProxyExecutionManager();59 var executionCriteria = new ExecutionCriteria(new List<string> { "C:\\temp\\test.dll" }, 1
InProcessProxyDiscoveryManager
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;2using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Discovery;3using Microsoft.VisualStudio.TestPlatform.ObjectModel;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;5using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11{12 {13 static void Main(string[] args)14 {15 var discoveryManager = new InProcessProxyDiscoveryManager();16 var discoveryCriteria = new DiscoveryCriteria(new List<string> { "1.csproj" }, 1, string.Empty);17 var discoveryEventsHandler = new DiscoveryEventsHandler();18 discoveryManager.DiscoverTests(discoveryCriteria, discoveryEventsHandler);19 Console.ReadKey();20 }21 }22 {23 public void HandleDiscoveryComplete(int totalTests, IEnumerable<TestCase> lastChunk, bool isAborted, IEnumerable<TestCase> discoveredTests, ITestRunStatistics testRunStatistics)24 {25 Console.WriteLine("HandleDiscoveryComplete");26 }27 public void HandleDiscoveredTests(IEnumerable<TestCase> discoveredTests)28 {29 Console.WriteLine("HandleDiscoveredTests");30 }31 public void HandleLogMessage(TestMessageLevel level, string message)32 {33 Console.WriteLine("HandleLogMessage");34 }35 public void HandleRawMessage(string rawMessage)36 {37 Console.WriteLine("HandleRawMessage");38 }39 public void HandleTestRunStatsChange(TestRunChangedEventArgs testRunChangedArgs)40 {41 Console.WriteLine("HandleTestRunStatsChange");42 }43 }44}45The ITestDiscoveryManager interface has a DiscoverTests() method that
InProcessProxyDiscoveryManager
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;2using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Discovery;3using Microsoft.VisualStudio.TestPlatform.ObjectModel;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;5using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11{12 {13 static void Main(string[] args)14 {15 var discoveryRequest = new DiscoveryRequest();16 var discoveryManager = new InProcessProxyDiscoveryManager();17 var testPlatform = new TestPlatform();18 var discoveryCriteria = new DiscoveryCriteria();19 discoveryCriteria.DiscoverySettings = new Dictionary<string, object>();20 discoveryCriteria.DiscoverySettings.Add("TargetPlatform", "x86");21 discoveryCriteria.DiscoverySettings.Add("TargetFrameworkVersion", "Framework45");22 discoveryCriteria.DiscoverySettings.Add("TargetFramework", "Framework45");23 discoveryCriteria.DiscoverySettings.Add("DesignMode", "False");24 discoveryCriteria.DiscoverySettings.Add("DisableAppDomain", "False");25 discoveryCriteria.DiscoverySettings.Add("SkipDefaultAdapters", "False");26 discoveryCriteria.DiscoverySettings.Add("IgnoreTestAdapterPathExtensions", "False");27 discoveryCriteria.DiscoverySettings.Add("IsTelemetryOptedIn", "False");28 discoveryCriteria.DiscoverySettings.Add("RunSettings", "<RunSettings><RunConfiguration><TargetFrameworkVersion>Framework45</TargetFrameworkVersion><TargetPlatform>x86</TargetPlatform></RunConfiguration></RunSettings>");29 discoveryCriteria.Sources = new List<string>();30 discoveryCriteria.Sources.Add(@"C:\Users\user\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.dll");31 discoveryManager.DiscoveryComplete += DiscoveryComplete;32 discoveryManager.DiscoveryMessage += DiscoveryMessage;33 discoveryManager.StartDiscovery(discoveryRequest, discoveryCriteria);34 Console.ReadLine();35 }36 private static void DiscoveryMessage(object sender, DiscoveryMessageEventArgs e)37 {38 Console.WriteLine("DiscoveryMessage");39 }40 private static void DiscoveryComplete(object sender, DiscoveryCompleteEventArgs e)41 {
InProcessProxyDiscoveryManager
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;2using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;4using Microsoft.VisualStudio.TestPlatform.ObjectModel;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10{11 {12 static void Main(string[] args)13 {14 IProxyDiscoveryManager discoveryManager = new InProcessProxyDiscoveryManager();15 DiscoveryRequest discoveryRequest = new DiscoveryRequest(@"C:\Path\To\My\Assembly.dll", null, null);16 discoveryManager.StartDiscovery(discoveryRequest);17 discoveryManager.WaitForDiscoveryCompletion();18 IEnumerable<TestCase> discoveredTests = discoveryManager.GetDiscoveredTests();19 discoveryManager.Dispose();20 }21 }22}23using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;24using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;25using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;26using Microsoft.VisualStudio.TestPlatform.ObjectModel;27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32{33 {34 static void Main(string[] args)35 {36 IProxyExecutionManager executionManager = new InProcessProxyExecutionManager();37 DiscoveryRequest discoveryRequest = new DiscoveryRequest(@"C:\Path\To\My\Assembly.dll", null, null);38 executionManager.StartDiscovery(discoveryRequest);39 executionManager.WaitForDiscoveryCompletion();40 IEnumerable<TestCase> discoveredTests = executionManager.GetDiscoveredTests();41 TestRunRequest testRunRequest = new TestRunRequest(discoveredTests, null, null, null, false);42 executionManager.StartTestRun(testRunRequest);43 executionManager.WaitForTestRunCompletion();
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!!