How to use CreateDiscoveryRequest method of Microsoft.VisualStudio.TestPlatform.Client.TestPlatform class

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Client.TestPlatform.CreateDiscoveryRequest

RunSpecificTestsArgumentProcessorTests.cs

Source:RunSpecificTestsArgumentProcessorTests.cs Github

copy

Full Screen

...107 var mockTestRunRequest = new Mock<ITestRunRequest>();108 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();109 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new TestPlatformException("DummyTestPlatformException"));110 mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Returns(mockTestRunRequest.Object);111 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);112 this.ResetAndAddSourceToCommandLineOptions();113 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);114 var executor = GetExecutor(testRequestManager);115 ArgumentProcessorResult argumentProcessorResult = executor.Execute();116 Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult);117 }118 [TestMethod]119 public void ExecutorExecuteShouldCatchInvalidOperationExceptionThrownDuringDiscoveryAndReturnFail()120 {121 var mockTestPlatform = new Mock<ITestPlatform>();122 var mockTestRunRequest = new Mock<ITestRunRequest>();123 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();124 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new InvalidOperationException("DummyInvalidOperationException"));125 mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Returns(mockTestRunRequest.Object);126 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);127 this.ResetAndAddSourceToCommandLineOptions();128 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);129 var executor = GetExecutor(testRequestManager);130 ArgumentProcessorResult argumentProcessorResult = executor.Execute();131 Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult);132 }133 [TestMethod]134 public void ExecutorExecuteShouldCatchSettingsExceptionThrownDuringDiscoveryAndReturnFail()135 {136 var mockTestPlatform = new Mock<ITestPlatform>();137 var mockTestRunRequest = new Mock<ITestRunRequest>();138 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();139 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new SettingsException("DummySettingsException"));140 mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Returns(mockTestRunRequest.Object);141 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);142 this.ResetAndAddSourceToCommandLineOptions();143 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);144 var executor = GetExecutor(testRequestManager);145 ArgumentProcessorResult argumentProcessorResult = executor.Execute();146 Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult);147 }148 [TestMethod]149 public void ExecutorExecuteShouldCatchTestPlatformExceptionThrownDuringExecutionAndReturnFail()150 {151 var mockTestPlatform = new Mock<ITestPlatform>();152 var mockTestRunRequest = new Mock<ITestRunRequest>();153 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();154 List<TestCase> list = new List<TestCase>();155 list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));156 list.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2"));157 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));158 mockTestRunRequest.Setup(dr => dr.ExecuteAsync()).Throws(new TestPlatformException("DummyTestPlatformException"));159 mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Returns(mockTestRunRequest.Object);160 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);161 this.ResetAndAddSourceToCommandLineOptions();162 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);163 var executor = GetExecutor(testRequestManager);164 executor.Initialize("Test1");165 ArgumentProcessorResult argumentProcessorResult = executor.Execute();166 Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult);167 }168 [TestMethod]169 public void ExecutorExecuteShouldCatchSettingsExceptionThrownDuringExecutionAndReturnFail()170 {171 var mockTestPlatform = new Mock<ITestPlatform>();172 var mockTestRunRequest = new Mock<ITestRunRequest>();173 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();174 List<TestCase> list = new List<TestCase>();175 list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));176 list.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2"));177 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));178 mockTestRunRequest.Setup(dr => dr.ExecuteAsync()).Throws(new SettingsException("DummySettingsException"));179 mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Returns(mockTestRunRequest.Object);180 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);181 this.ResetAndAddSourceToCommandLineOptions();182 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);183 var executor = GetExecutor(testRequestManager);184 executor.Initialize("Test1");185 ArgumentProcessorResult argumentProcessorResult = executor.Execute();186 Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult);187 }188 [TestMethod]189 public void ExecutorExecuteShouldCatchInvalidOperationExceptionThrownDuringExecutionAndReturnFail()190 {191 var mockTestPlatform = new Mock<ITestPlatform>();192 var mockTestRunRequest = new Mock<ITestRunRequest>();193 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();194 List<TestCase> list = new List<TestCase>();195 list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));196 list.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2"));197 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));198 mockTestRunRequest.Setup(dr => dr.ExecuteAsync()).Throws(new SettingsException("DummySettingsException"));199 mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Returns(mockTestRunRequest.Object);200 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);201 this.ResetAndAddSourceToCommandLineOptions();202 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);203 var executor = GetExecutor(testRequestManager);204 executor.Initialize("Test1");205 ArgumentProcessorResult argumentProcessorResult = executor.Execute();206 Assert.AreEqual(ArgumentProcessorResult.Fail, argumentProcessorResult);207 }208 [TestMethod]209 public void ExecutorExecuteShouldForValidSourcesAndNoTestsDiscoveredShouldLogWarningAndReturnSuccess()210 {211 var mockTestPlatform = new Mock<ITestPlatform>();212 var mockTestRunRequest = new Mock<ITestRunRequest>();213 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();214 this.ResetAndAddSourceToCommandLineOptions();215 // Setting some testdapterpath216 CommandLineOptions.Instance.TestAdapterPath = @"C:\Foo";217 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(new List<TestCase>()));218 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);219 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);220 var executor = GetExecutor(testRequestManager);221 executor.Initialize("Test1");222 ArgumentProcessorResult argumentProcessorResult = executor.Execute();223 this.mockOutput.Verify(o => o.WriteLine("Starting test discovery, please wait...", OutputLevel.Information), Times.Once);224 this.mockOutput.Verify(o => o.WriteLine(NoDiscoveredTestsWarning, OutputLevel.Warning), Times.Once);225 Assert.AreEqual(ArgumentProcessorResult.Success, argumentProcessorResult);226 }227 [TestMethod]228 public void ExecutorExecuteShouldForValidSourcesAndNoTestsDiscoveredShouldLogAppropriateWarningIfTestAdapterPathIsNotSetAndReturnSuccess()229 {230 var mockTestPlatform = new Mock<ITestPlatform>();231 var mockTestRunRequest = new Mock<ITestRunRequest>();232 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();233 this.ResetAndAddSourceToCommandLineOptions();234 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(new List<TestCase>()));235 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);236 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);237 var executor = GetExecutor(testRequestManager);238 executor.Initialize("Test1");239 ArgumentProcessorResult argumentProcessorResult = executor.Execute();240 this.mockOutput.Verify(o => o.WriteLine("Starting test discovery, please wait...", OutputLevel.Information), Times.Once);241 this.mockOutput.Verify(o => o.WriteLine(NoDiscoveredTestsWarning + " " + TestAdapterPathSuggestion, OutputLevel.Warning), Times.Once);242 Assert.AreEqual(ArgumentProcessorResult.Success, argumentProcessorResult);243 }244 [TestMethod]245 public void ExecutorExecuteShouldForValidSourcesAndValidSelectedTestsRunsTestsAndReturnSuccess()246 {247 var mockTestPlatform = new Mock<ITestPlatform>();248 var mockTestRunRequest = new Mock<ITestRunRequest>();249 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();250 this.ResetAndAddSourceToCommandLineOptions();251 List<TestCase> list = new List<TestCase>();252 list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));253 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));254 mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>())).Returns(mockTestRunRequest.Object);255 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>())).Returns(mockDiscoveryRequest.Object);256 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);257 var executor = GetExecutor(testRequestManager);258 executor.Initialize("Test1");259 ArgumentProcessorResult argumentProcessorResult = executor.Execute();260 Assert.AreEqual(ArgumentProcessorResult.Success, argumentProcessorResult);261 }262 #endregion263 private void ResetAndAddSourceToCommandLineOptions()264 {265 CommandLineOptions.Instance.Reset();266 CommandLineOptions.Instance.TestCaseFilterValue = null;267 CommandLineOptions.Instance.FileHelper = this.mockFileHelper.Object;268 CommandLineOptions.Instance.AddSource(this.dummyTestFilePath);269 }...

