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

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

TestPlatform.cs

Source:TestPlatform.cs Github

copy

Full Screen

...176 return;177 }178 foreach (string customTestAdaptersPath in customTestAdaptersPaths)179 {180 IEnumerable<string> extensionAssemblies = ExpandTestAdapterPaths(customTestAdaptersPath, _fileHelper, adapterLoadingStrategy);181 if (extensionAssemblies.Any())182 {183 UpdateExtensions(extensionAssemblies, skipExtensionFilters: false);184 }185 }186 }187 /// <summary>188 /// Updates the test logger paths from source directory.189 /// </summary>190 ///191 /// <param name="sources">The list of sources.</param>192 private void AddLoggerAssembliesFromSource(IEnumerable<string> sources, TestAdapterLoadingStrategy strategy)193 {194 // Skip discovery unless we're using the default behavior, or NextToSource is specified.195 if (strategy != TestAdapterLoadingStrategy.Default && !strategy.HasFlag(TestAdapterLoadingStrategy.NextToSource))196 {197 return;198 }199 // Currently we support discovering loggers only from Source directory.200 List<string> loggersToUpdate = new();201 foreach (string source in sources)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 {...

Full Screen

Full Screen

ExpandTestAdapterPaths

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Client;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;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 TestPlatform testPlatform = TestPlatform.Create();15 IMessageLogger logger = new ConsoleLogger();16 string runSettings = @"<RunSettings><RunConfiguration><TargetFrameworkVersion>Framework45</TargetFrameworkVersion></RunConfiguration></RunSettings>";17 TestRunCriteria testRunCriteria = new TestRunCriteria(new List<string> { "C:/users/username/source/repos/UnitTestProject1/UnitTestProject1/bin/Debug/netcoreapp1.1/UnitTestProject1.dll" }, runSettings, TestPlatformOptions.None);18 List<string> testAdaptersPaths = testPlatform.GetTestAdapters(testRunCriteria.Sources);19 List<string> expandedTestAdaptersPaths = testPlatform.ExpandTestAdapterPaths(testAdaptersPaths);20 Console.WriteLine("Test Adapters Paths:");21 foreach (string testAdapterPath in testAdaptersPaths)22 {23 Console.WriteLine(testAdapterPath);24 }25 Console.WriteLine("Expanded Test Adapters Paths:");26 foreach (string expandedTestAdapterPath in expandedTestAdaptersPaths)27 {28 Console.WriteLine(expandedTestAdapterPath);29 }30 Console.ReadLine();31 }32 }33}

Full Screen

Full Screen

ExpandTestAdapterPaths

Using AI Code Generation

copy

Full Screen

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;8{9 {10 static void Main(string[] args)11 {12 var testPlatform = new TestPlatform();13 var sources = new List<string>() { @"C:\Users\user1\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll" };14 var settings = new Dictionary<string, string>() { { "RunConfiguration.TargetPlatform", "x64" } };15 var testAdapterPaths = new List<string>() { @"C:\Users\user1\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug" };16 var result = testPlatform.ExpandTestAdapterPaths(sources, settings, testAdapterPaths);17 foreach (var item in result)18 {19 Console.WriteLine(item);20 }21 Console.ReadLine();22 }23 }24}

Full Screen

Full Screen

ExpandTestAdapterPaths

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Client;2using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8{9 {10 static void Main(string[] args)11 {12 var testPlatform = new TestPlatform();13 var testAdapterPaths = new List<string>() { "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\Common7\\IDE\\Extensions\\TestPlatform\\vstest.console.exe" };14 </RunSettings>";15 var expandedTestAdapterPaths = testPlatform.ExpandTestAdapterPaths(testAdapterPaths, runSettings);16 Console.WriteLine(expandedTestAdapterPaths);17 }18 }19}20C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.MSTestAdapter.dll21C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.TestFramework.dll22C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll23C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.TestHostRuntimeProvider.dll24C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.TestHostProvider.dll25C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.Utilities.dll26C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.dll27C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\x64\Microsoft.VisualStudio.TestPlatform.Extensions.HtmlLogger.dll

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