Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities.TestDiscovererPluginInformation.GetAssemblyType
TestDiscovererPluginInformation.cs
Source:TestDiscovererPluginInformation.cs
...24 if (testDiscovererType != null)25 {26 this.FileExtensions = GetFileExtensions(testDiscovererType);27 this.DefaultExecutorUri = GetDefaultExecutorUri(testDiscovererType);28 this.AssemblyType = GetAssemblyType(testDiscovererType);29 }30 }31 /// <summary>32 /// Metadata for the test plugin33 /// </summary>34 public override ICollection<Object> Metadata35 {36 get37 {38 return new object[] { this.FileExtensions, this.DefaultExecutorUri, this.AssemblyType };39 }40 }41 /// <summary>42 /// Gets collection of file extensions supported by discoverer plugin.43 /// </summary>44 public List<string> FileExtensions45 {46 get;47 private set;48 }49 /// <summary>50 /// Gets the Uri identifying the executor51 /// </summary>52 public string DefaultExecutorUri53 {54 get;55 private set;56 }57 /// <summary>58 /// Gets the assembly type supported by discoverer plugin.59 /// </summary>60 public AssemblyType AssemblyType61 {62 get;63 private set;64 }65 /// <summary>66 /// Helper to get file extensions from the FileExtensionAttribute on the discover plugin.67 /// </summary>68 /// <param name="testDicovererType">Data type of the test discoverer</param>69 /// <returns>List of file extensions</returns>70 private static List<string> GetFileExtensions(Type testDicovererType)71 {72 var fileExtensions = new List<string>();73 74 var attributes = testDicovererType.GetTypeInfo().GetCustomAttributes(typeof(FileExtensionAttribute), false).ToArray();75 if (attributes != null && attributes.Length > 0)76 {77 foreach (var attribute in attributes)78 {79 var fileExtAttribute = (FileExtensionAttribute)attribute;80 if (!string.IsNullOrEmpty(fileExtAttribute.FileExtension))81 {82 fileExtensions.Add(fileExtAttribute.FileExtension);83 }84 }85 }86 return fileExtensions;87 }88 /// <summary>89 /// Returns the value of default executor Uri on this type. 'Null' if not present.90 /// </summary>91 /// <param name="testDiscovererType"> The test discoverer Type. </param>92 /// <returns> The default executor URI. </returns>93 private static string GetDefaultExecutorUri(Type testDiscovererType)94 {95 string result = string.Empty;96 97 object[] attributes = testDiscovererType.GetTypeInfo().GetCustomAttributes(typeof(DefaultExecutorUriAttribute), false).ToArray();98 if (attributes != null && attributes.Length > 0)99 {100 DefaultExecutorUriAttribute executorUriAttribute = (DefaultExecutorUriAttribute)attributes[0];101 if (!string.IsNullOrEmpty(executorUriAttribute.ExecutorUri))102 {103 result = executorUriAttribute.ExecutorUri;104 }105 }106 return result;107 }108 /// <summary>109 /// Helper to get the supported assembly type from the CategoryAttribute on the discover plugin.110 /// </summary>111 /// <param name="testDiscovererType"> The test discoverer Type. </param>112 /// <returns> Supported assembly type. </returns>113 private AssemblyType GetAssemblyType(Type testDiscovererType)114 {115 var assemblyType = default(AssemblyType);116 // Get Category117 var attribute = testDiscovererType.GetTypeInfo().GetCustomAttribute(typeof(CategoryAttribute));118 var category = (attribute as CategoryAttribute)?.Category;119 // Get assembly type from category.120 Enum.TryParse(category, true, out assemblyType);121 return assemblyType;122 }123 }124}...
GetAssemblyType
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;7{8 {9 static void Main(string[] args)10 {11 TestDiscovererPluginInformation testDiscovererPluginInformation = new TestDiscovererPluginInformation("test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test");12 Type type = testDiscovererPluginInformation.GetAssemblyType();13 Console.WriteLine(type);14 Console.ReadLine();15 }16 }17}
GetAssemblyType
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;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 [FileExtension(".dll")]12 {13 public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)14 {15 var type = testDiscovererPluginInformation.GetAssemblyType("ConsoleApp1.TestDiscoverer");16 Console.WriteLine("Type: " + type);17 }18 }19}20using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;21using Microsoft.VisualStudio.TestPlatform.ObjectModel;22using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;23using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;24using System;25using System.Collections.Generic;26using System.Linq;27using System.Text;28using System.Threading.Tasks;29{30 [FileExtension(".dll")]31 {32 public void Cancel()33 {34 throw new NotImplementedException();35 }36 public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)37 {38 var type = testExecutorPluginInformation.GetAssemblyType("ConsoleApp1.TestExecutor");39 Console.WriteLine("Type: " + type);40 }41 public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)42 {43 throw new NotImplementedException();44 }45 }46}47using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;48using Microsoft.VisualStudio.TestPlatform.ObjectModel;49using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;50using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;51using System;52using System.Collections.Generic;
GetAssemblyType
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;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.Diagnostics;8using System.Linq;9using System.Reflection;10using System.Text;11using System.Threading.Tasks;12{13 [FileExtension(".dll")]14 [FileExtension(".exe")]15 {16 public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)17 {18 var testDiscovererPluginInformation = new TestDiscovererPluginInformation(typeof(TestDiscoverer), "TestDiscoverer", "1.0", "TestDiscoverer", "TestDiscoverer", "TestDiscoverer");19 var assemblyType = testDiscovererPluginInformation.GetAssemblyType();20 var assembly = Assembly.LoadFrom(sources.First());21 var type = assembly.GetType(assemblyType);22 var method = type.GetMethod("DiscoverTests");23 var instance = Activator.CreateInstance(type);24 method.Invoke(instance, new object[] { sources, discoveryContext, logger, discoverySink });25 }26 }27}
GetAssemblyType
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 var testDiscovererPluginInformation = new Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities.TestDiscovererPluginInformation();11 var testDiscoverer = testDiscovererPluginInformation.GetAssemblyType("C:\\Users\\test\\source\\repos\\ConsoleApp1\\ConsoleApp1\\bin\\Debug\\ConsoleApp1.dll");12 Console.WriteLine(testDiscoverer);13 Console.ReadLine();14 }15 }16}
GetAssemblyType
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8{9 {10 static void Main(string[] args)11 {12 TestDiscovererPluginInformation testDiscovererPluginInformation = new TestDiscovererPluginInformation("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\Extensions\\TestPlatform\\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll", "Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.VSTestDiscoverer");13 Type type = testDiscovererPluginInformation.GetAssemblyType();14 Console.WriteLine(type.FullName);15 Console.ReadLine();16 }17 }18}
GetAssemblyType
Using AI Code Generation
1using System;2using System.Reflection;3{4 {5 static void Main(string[] args)6 {7 var testDiscovererPluginInformationType = Assembly.LoadFrom(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.dll").GetType("Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities.TestDiscovererPluginInformation");8 var testDiscovererPluginInformationObject = Activator.CreateInstance(testDiscovererPluginInformationType);9 var getAssemblyMethod = testDiscovererPluginInformationType.GetMethod("GetAssembly");10 var assembly = getAssemblyMethod.Invoke(testDiscovererPluginInformationObject, new object[] { "C:\\Users\\sivakumar\\Desktop\\Test\\Test\\bin\\Debug\\Test.dll" });11 var getAssemblyType = assembly.GetType().GetMethod("GetType", new Type[] { typeof(string), typeof(bool), typeof(bool) });12 var assemblyType = getAssemblyType.Invoke(assembly, new object[] { "Test.UnitTest1", false, true });13 Console.WriteLine(assemblyType);14 Console.ReadLine();15 }16 }17}18using System;19using System.Reflection;20{21 {22 static void Main(string[] args)23 {24 var testDiscovererPluginInformationType = Assembly.LoadFrom(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.dll").GetType("Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities.TestDiscovererPluginInformation");25 var testDiscovererPluginInformationObject = Activator.CreateInstance(testDiscovererPluginInformationType);26 var getAssemblyMethod = testDiscovererPluginInformationType.GetMethod("GetAssembly");27 var assembly = getAssemblyMethod.Invoke(testDiscovererPluginInformationObject, new object[] { "C:\\Users\\sivakumar\\Desktop\\Test\\Test\\bin\\Debug\\Test.dll" });28 var getAssemblyType = assembly.GetType().GetMethod("GetType", new Type[] { typeof(string), typeof(bool), typeof(bool) });29 var assemblyType = getAssemblyType.Invoke(assembly, new object[] { "Test.UnitTest1", false, true });
GetAssemblyType
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;7using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;9{10 [FriendlyName("TestDiscoverer")]11 {12 public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)13 {14 {15 var assemblyType = TestDiscovererPluginInformation.GetAssemblyType(sources.First());16 }17 catch (Exception ex)18 {19 logger.SendMessage(TestMessageLevel.Error, "Exception " + ex.ToString());20 }21 }22 }23}24using System;25using System.Collections.Generic;26using System.Linq;27using System.Text;28using System.Threading.Tasks;29using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;30using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;31using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;32{33 [FriendlyName("TestExecutor")]34 {35 public void Cancel()36 {37 throw new NotImplementedException();38 }39 public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)40 {41 {42 var assemblyType = TestExecutorPluginInformation.GetAssemblyType(sources.First());43 }44 catch (Exception ex)45 {46 frameworkHandle.SendMessage(TestMessageLevel.Error, "Exception " + ex.ToString());47 }48 }49 public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)50 {51 throw new NotImplementedException();52 }53 }54}55using System;56using System.Collections.Generic;57using System.Linq;58using System.Text;59using System.Threading.Tasks;60using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;
GetAssemblyType
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Reflection;8using System.Text;9using System.Threading.Tasks;10{11 {12 static void Main(string[] args)13 {14 Assembly assembly = Assembly.LoadFrom("C:\\Users\\sivakumar\\Desktop\\TestProject1\\packages\\MSTest.TestAdapter.1.0.0\\tools\\vstest.console.exe");15 TestDiscovererPluginInformation pluginInfo = new TestDiscovererPluginInformation(assembly, "Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestDiscoverer", "Microsoft.VisualStudio.TestTools.UnitTesting");16 Type type = pluginInfo.GetAssemblyType();17 Console.WriteLine(type.FullName);18 Console.ReadLine();19 }20 }21}22using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;23using Microsoft.VisualStudio.TestPlatform.ObjectModel;24using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;25using System;26using System.Collections.Generic;27using System.Linq;28using System.Reflection;29using System.Text;30using System.Threading.Tasks;31{32 {33 static void Main(string[] args)34 {35 Assembly assembly = Assembly.LoadFrom("C:\\Users\\sivakumar\\Desktop\\TestProject1\\packages\\MSTest.TestAdapter.1.0.0\\tools\\vstest.console.exe");36 TestExtensionManager pluginManager = new TestExtensionManager();37 pluginManager.LoadTestExtensions(new List<string>() { "C:\\Users\\sivakumar\\Desktop\\TestProject1\\packages\\MSTest.TestAdapter.1.0.0\\tools\\vstest.console.exe" });38 IEnumerable<TestDiscovererPluginInformation> testDiscoverers = pluginManager.GetTestDiscoverers();39 foreach (TestDiscovererPluginInformation testDiscoverer in testDiscoverers)40 {41 Console.WriteLine(testDiscoverer.FriendlyName);42 Console.WriteLine(testDiscoverer.Uri);43 Console.WriteLine(testDiscoverer.GetAssemblyType().FullName);44 }45 Console.ReadLine();46 }47 }48}
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!!