Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Client.TestPlatform.UpdateExtensions
TestPlatform.cs
Source:TestPlatform.cs
...145 /// The update extensions.146 /// </summary>147 /// <param name="pathToAdditionalExtensions"> The path to additional extensions. </param>148 /// <param name="skipExtensionFilters">Skips filtering by name (if true).</param>149 public void UpdateExtensions(IEnumerable<string> pathToAdditionalExtensions, bool skipExtensionFilters)150 {151 this.TestEngine.GetExtensionManager()152 .UseAdditionalExtensions(pathToAdditionalExtensions, skipExtensionFilters);153 }154 /// <summary>155 /// Clears the cached extensions156 /// </summary>157 public void ClearExtensions()158 {159 this.TestEngine.GetExtensionManager().ClearExtensions();160 }161 private void ThrowExceptionIfTestHostManagerIsNull(ITestRuntimeProvider testHostManager, string settingXml)162 {163 if (testHostManager == null)164 {165 var config = XmlRunSettingsUtilities.GetRunConfigurationNode(settingXml);166 var framework = config.TargetFramework;167 EqtTrace.Error("TestPlatform.CreateTestRunRequest: No suitable testHostProvider found for runsettings : {0}", settingXml);168 throw new TestPlatformException(String.Format(CultureInfo.CurrentCulture, ClientResources.NoTestHostProviderFound));169 }170 }171 /// <summary>172 /// Update the test adapter paths provided through run settings to be used by the test service173 /// </summary>174 /// <param name="runSettings">175 /// The run Settings.176 /// </param>177 private void AddExtensionAssemblies(string runSettings)178 {179 IEnumerable<string> customTestAdaptersPaths = RunSettingsUtilities.GetTestAdaptersPaths(runSettings);180 if (customTestAdaptersPaths != null)181 {182 foreach (string customTestAdaptersPath in customTestAdaptersPaths)183 {184 var adapterPath = Path.GetFullPath(Environment.ExpandEnvironmentVariables(customTestAdaptersPath));185 if (!Directory.Exists(adapterPath))186 {187 if (EqtTrace.IsWarningEnabled)188 {189 EqtTrace.Warning(string.Format("AdapterPath Not Found:", adapterPath));190 }191 continue;192 }193 var extensionAssemblies = new List<string>(this.fileHelper.EnumerateFiles(adapterPath, SearchOption.AllDirectories, TestPlatformConstants.TestAdapterEndsWithPattern, TestPlatformConstants.TestLoggerEndsWithPattern, TestPlatformConstants.RunTimeEndsWithPattern));194 if (extensionAssemblies.Count > 0)195 {196 this.UpdateExtensions(extensionAssemblies, skipExtensionFilters: false);197 }198 }199 }200 }201 /// <summary>202 /// Update the extension assemblies from source directory203 /// </summary>204 /// <param name="testRunCriteria">205 /// The test Run Criteria.206 /// </param>207 private void AddExtensionAssembliesFromSource(TestRunCriteria testRunCriteria)208 {209 IEnumerable<string> sources = testRunCriteria.Sources;210 if (testRunCriteria.HasSpecificTests)211 {212 // If the test execution is with a test filter, group them by sources213 sources = testRunCriteria.Tests.Select(tc => tc.Source).Distinct();214 }215 AddExtensionAssembliesFromSource(sources);216 }217 /// <summary>218 /// Update the test logger paths from source directory219 /// </summary>220 /// <param name="sources"></param>221 private void AddExtensionAssembliesFromSource(IEnumerable<string> sources)222 {223 // Currently we support discovering loggers only from Source directory224 var loggersToUpdate = new List<string>();225 foreach (var source in sources)226 {227 var sourceDirectory = Path.GetDirectoryName(source);228 if (!string.IsNullOrEmpty(sourceDirectory) && this.fileHelper.DirectoryExists(sourceDirectory))229 {230 loggersToUpdate.AddRange(this.fileHelper.EnumerateFiles(sourceDirectory, SearchOption.TopDirectoryOnly, TestPlatformConstants.TestLoggerEndsWithPattern));231 }232 }233 if (loggersToUpdate.Count > 0)234 {235 this.UpdateExtensions(loggersToUpdate, skipExtensionFilters: false);236 }237 }238 /// <summary>239 /// Find all test platform extensions from the `.\Extensions` directory. This is used to load the inbox extensions like240 /// Trx logger and legacy test extensions like mstest v1, mstest c++ etc..241 /// </summary>242 private static void AddExtensionAssembliesFromExtensionDirectory()243 {244 var fileHelper = new FileHelper();245 var extensionsFolder = Path.Combine(Path.GetDirectoryName(typeof(TestPlatform).GetTypeInfo().Assembly.GetAssemblyLocation()), "Extensions");246 if (fileHelper.DirectoryExists(extensionsFolder))247 {248 var defaultExtensionPaths = fileHelper.EnumerateFiles(extensionsFolder, SearchOption.TopDirectoryOnly, ".dll", ".exe");249 TestPluginCache.Instance.DefaultExtensionPaths = defaultExtensionPaths;...
UpdateExtensions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Client;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;9using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;10{11 {12 static void Main(string[] args)13 {14 TestPlatform testPlatform = TestPlatform.Create();15 var testPlatformOptions = new TestPlatformOptions();16 var testRunCriteria = new TestRunCriteria("3.dll", testPlatformOptions);17 testPlatform.UpdateExtensions(testRunCriteria);18 var runRequest = testPlatform.CreateTestRunRequest(testRunCriteria);19 runRequest.Execute();20 }21 }22}23using Microsoft.VisualStudio.TestTools.UnitTesting;24{25 {26 public void TestMethod1()27 {28 Assert.IsTrue(true);29 }30 }31}
UpdateExtensions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Client;7using Microsoft.VisualStudio.TestPlatform.Common;8using Microsoft.VisualStudio.TestPlatform.ObjectModel;9using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;10using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;11{12 {13 static void Main(string[] args)14 {15 TestPlatform testPlatform = TestPlatform.Create();16 var discoveryCriteria = new DiscoveryCriteria(new List<string>() { "3.dll" }, 32, null);17 var discoveryEvents = new DiscoveryEvents();18 var discoveryResult = testPlatform.DiscoverTests(discoveryCriteria, discoveryEvents);19 var testRunCriteria = new TestRunCriteria(discoveryResult, 32);20 var testRunEvents = new TestRunEvents();21 var testRunResult = testPlatform.RunTests(testRunCriteria, testRunEvents);22 Console.ReadKey();23 }24 }25 {26 public void HandleLogMessage(TestMessageLevel level, string message)27 {28 Console.WriteLine("HandleLogMessage");29 }30 public void HandleRawMessage(string rawMessage)31 {32 Console.WriteLine("HandleRawMessage");33 }34 public void HandleTestRunComplete(TestRunCompleteEventArgs completeArgs, CancellationToken cancellationToken, ITestRunStatisticsAggregator runStatsAggregator)35 {36 Console.WriteLine("HandleTestRunComplete");37 }38 public void HandleTestRunStatisticsChange(TestRunChangedEventArgs testRunChangedEventArgs)39 {40 Console.WriteLine("HandleTestRunStatisticsChange");41 }42 }43 {44 public void HandleDiscoveryComplete(DiscoveryCompleteEventArgs discoveryCompleteEventArgs, CancellationToken cancellationToken)45 {46 Console.WriteLine("HandleDiscoveryComplete");47 }48 public void HandleDiscoveredTests(DiscoveredTestsEventArgs discoveredTestsEvents)49 {50 Console.WriteLine("HandleDiscoveredTests");51 }52 public void HandleLogMessage(TestMessageLevel level, string message)53 {54 Console.WriteLine("HandleLogMessage");55 }56 public void HandleRawMessage(string rawMessage)57 {58 Console.WriteLine("HandleRawMessage");59 }60 }61}62using System;63using System.Collections.Generic;64using System.Linq;65using System.Text;66using System.Threading.Tasks;
UpdateExtensions
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Client;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 var testPlatform = TestPlatformFactory.Create();14 var discoveryRequestData = new DiscoveryRequestData(new TestPlatformOptions(), new TestPackage("D:\\Test\\UnitTestProject2\\bin\\Debug\\UnitTestProject2.dll"));15 var discoveryRequest = testPlatform.CreateDiscoveryRequest(discoveryRequestData);16 var discoveryEventHandler = new DiscoveryEventHandler();17 discoveryRequest.DiscoverAsync().GetAwaiter().GetResult();18 discoveryRequest.DiscoveryComplete += (sender, discoveryEventArgs) =>19 {20 discoveryEventHandler.HandleDiscoveryComplete(sender, discoveryEventArgs);
UpdateExtensions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Client;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;9{10 {11 static void Main(string[] args)12 {13 TestPlatform testPlatform = TestPlatform.Create();14 UpdateExtensions(testPlatform);15 Console.ReadLine();16 }17 private static void UpdateExtensions(TestPlatform testPlatform)18 {19 string extensionsPath = @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow";20 IEnumerable<string> extensions = testPlatform.GetExtensions(extensionsPath);21 testPlatform.UpdateExtensions(extensions, extensionsPath);22 }23 }24}
UpdateExtensions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7 {8 static void Main(string[] args)9 {10 Microsoft.VisualStudio.TestPlatform.Client.TestPlatform testPlatform = new Microsoft.VisualStudio.TestPlatform.Client.TestPlatform();11 testPlatform.UpdateExtensions(new List<string>() { "C:\\TestExtensions\\TestExtension.1.0.0.vsix" });12 }13 }14}15using System;16using System.Collections.Generic;17using System.Linq;18using System.Text;19using System.Threading.Tasks;20{21 {22 static void Main(string[] args)23 {24 Microsoft.VisualStudio.TestPlatform.Client.TestPlatform testPlatform = new Microsoft.VisualStudio.TestPlatform.Client.TestPlatform();25 testPlatform.UpdateExtensions(new List<string>() { "C:\\TestExtensions\\TestExtension.1.0.0.vsix" }, true);26 }27 }28}29using System;
UpdateExtensions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using Microsoft.VisualStudio.TestPlatform.Client;5using Microsoft.VisualStudio.TestPlatform.ObjectModel;6using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;7{8 {9 static void Main(string[] args)10 {11 var testPlatform = TestPlatformFactory.GetTestPlatform();12 var extensions = testPlatform.GetTestExtensions();13 var updatedExtensions = testPlatform.UpdateExtensions(extensions);14 var updatedExtensions2 = testPlatform.GetTestExtensions();15 Console.WriteLine("Extensions are updated: " + updatedExtensions.SequenceEqual(updatedExtensions2));16 }17 }18}19using System;20using System.Collections.Generic;21using System.Linq;22using Microsoft.VisualStudio.TestPlatform.Client;23using Microsoft.VisualStudio.TestPlatform.ObjectModel;24using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;25{26 {27 static void Main(string[] args)28 {29 var testPlatform = TestPlatformFactory.GetTestPlatform();30 var testHostLaunchArguments = new TestProcessStartInfo();31 var testHostProcessStartInfo = testPlatform.GetTestHostProcessStartInfo(testHostLaunchArguments);32 Console.WriteLine("Test host process start information is returned: " + testHostProcessStartInfo != null);33 }34 }35}
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!!