Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Client.TestPlatform.GetSearchOption
TestPlatform.cs
Source:TestPlatform.cs
...202 {203 var sourceDirectory = Path.GetDirectoryName(source);204 if (!string.IsNullOrEmpty(sourceDirectory) && _fileHelper.DirectoryExists(sourceDirectory))205 {206 SearchOption searchOption = GetSearchOption(strategy, SearchOption.TopDirectoryOnly);207 loggersToUpdate.AddRange(208 _fileHelper.EnumerateFiles(209 sourceDirectory,210 searchOption,211 TestPlatformConstants.TestLoggerEndsWithPattern));212 }213 }214 if (loggersToUpdate.Count > 0)215 {216 UpdateExtensions(loggersToUpdate, skipExtensionFilters: false);217 }218 }219 /// <summary>220 /// Finds all test platform extensions from the `.\Extensions` directory. This is used to221 /// load the inbox extensions like TrxLogger and legacy test extensions like MSTest v1,222 /// MSTest C++, etc..223 /// </summary>224 private static void AddExtensionAssembliesFromExtensionDirectory()225 {226 // This method needs to run statically before we have any adapter discovery.227 // TestHostProviderManager get initialized just after this call and it228 // requires DefaultExtensionPaths to be set to resolve a TestHostProvider.229 // Since it's static, it forces us to set the adapter paths.230 //231 // Otherwise we will always get a "No suitable test runtime provider found for this run." error.232 // I (@haplois) will modify this behavior later on, but we also need to consider legacy adapters233 // and make sure they still work after modification.234 string? runSettings = RunSettingsManager.Instance.ActiveRunSettings.SettingsXml;235 RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);236 TestAdapterLoadingStrategy strategy = runConfiguration.TestAdapterLoadingStrategy;237 FileHelper fileHelper = new();238 IEnumerable<string> defaultExtensionPaths = Enumerable.Empty<string>();239 // Explicit adapter loading240 if (strategy.HasFlag(TestAdapterLoadingStrategy.Explicit))241 {242 defaultExtensionPaths = RunSettingsUtilities.GetTestAdaptersPaths(runSettings)243 .SelectMany(path => ExpandTestAdapterPaths(path, fileHelper, strategy))244 .Union(defaultExtensionPaths);245 }246 string extensionsFolder = Path.Combine(247 Path.GetDirectoryName(typeof(TestPlatform).GetTypeInfo().Assembly.GetAssemblyLocation())!,248 "Extensions");249 if (!fileHelper.DirectoryExists(extensionsFolder))250 {251 // TODO: Since we no-longer run from <playground>\vstest.console\vstest.conosle.exe in Playground, the relative252 // extensions folder location changed and we need to patch it. This should be a TEMPORARY solution though, we253 // should come up with a better way of fixing this.254 // NOTE: This is specific to Playground which references vstest.console from a location that doesn't contain255 // the Extensions folder. Normal projects shouldn't have this issue.256 extensionsFolder = Path.Combine(Path.GetDirectoryName(extensionsFolder)!, "vstest.console", "Extensions");257 }258 if (fileHelper.DirectoryExists(extensionsFolder))259 {260 // Load default runtime providers261 if (strategy.HasFlag(TestAdapterLoadingStrategy.DefaultRuntimeProviders))262 {263 defaultExtensionPaths = fileHelper264 .EnumerateFiles(extensionsFolder, SearchOption.TopDirectoryOnly, TestPlatformConstants.RunTimeEndsWithPattern)265 .Union(defaultExtensionPaths);266 }267 // Default extension loader268 if (strategy == TestAdapterLoadingStrategy.Default || strategy.HasFlag(TestAdapterLoadingStrategy.ExtensionsDirectory))269 {270 defaultExtensionPaths = fileHelper271 .EnumerateFiles(extensionsFolder, SearchOption.TopDirectoryOnly, ".dll", ".exe")272 .Union(defaultExtensionPaths);273 }274 }275 TestPluginCache.Instance.DefaultExtensionPaths = defaultExtensionPaths.Distinct();276 }277 private static SearchOption GetSearchOption(TestAdapterLoadingStrategy strategy, SearchOption defaultStrategyOption)278 {279 return strategy == TestAdapterLoadingStrategy.Default280 ? defaultStrategyOption281 : strategy.HasFlag(TestAdapterLoadingStrategy.Recursive) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;282 }283 private static IEnumerable<string> ExpandTestAdapterPaths(string path, IFileHelper fileHelper, TestAdapterLoadingStrategy strategy)284 {285 string adapterPath = Path.GetFullPath(Environment.ExpandEnvironmentVariables(path));286 // Default behavior is to only accept directories.287 if (strategy == TestAdapterLoadingStrategy.Default)288 {289 return ExpandAdaptersWithDefaultStrategy(adapterPath, fileHelper);290 }291 IEnumerable<string> adapters = ExpandAdaptersWithExplicitStrategy(adapterPath, fileHelper, strategy);292 return adapters.Distinct();293 }294 private static IEnumerable<string> ExpandAdaptersWithExplicitStrategy(string path, IFileHelper fileHelper, TestAdapterLoadingStrategy strategy)295 {296 if (!strategy.HasFlag(TestAdapterLoadingStrategy.Explicit))297 {298 return Enumerable.Empty<string>();299 }300 if (fileHelper.Exists(path))301 {302 return new[] { path };303 }304 else if (fileHelper.DirectoryExists(path))305 {306 SearchOption searchOption = GetSearchOption(strategy, SearchOption.TopDirectoryOnly);307 IEnumerable<string> adapterPaths = fileHelper.EnumerateFiles(308 path,309 searchOption,310 TestPlatformConstants.TestAdapterEndsWithPattern,311 TestPlatformConstants.TestLoggerEndsWithPattern,312 TestPlatformConstants.DataCollectorEndsWithPattern,313 TestPlatformConstants.RunTimeEndsWithPattern);314 return adapterPaths;315 }316 EqtTrace.Warning($"{nameof(TestPlatform)}.{nameof(ExpandAdaptersWithExplicitStrategy)} AdapterPath Not Found: {path}");317 return Enumerable.Empty<string>();318 }319 private static IEnumerable<string> ExpandAdaptersWithDefaultStrategy(string path, IFileHelper fileHelper)320 {...
GetSearchOption
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 string testSource = @"C:\Users\Public\Documents\Microsoft Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";15 TestPlatform testPlatform = new TestPlatform();16 var testRunCriteria = new TestRunCriteria(new List<string> { testSource }, null, TestPlatformOptions.None, TestFrameworkOptions.None);17 var testRunSettings = new TestRunSettings();18 testRunSettings.SettingsXml = @"<RunSettings><RunConfiguration><TargetPlatform>x64</TargetPlatform></RunConfiguration></RunSettings>";19 testRunCriteria.TestRunSettings = testRunSettings;20 var testRunRequest = testPlatform.CreateTestRunRequest(testRunCriteria);21 testRunRequest.Execute();22 }23 }24}
GetSearchOption
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;7{8 {9 static void Main(string[] args)10 {11 TestPlatform testPlatform = new TestPlatform();12 var searchOption = testPlatform.GetSearchOption();13 Console.WriteLine(searchOption);14 Console.ReadLine();15 }16 }17}
GetSearchOption
Using AI Code Generation
1using System;2using Microsoft.VisualStudio.TestPlatform.Client;3using Microsoft.VisualStudio.TestPlatform.ObjectModel;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;5using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;6using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;7using System.Collections.Generic;8using System.Collections.ObjectModel;9using System.Linq;10using System.Text;11using System.Threading.Tasks;12using System.Threading;13using System.Diagnostics;14using System.IO;15using System.Reflection;16{17 {18 static void Main(string[] args)19 {20 string path = @"C:\Users\user\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug";21 string pattern = "*.dll";22 string source = path + "\\" + pattern;23 string destination = @"C:\Users\user\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";24 File.Copy(source, destination, true);25 TestPlatform testPlatform = new TestPlatform();26 string testAdapterPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);27 ITestRuntimeProvider testRuntimeProvider = null;28 ITestDiscoveryEventsHandler2 discoveryEventsHandler = null;29 ITestRunEventsHandler2 runEventsHandler = null;30 ITestDiscoveryEventsHandler2 discoveryEventsHandler1 = null;31 ITestRunEventsHandler2 runEventsHandler1 = null;32 TestPlatformOptions options = new TestPlatformOptions();33 options.SetCacheTestPlatformExtensions(true);34 options.SetDesignMode(false);35 options.SetDisableAppDomain(true);36 options.SetDisableParallelization(false);37 options.SetMaxCpuCount(0);38 options.SetCollectSourceInformation(false);39 options.SetSkipDefaultAdapters(false);40 options.SetIsTelemetryOptedIn(true);41 options.SetProxyExecution(false);42 options.SetRunStatsChangeEventTimeout(0);43 options.SetTestCaseFilter("");44 options.SetTestHostLauncher(TestHostLauncherFactory.Instance.GetTestHostLauncher());45 options.SetTestRunDirectory("");46 options.SetUseNewTestExecutor(true);47 options.SetResultsDirectory("");48 options.SetRunSettings("");49 options.SetIsDataCollectionEnabled(false);50 options.SetIsDebug(false);51 options.SetIsDesignMode(false);52 options.SetIsRerun(false);53 options.SetIsSkipTestCaseFilter(false);54 options.SetIsTelemetryOptedIn(true);55 options.SetIsUI(false);
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!!