Full Screen

Full Screen

ListTestsArgumentProcessorTests.cs

Source:ListTestsArgumentProcessorTests.cs Github

copy

Full Screen

...132 {133 var mockTestPlatform = new Mock<ITestPlatform>();134 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();135 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new TestPlatformException("DummyTestPlatformException"));136 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);137 this.ResetAndAddSourceToCommandLineOptions();138 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask, this.mockProcessHelper.Object, this.mockAttachmentsProcessingManager.Object);139 var executor = GetExecutor(testRequestManager, null);140 Assert.ThrowsException<TestPlatformException>(() => executor.Execute());141 }142 [TestMethod]143 public void ExecutorExecuteShouldThrowSettingsException()144 {145 var mockTestPlatform = new Mock<ITestPlatform>();146 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();147 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new SettingsException("DummySettingsException"));148 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);149 this.ResetAndAddSourceToCommandLineOptions();150 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask, this.mockProcessHelper.Object, this.mockAttachmentsProcessingManager.Object);151 var listTestsArgumentExecutor = GetExecutor(testRequestManager, null);152 Assert.ThrowsException<SettingsException>(() => listTestsArgumentExecutor.Execute());153 }154 [TestMethod]155 public void ExecutorExecuteShouldThrowInvalidOperationException()156 {157 var mockTestPlatform = new Mock<ITestPlatform>();158 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();159 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new InvalidOperationException("DummyInvalidOperationException"));160 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);161 this.ResetAndAddSourceToCommandLineOptions();162 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask, this.mockProcessHelper.Object, this.mockAttachmentsProcessingManager.Object);163 var listTestsArgumentExecutor = GetExecutor(testRequestManager, null);164 Assert.ThrowsException<InvalidOperationException>(() => listTestsArgumentExecutor.Execute());165 }166 [TestMethod]167 public void ExecutorExecuteShouldThrowOtherExceptions()168 {169 var mockTestPlatform = new Mock<ITestPlatform>();170 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();171 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Throws(new Exception("DummyException"));172 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);173 this.ResetAndAddSourceToCommandLineOptions();174 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask, this.mockProcessHelper.Object, this.mockAttachmentsProcessingManager.Object);175 var executor = GetExecutor(testRequestManager, null);176 Assert.ThrowsException<Exception>(() => executor.Execute());177 }178 [TestMethod]179 public void ExecutorExecuteShouldOutputDiscoveredTestsAndReturnSuccess()180 {181 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();182 var mockConsoleOutput = new Mock<IOutput>();183 this.RunListTestArgumentProcessorExecuteWithMockSetup(mockDiscoveryRequest, mockConsoleOutput);184 // Assert185 mockDiscoveryRequest.Verify(dr => dr.DiscoverAsync(), Times.Once);186 mockConsoleOutput.Verify((IOutput co) => co.WriteLine(" Test1", OutputLevel.Information));187 mockConsoleOutput.Verify((IOutput co) => co.WriteLine(" Test2", OutputLevel.Information));188 }189 [TestMethod]190 public void ListTestArgumentProcessorExecuteShouldInstrumentDiscoveryRequestStart()191 {192 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();193 var mockConsoleOutput = new Mock<IOutput>();194 this.RunListTestArgumentProcessorExecuteWithMockSetup(mockDiscoveryRequest, mockConsoleOutput);195 this.mockTestPlatformEventSource.Verify(x => x.DiscoveryRequestStart(), Times.Once);196 }197 [TestMethod]198 public void ListTestArgumentProcessorExecuteShouldInstrumentDiscoveryRequestStop()199 {200 var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();201 var mockConsoleOutput = new Mock<IOutput>();202 this.RunListTestArgumentProcessorExecuteWithMockSetup(mockDiscoveryRequest, mockConsoleOutput);203 this.mockTestPlatformEventSource.Verify(x => x.DiscoveryRequestStop(), Times.Once);204 }205 #endregion206 private void RunListTestArgumentProcessorExecuteWithMockSetup(Mock<IDiscoveryRequest> mockDiscoveryRequest, Mock<IOutput> mockConsoleOutput)207 {208 var mockTestPlatform = new Mock<ITestPlatform>();209 var list = new List<TestCase>();210 list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));211 list.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2"));212 mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));213 mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);214 this.ResetAndAddSourceToCommandLineOptions();215 var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask, this.mockProcessHelper.Object, this.mockAttachmentsProcessingManager.Object);216 GetExecutor(testRequestManager, mockConsoleOutput.Object).Execute();217 }218 private void ResetAndAddSourceToCommandLineOptions()219 {220 CommandLineOptions.Instance.Reset();221 CommandLineOptions.Instance.FileHelper = this.mockFileHelper.Object;222 CommandLineOptions.Instance.FilePatternParser = new FilePatternParser(new Mock<Matcher>().Object, this.mockFileHelper.Object);223 CommandLineOptions.Instance.AddSource(this.dummyTestFilePath);224 }225 }226}...

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful