Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.RunSettingsHelper
VsTestRunner.cs
Source:VsTestRunner.cs
...68 Guard.ArgumentValid("sources", "AppX not supported for discovery", !ContainsAppX(sources));69 var stopwatch = Stopwatch.StartNew();70 var loggerHelper = new LoggerHelper(logger, stopwatch);71#if NET452 || NETCOREAPP1_072 RunSettingsHelper.ReadRunSettings(discoveryContext?.RunSettings?.SettingsXml);73 if (!ValidateRuntimeFramework())74 return;75#endif76 var testPlatformContext = new TestPlatformContext77 {78 // Discovery from command line (non designmode) never requires source information79 // since there is no session or command line runner doesn't send back VSTestCase objects80 // back to adapter.81 RequireSourceInformation = RunSettingsHelper.CollectSourceInformation,82 // Command line runner could request for Discovery in case of running specific tests. We need83 // the XunitTestCase serialized in this scenario.84 RequireXunitTestProperty = true85 };86 DiscoverTests(sources,87 loggerHelper,88 testPlatformContext,89 (source, discoverer, discoveryOptions) => new VsDiscoverySink(source, discoverer, loggerHelper, discoverySink, discoveryOptions, testPlatformContext, () => cancelled)90 );91 }92 void ITestExecutor.RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)93 {94 Guard.ArgumentNotNull("sources", sources);95 var stopwatch = Stopwatch.StartNew();96 var logger = new LoggerHelper(frameworkHandle, stopwatch);97#if NET452 || NETCOREAPP1_098 RunSettingsHelper.ReadRunSettings(runContext?.RunSettings?.SettingsXml);99 if (!ValidateRuntimeFramework())100 return;101#endif102 // In the context of Run All tests, commandline runner doesn't require source information or103 // serialized xunit test case property104 var testPlatformContext = new TestPlatformContext105 {106 RequireSourceInformation = RunSettingsHelper.CollectSourceInformation,107 RequireXunitTestProperty = RunSettingsHelper.DesignMode108 };109 // In this case, we need to go thru the files manually110 if (ContainsAppX(sources))111 {112#if WINDOWS_UAP113 var sourcePath = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;114#elif NETCOREAPP1_0115 var sourcePath = Directory.GetCurrentDirectory();116#else117 var sourcePath = Environment.CurrentDirectory;118#endif119 sources = Directory.GetFiles(sourcePath, "*.dll")120 .Where(file => !platformAssemblies.Contains(Path.GetFileName(file)))121 .ToList();122 ((List<string>)sources).AddRange(Directory.GetFiles(sourcePath, "*.exe")123 .Where(file => !platformAssemblies.Contains(Path.GetFileName(file))));124 }125 RunTests(runContext, frameworkHandle, logger, testPlatformContext, () =>126 sources.Select(source =>127 {128 var assemblyFileName = GetAssemblyFileName(source);129 return new AssemblyRunInfo130 {131 AssemblyFileName = assemblyFileName,132 Configuration = LoadConfiguration(assemblyFileName),133 TestCases = null // PERF: delay the discovery until we actually require it in RunTestsInAssembly134 };135 }).ToList());136 }137 void ITestExecutor.RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)138 {139 Guard.ArgumentNotNull("tests", tests);140 Guard.ArgumentValid("tests", "AppX not supported in this overload", !ContainsAppX(tests.Select(t => t.Source)));141 var stopwatch = Stopwatch.StartNew();142 var logger = new LoggerHelper(frameworkHandle, stopwatch);143#if NET452 || NETCOREAPP1_0144 RunSettingsHelper.ReadRunSettings(runContext?.RunSettings?.SettingsXml);145#endif146 // In the context of Run Specific tests, commandline runner doesn't require source information or147 // serialized xunit test case property148 var testPlatformContext = new TestPlatformContext149 {150 RequireSourceInformation = RunSettingsHelper.CollectSourceInformation,151 RequireXunitTestProperty = RunSettingsHelper.DesignMode152 };153 RunTests(154 runContext, frameworkHandle, logger, testPlatformContext,155 () => tests.GroupBy(testCase => testCase.Source)156 .Select(group => new AssemblyRunInfo { AssemblyFileName = group.Key, Configuration = LoadConfiguration(group.Key), TestCases = group.ToList() })157 .ToList()158 );159 }160 // Helpers161 static bool ContainsAppX(IEnumerable<string> sources)162 => sources.Any(s => string.Compare(Path.GetExtension(s), ".appx", StringComparison.OrdinalIgnoreCase) == 0);163 void DiscoverTests<TVisitor>(IEnumerable<string> sources,164 LoggerHelper logger,165 TestPlatformContext testPlatformContext,166 Func<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitorFactory,167 Action<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitComplete = null)168 where TVisitor : IVsDiscoverySink, IDisposable169 {170 try171 {172 RemotingUtility.CleanUpRegisteredChannels();173#if NET452174 using (AssemblyHelper.SubscribeResolve())175#endif176 {177 foreach (var assemblyFileNameCanBeWithoutAbsolutePath in sources)178 {179 var assemblyFileName = GetAssemblyFileName(assemblyFileNameCanBeWithoutAbsolutePath);180 var configuration = LoadConfiguration(assemblyFileName);181 var fileName = Path.GetFileNameWithoutExtension(assemblyFileName);182 var shadowCopy = configuration.ShadowCopyOrDefault;183 var diagnosticSink = new DiagnosticMessageSink(logger, fileName, configuration.DiagnosticMessagesOrDefault);184 using (var framework = new XunitFrontController(AppDomainDefaultBehavior, assemblyFileName, shadowCopy: shadowCopy, diagnosticMessageSink: MessageSinkAdapter.Wrap(diagnosticSink)))185 if (!DiscoverTestsInSource(framework, logger, testPlatformContext, visitorFactory, visitComplete, assemblyFileName, shadowCopy, configuration))186 break;187 }188 }189 }190 catch (Exception e)191 {192 logger.LogWarning("Exception discovering tests: {0}", e.Unwrap());193 }194 }195 private bool DiscoverTestsInSource<TVisitor>(XunitFrontController framework,196 LoggerHelper logger,197 TestPlatformContext testPlatformContext,198 Func<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitorFactory,199 Action<string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitComplete,200 string assemblyFileName,201 bool shadowCopy,202 TestAssemblyConfiguration configuration)203 where TVisitor : IVsDiscoverySink, IDisposable204 {205 if (cancelled)206 return false;207 var fileName = "(unknown assembly)";208 try209 {210 var reporterMessageHandler = GetRunnerReporter(new[] { assemblyFileName }).CreateMessageHandler(new VisualStudioRunnerLogger(logger));211 var assembly = new XunitProjectAssembly { AssemblyFilename = assemblyFileName };212 fileName = Path.GetFileNameWithoutExtension(assemblyFileName);213 if (!IsXunitTestAssembly(assemblyFileName))214 {215 if (configuration.DiagnosticMessagesOrDefault)216 logger.Log("Skipping: {0} (no reference to xUnit.net)", fileName);217 }218 else219 {220 var targetFramework = framework.TargetFramework;221 if (targetFramework.StartsWith("MonoTouch", StringComparison.OrdinalIgnoreCase) ||222 targetFramework.StartsWith("MonoAndroid", StringComparison.OrdinalIgnoreCase) ||223 targetFramework.StartsWith("Xamarin.iOS", StringComparison.OrdinalIgnoreCase))224 {225 if (configuration.DiagnosticMessagesOrDefault)226 logger.Log("Skipping: {0} (unsupported target framework '{1}')", fileName, targetFramework);227 }228 else229 {230 var discoveryOptions = TestFrameworkOptions.ForDiscovery(configuration);231 using (var visitor = visitorFactory(assemblyFileName, framework, discoveryOptions))232 {233 var usingAppDomains = framework.CanUseAppDomains && AppDomainDefaultBehavior != AppDomainSupport.Denied;234 reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(assembly, usingAppDomains, shadowCopy, discoveryOptions));235 framework.Find(testPlatformContext.RequireSourceInformation, visitor, discoveryOptions);236 var totalTests = visitor.Finish();237 visitComplete?.Invoke(assemblyFileName, framework, discoveryOptions, visitor);238 reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryFinished(assembly, discoveryOptions, totalTests, totalTests));239 }240 }241 }242 }243 catch (Exception e)244 {245 var ex = e.Unwrap();246 if (ex is InvalidOperationException)247 logger.LogWarning("Skipping: {0} ({1})", fileName, ex.Message);248 else if (ex is FileNotFoundException fileNotFound)249 logger.LogWarning("Skipping: {0} (could not find dependent assembly '{1}')", fileName, Path.GetFileNameWithoutExtension(fileNotFound.FileName));250#if !WINDOWS_UAP251 else if (ex is FileLoadException fileLoad)252 logger.LogWarning("Skipping: {0} (could not find dependent assembly '{1}')", fileName, Path.GetFileNameWithoutExtension(fileLoad.FileName));253#endif254 else255 logger.LogWarning("Exception discovering tests from {0}: {1}", fileName, ex);256 }257 return true;258 }259 static Stream GetConfigurationStreamForAssembly(string assemblyName)260 {261 // See if there's a directory with the assm name. this might be the case for appx262 if (Directory.Exists(assemblyName))263 {264 if (File.Exists(Path.Combine(assemblyName, $"{assemblyName}.xunit.runner.json")))265 return File.OpenRead(Path.Combine(assemblyName, $"{assemblyName}.xunit.runner.json"));266 if (File.Exists(Path.Combine(assemblyName, "xunit.runner.json")))267 return File.OpenRead(Path.Combine(assemblyName, "xunit.runner.json"));268 }269 // Fallback to working dir270 if (File.Exists($"{assemblyName}.xunit.runner.json"))271 return File.OpenRead($"{assemblyName}.xunit.runner.json");272 if (File.Exists("xunit.runner.json"))273 return File.OpenRead("xunit.runner.json");274 return null;275 }276 static TestProperty GetTestProperty()277 => TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner));278 static bool IsXunitTestAssembly(string assemblyFileName)279 {280 // Don't try to load ourselves (or any test framework assemblies), since we fail (issue #47 in xunit/xunit).281 if (platformAssemblies.Contains(Path.GetFileName(assemblyFileName)))282 return false;283#if NETCOREAPP1_0284 return IsXunitPackageReferenced(assemblyFileName);285#else286 var assemblyFolder = Path.GetDirectoryName(assemblyFileName);287 return File.Exists(Path.Combine(assemblyFolder, "xunit.dll"))288 || Directory.GetFiles(assemblyFolder, "xunit.execution.*.dll").Length > 0;289#endif290 }291#if NETCOREAPP1_0292 static bool IsXunitPackageReferenced(string assemblyFileName)293 {294 var depsFile = assemblyFileName.Replace(".dll", ".deps.json");295 if (!File.Exists(depsFile))296 return false;297 try298 {299 using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(depsFile))))300 {301 var context = new DependencyContextJsonReader().Read(stream);302 var xunitLibrary = context.RuntimeLibraries.Where(lib => lib.Name.Equals("xunit") || lib.Name.Equals("xunit.core")).FirstOrDefault();303 return xunitLibrary != null;304 }305 }306 catch307 {308 return false;309 }310 }311#endif312 static string GetAssemblyFileName(string source)313 {314#if !WINDOWS_UAP315 return Path.GetFullPath(source);316#else317 return source;318#endif319 }320 static TestAssemblyConfiguration LoadConfiguration(string assemblyName)321 {322#if WINDOWS_UAP323 var stream = GetConfigurationStreamForAssembly(assemblyName);324 return stream == null ? new TestAssemblyConfiguration() : ConfigReader.Load(stream);325#else326 return ConfigReader.Load(assemblyName);327#endif328 }329 void RunTests(IRunContext runContext,330 IFrameworkHandle frameworkHandle,331 LoggerHelper logger,332 TestPlatformContext testPlatformContext,333 Func<List<AssemblyRunInfo>> getRunInfos)334 {335 Guard.ArgumentNotNull("runContext", runContext);336 Guard.ArgumentNotNull("frameworkHandle", frameworkHandle);337 try338 {339 RemotingUtility.CleanUpRegisteredChannels();340 cancelled = false;341 var runInfos = getRunInfos();342 var parallelizeAssemblies = !RunSettingsHelper.DisableParallelization && runInfos.All(runInfo => runInfo.Configuration.ParallelizeAssemblyOrDefault);343 var reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(GetRunnerReporter(runInfos.Select(ari => ari.AssemblyFileName))344 .CreateMessageHandler(new VisualStudioRunnerLogger(logger)));345#if NET452346 using (AssemblyHelper.SubscribeResolve())347#endif348 {349 if (parallelizeAssemblies)350 runInfos351 .Select(runInfo => RunTestsInAssemblyAsync(runContext, frameworkHandle, logger, testPlatformContext, reporterMessageHandler, runInfo))352 .ToList()353 .ForEach(@event => @event.WaitOne());354 else355 runInfos356 .ForEach(runInfo => RunTestsInAssembly(runContext, frameworkHandle, logger, testPlatformContext, reporterMessageHandler, runInfo));357 }358 }359 catch (Exception ex)360 {361 logger.LogError("Catastrophic failure: {0}", ex);362 }363 }364 void RunTestsInAssembly(IRunContext runContext,365 IFrameworkHandle frameworkHandle,366 LoggerHelper logger,367 TestPlatformContext testPlatformContext,368 IMessageSinkWithTypes reporterMessageHandler,369 AssemblyRunInfo runInfo)370 {371 if (cancelled)372 return;373 var assemblyDisplayName = "(unknown assembly)";374 try375 {376 var assembly = new XunitProjectAssembly { AssemblyFilename = runInfo.AssemblyFileName };377 var assemblyFileName = runInfo.AssemblyFileName;378 assemblyDisplayName = Path.GetFileNameWithoutExtension(assemblyFileName);379 var configuration = runInfo.Configuration;380 var shadowCopy = configuration.ShadowCopyOrDefault;381 var appDomain = assembly.Configuration.AppDomain ?? AppDomainDefaultBehavior;382 var longRunningSeconds = assembly.Configuration.LongRunningTestSecondsOrDefault;383 if (RunSettingsHelper.DisableAppDomain)384 appDomain = AppDomainSupport.Denied;385#if WINDOWS_UAP386 // For AppX Apps, use the package location387 assemblyFileName = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, Path.GetFileName(assemblyFileName));388#endif389 var diagnosticSink = new DiagnosticMessageSink(logger, assemblyDisplayName, runInfo.Configuration.DiagnosticMessagesOrDefault);390 var diagnosticMessageSink = MessageSinkAdapter.Wrap(diagnosticSink);391 using (var controller = new XunitFrontController(appDomain, assemblyFileName, shadowCopy: shadowCopy, diagnosticMessageSink: diagnosticMessageSink))392 {393 var testCasesMap = new Dictionary<string, TestCase>();394 var testCases = new List<ITestCase>();395 if (runInfo.TestCases == null || !runInfo.TestCases.Any())396 {397 // Discover tests398 AssemblyDiscoveredInfo assemblyDiscoveredInfo = new AssemblyDiscoveredInfo();399 DiscoverTestsInSource(controller, logger, testPlatformContext,400 (source, discoverer, discoveryOptions) => new VsExecutionDiscoverySink(() => cancelled),401 (source, discoverer, discoveryOptions, visitor) =>402 {403 if (discoveryOptions.GetInternalDiagnosticMessagesOrDefault())404 foreach (var testCase in visitor.TestCases)405 logger.Log(testCase, "Discovered [execution] test case '{0}' (ID = '{1}')",406 testCase.DisplayName, testCase.UniqueID);407 assemblyDiscoveredInfo = new AssemblyDiscoveredInfo408 {409 AssemblyFileName = source,410 DiscoveredTestCases = GetVsTestCases(source, discoverer, visitor, logger, testPlatformContext)411 };412 },413 assemblyFileName,414 shadowCopy,415 configuration416 );417 if (assemblyDiscoveredInfo.DiscoveredTestCases == null || !assemblyDiscoveredInfo.DiscoveredTestCases.Any())418 {419 if (configuration.InternalDiagnosticMessagesOrDefault)420 logger.LogWarning("Skipping '{0}' since no tests were found during discovery [execution].", assemblyDiscoveredInfo.AssemblyFileName);421 return;422 }423 // Filter tests424 var traitNames = new HashSet<string>(assemblyDiscoveredInfo.DiscoveredTestCases.SelectMany(testCase => testCase.TraitNames));425 var filter = new TestCaseFilter(runContext, logger, assemblyDiscoveredInfo.AssemblyFileName, traitNames);426 var filteredTestCases = assemblyDiscoveredInfo.DiscoveredTestCases.Where(dtc => filter.MatchTestCase(dtc.VSTestCase)).ToList();427 foreach (var filteredTestCase in filteredTestCases)428 {429 var uniqueID = filteredTestCase.UniqueID;430 if (testCasesMap.ContainsKey(uniqueID))431 logger.LogWarning(filteredTestCase.TestCase, "Skipping test case with duplicate ID '{0}' ('{1}' and '{2}')", uniqueID, testCasesMap[uniqueID].DisplayName, filteredTestCase.VSTestCase.DisplayName);432 else433 {434 testCasesMap.Add(uniqueID, filteredTestCase.VSTestCase);435 testCases.Add(filteredTestCase.TestCase);436 }437 }438 }439 else440 {441 try442 {443 var serializations = runInfo.TestCases444 .Select(tc => tc.GetPropertyValue<string>(SerializedTestCaseProperty, null))445 .ToList();446 if (configuration.InternalDiagnosticMessagesOrDefault)447 logger.LogWithSource(runInfo.AssemblyFileName, "Deserializing {0} test case(s):{1}{2}",448 serializations.Count,449 Environment.NewLine,450 string.Join(Environment.NewLine, serializations.Select(x => $" {x}")));451 var deserializedTestCasesByUniqueId = controller.BulkDeserialize(serializations);452 if (deserializedTestCasesByUniqueId == null)453 logger.LogErrorWithSource(assemblyFileName, "Received null response from BulkDeserialize");454 else455 {456 for (int idx = 0; idx < runInfo.TestCases.Count; ++idx)457 {458 try459 {460 var kvp = deserializedTestCasesByUniqueId[idx];461 var vsTestCase = runInfo.TestCases[idx];462 if (kvp.Value == null)463 {464 logger.LogErrorWithSource(assemblyFileName, "Test case {0} failed to deserialize: {1}", vsTestCase.DisplayName, kvp.Key);465 }466 else467 {468 testCasesMap.Add(kvp.Key, vsTestCase);469 testCases.Add(kvp.Value);470 }471 }472 catch (Exception ex)473 {474 logger.LogErrorWithSource(assemblyFileName, "Catastrophic error deserializing item #{0}: {1}", idx, ex);475 }476 }477 }478 }479 catch (Exception ex)480 {481 logger.LogWarningWithSource(assemblyFileName, "Catastrophic error during deserialization: {0}", ex);482 }483 }484 // Execute tests485 var executionOptions = TestFrameworkOptions.ForExecution(runInfo.Configuration);486 if (RunSettingsHelper.DisableParallelization)487 {488 executionOptions.SetSynchronousMessageReporting(true);489 executionOptions.SetDisableParallelization(true);490 }491 reporterMessageHandler.OnMessage(new TestAssemblyExecutionStarting(assembly, executionOptions));492 using (var vsExecutionSink = new VsExecutionSink(reporterMessageHandler, frameworkHandle, logger, testCasesMap, executionOptions, () => cancelled))493 {494 IExecutionSink resultsSink = vsExecutionSink;495 if (longRunningSeconds > 0)496 resultsSink = new DelegatingLongRunningTestDetectionSink(resultsSink, TimeSpan.FromSeconds(longRunningSeconds), diagnosticSink);497 controller.RunTests(testCases, resultsSink, executionOptions);498 resultsSink.Finished.WaitOne();499 reporterMessageHandler.OnMessage(new TestAssemblyExecutionFinished(assembly, executionOptions, resultsSink.ExecutionSummary));500 }501 }502 }503 catch (Exception ex)504 {505 logger.LogError("{0}: Catastrophic failure: {1}", assemblyDisplayName, ex);506 }507 }508 ManualResetEvent RunTestsInAssemblyAsync(IRunContext runContext,509 IFrameworkHandle frameworkHandle,510 LoggerHelper logger,511 TestPlatformContext testPlatformContext,512 IMessageSinkWithTypes reporterMessageHandler,513 AssemblyRunInfo runInfo)514 {515 var @event = new ManualResetEvent(initialState: false);516 Action handler = () =>517 {518 try519 {520 RunTestsInAssembly(runContext, frameworkHandle, logger, testPlatformContext, reporterMessageHandler, runInfo);521 }522 finally523 {524 @event.Set();525 }526 };527#if WINDOWS_UAP528 var fireAndForget = Windows.System.Threading.ThreadPool.RunAsync(_ => handler());529#else530 ThreadPool.QueueUserWorkItem(_ => handler());531#endif532 return @event;533 }534 public static IRunnerReporter GetRunnerReporter(IEnumerable<string> assemblyFileNames)535 {536 var reporter = default(IRunnerReporter);537 try538 {539 if (!RunSettingsHelper.NoAutoReporters)540 reporter = GetAvailableRunnerReporters(assemblyFileNames).FirstOrDefault(r => r.IsEnvironmentallyEnabled);541 }542 catch { }543 return reporter ?? new DefaultRunnerReporterWithTypes();544 }545 static IEnumerable<IRunnerReporter> GetAvailableRunnerReporters(IEnumerable<string> sources)546 {547#if WINDOWS_UAP548 // No reporters on UWP549 return Enumerable.Empty<IRunnerReporter>();550#elif NETCOREAPP1_0551 // Combine all input libs and merge their contexts to find the potential reporters552 var result = new List<IRunnerReporter>();553 var dcjr = new DependencyContextJsonReader();554 var deps = sources.Select(Path.GetFullPath)555 .Select(s => s.Replace(".dll", ".deps.json"))556 .Where(File.Exists)557 .Select(f => new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(f))))558 .Select(dcjr.Read);559 var ctx = deps.Aggregate(DependencyContext.Default, (context, dependencyContext) => context.Merge(dependencyContext));560 dcjr.Dispose();561 var depsAssms = ctx.GetRuntimeAssemblyNames(RuntimeEnvironment.GetRuntimeIdentifier())562 .ToList();563 // Make sure to also check assemblies within the directory of the sources564 var dllsInSources = sources.Select(Path.GetFullPath)565 .Select(Path.GetDirectoryName)566 .Distinct(StringComparer.OrdinalIgnoreCase)567 .SelectMany(p => Directory.GetFiles(p, "*.dll").Select(f => Path.Combine(p, f)))568 .Select(f => new AssemblyName { Name = Path.GetFileNameWithoutExtension(f) })569 .ToList();570 foreach (var assemblyName in depsAssms.Concat(dllsInSources))571 {572 try573 {574 var assembly = Assembly.Load(assemblyName);575 foreach (var type in assembly.DefinedTypes)576 {577#pragma warning disable CS0618578 if (type == null || type.IsAbstract || type == typeof(DefaultRunnerReporter).GetTypeInfo() || type == typeof(DefaultRunnerReporterWithTypes).GetTypeInfo() || type.ImplementedInterfaces.All(i => i != typeof(IRunnerReporter)))579 continue;580#pragma warning restore CS0618581 var ctor = type.DeclaredConstructors.FirstOrDefault(c => c.GetParameters().Length == 0);582 if (ctor == null)583 {584 ConsoleHelper.SetForegroundColor(ConsoleColor.Yellow);585 Console.WriteLine($"Type {type.FullName} in assembly {assembly} appears to be a runner reporter, but does not have an empty constructor.");586 ConsoleHelper.ResetColor();587 continue;588 }589 result.Add((IRunnerReporter)ctor.Invoke(new object[0]));590 }591 }592 catch593 {594 continue;595 }596 }597 return result;598#elif NET452599 var result = new List<IRunnerReporter>();600 var runnerPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetLocalCodeBase());601 var runnerReporterInterfaceAssemblyFullName = typeof(IRunnerReporter).Assembly.GetName().FullName;602 foreach (var dllFile in Directory.GetFiles(runnerPath, "*.dll").Select(f => Path.Combine(runnerPath, f)))603 {604 Type[] types;605 try606 {607 var assembly = Assembly.LoadFile(dllFile);608 // Calling Assembly.GetTypes can be very expensive, while Assembly.GetReferencedAssemblies609 // is relatively cheap. We can avoid loading types for assemblies that couldn't possibly610 // reference IRunnerReporter.611 if (!assembly.GetReferencedAssemblies().Where(name => name.FullName == runnerReporterInterfaceAssemblyFullName).Any())612 continue;613 types = assembly.GetTypes();614 }615 catch (ReflectionTypeLoadException ex)616 {617 types = ex.Types;618 }619 catch620 {621 continue;622 }623 foreach (var type in types)624 {625#pragma warning disable CS0618626 if (type == null || type.IsAbstract || type == typeof(DefaultRunnerReporter) || type == typeof(DefaultRunnerReporterWithTypes) || !type.GetInterfaces().Any(t => t == typeof(IRunnerReporter)))627 continue;628#pragma warning restore CS0618629 var ctor = type.GetConstructor(new Type[0]);630 if (ctor == null)631 {632 ConsoleHelper.SetForegroundColor(ConsoleColor.Yellow);633 Console.WriteLine($"Type {type.FullName} in assembly {dllFile} appears to be a runner reporter, but does not have an empty constructor.");634 ConsoleHelper.ResetColor();635 continue;636 }637 result.Add((IRunnerReporter)ctor.Invoke(new object[0]));638 }639 }640 return result;641#endif642 }643 /// <summary>644 /// Validates the runtime target framework from test platform with the current adapter's target.645 /// </summary>646 /// <returns>True if the target frameworks match.</returns>647 static bool ValidateRuntimeFramework()648 {649#if NETCOREAPP1_0650 var targetFrameworkVersion = RunSettingsHelper.TargetFrameworkVersion;651 return targetFrameworkVersion.StartsWith(".NETCore", StringComparison.OrdinalIgnoreCase) ||652 targetFrameworkVersion.StartsWith("FrameworkCore", StringComparison.OrdinalIgnoreCase);653#else654 return true;655#endif656 }657 IList<DiscoveredTestCase> GetVsTestCases(string source, ITestFrameworkDiscoverer discoverer, VsExecutionDiscoverySink visitor, LoggerHelper logger, TestPlatformContext testPlatformContext)658 {659 var descriptorProvider = (discoverer as ITestCaseDescriptorProvider) ?? new DefaultTestCaseDescriptorProvider(discoverer);660 var testCases = visitor.TestCases;661 var descriptors = descriptorProvider.GetTestCaseDescriptors(testCases, false);662 var results = new List<DiscoveredTestCase>(descriptors.Count);663 for (int idx = 0; idx < descriptors.Count; ++idx)664 results.Add(new DiscoveredTestCase(source, descriptors[idx], testCases[idx], logger, testPlatformContext));...
RunSettingsArgumentProcessor.cs
Source:RunSettingsArgumentProcessor.cs
...113 }114 var platformStr = _runSettingsManager.QueryRunSettingsNode(PlatformArgumentExecutor.RunSettingsPath);115 if (Enum.TryParse<Architecture>(platformStr, true, out var architecture))116 {117 RunSettingsHelper.Instance.IsDefaultTargetArchitecture = false;118 _commandLineOptions.TargetArchitecture = architecture;119 }120 }121 protected virtual XmlReader GetReaderForFile(string runSettingsFile)122 {123 return XmlReader.Create(new StringReader(File.ReadAllText(runSettingsFile, Encoding.UTF8)), XmlRunSettingsUtilities.ReaderSettings);124 }125 private XmlDocument GetRunSettingsDocument(string runSettingsFile)126 {127 XmlDocument runSettingsDocument;128 if (!MSTestSettingsUtilities.IsLegacyTestSettingsFile(runSettingsFile))129 {130 using XmlReader reader = GetReaderForFile(runSettingsFile);131 var settingsDocument = new XmlDocument();...
RunSettingsHelper.cs
Source:RunSettingsHelper.cs
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.3using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;4namespace Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;5/// <summary>6/// RunSettingsHelper is used to globally share additional informations about the state of runsettings.7/// </summary>8internal class RunSettingsHelper : IRunSettingsHelper9{10 private static readonly IRunSettingsHelper RunSettings = new RunSettingsHelper();11 public static IRunSettingsHelper Instance = RunSettings;12 /// <summary>13 /// If false user updated the RunConfiguration.TargetPlatform using14 /// --arch or runsettings file or -- RunConfiguration.TargetPlatform=arch15 /// This option is needed because we otherwise don't know if user specified an architecture or if we inferred it from16 /// the dll. When we add the capability to distinguish this in runsettings, this helper won't be needed.17 /// </summary>18 public bool IsDefaultTargetArchitecture { get; set; } = true;19 /// <summary>20 /// True indicates the test run is started from an Editor or IDE.21 /// Defaults to false.22 /// </summary>23 public bool IsDesignMode { get; set; }24}...
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 var runSettings = RunSettingsHelper.CreateRunSettings();12 Console.WriteLine(runSettings);13 }14 }15}16using Microsoft.VisualStudio.TestPlatform.ObjectModel;17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22{23 {24 static void Main(string[] args)25 {26 var runSettings = RunSettingsHelper.CreateRunSettings();27 Console.WriteLine(runSettings);28 }29 }30}
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 var settings = new Dictionary<string, string>();12 settings["key1"] = "value1";13 settings["key2"] = "value2";14 RunSettingsHelper.UpdateRunSettingsFile(@"C:\Users\username\source\repos\ConsoleApp1\RunSettings.runsettings", settings);15 }16 }17}18using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24{25 {26 static void Main(string[] args)27 {28 var settings = new Dictionary<string, string>();29 settings["key1"] = "value1";30 settings["key2"] = "value2";31 RunSettingsHelper.UpdateRunSettingsFile(@"C:\Users\username\source\repos\ConsoleApp1\RunSettings.runsettings", settings);32 }33 }34}35using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;36using System;37using System.Collections.Generic;38using System.Linq;39using System.Text;40using System.Threading.Tasks;41{42 {43 static void Main(string[] args)44 {45 var settings = new Dictionary<string, string>();46 settings["key1"] = "value1";47 settings["key2"] = "value2";48 RunSettingsHelper.UpdateRunSettingsFile(@"C:\Users\username\source\repos\ConsoleApp1\RunSettings.runsettings", settings);49 }50 }51}52using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;53using System;54using System.Collections.Generic;55using System.Linq;56using System.Text;57using System.Threading.Tasks;58{59 {60 static void Main(string[] args)61 {62 var settings = new Dictionary<string, string>();63 settings["key1"] = "value1";
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 var runSettingsHelper = new RunSettingsHelper();12 var runSettings = runSettingsHelper.GetRunSettings(args[0]);13 var testSettings = runSettingsHelper.GetTestSettings(runSettings);14 var testSettingsNode = runSettingsHelper.GetTestSettingsNode(runSettings);15 var testRunParameters = runSettingsHelper.GetTestRunParameters(runSettings);16 var testRunParametersNode = runSettingsHelper.GetTestRunParametersNode(runSettings);17 var testRunParametersNode2 = runSettingsHelper.GetTestRunParametersNode(runSettings, "TestRunParameters");18 var testRunParametersNode3 = runSettingsHelper.GetTestRunParametersNode(runSettings, "TestRunParameters2");19 Console.WriteLine("RunSettings: " + runSettings);20 Console.WriteLine("TestSettings: " + testSettings);21 Console.WriteLine("TestSettingsNode: " + testSettingsNode);22 Console.WriteLine("TestRunParameters: " + testRunParameters);23 Console.WriteLine("TestRunParametersNode: " + testRunParametersNode);24 Console.WriteLine("TestRunParametersNode2: " + testRunParametersNode2);25 Console.WriteLine("TestRunParametersNode3: " + testRunParametersNode3);26 Console.ReadLine();27 }28 }29}30using Microsoft.VisualStudio.TestPlatform.ObjectModel;31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36{37 {38 static void Main(string[] args)39 {40 var runSettingsHelper = new RunSettingsHelper();41 var runSettings = runSettingsHelper.GetRunSettings(args[0]);42 var testSettings = runSettingsHelper.GetTestSettings(runSettings);43 var testSettingsNode = runSettingsHelper.GetTestSettingsNode(runSettings);44 var testRunParameters = runSettingsHelper.GetTestRunParameters(runSettings);45 var testRunParametersNode = runSettingsHelper.GetTestRunParametersNode(runSettings);46 var testRunParametersNode2 = runSettingsHelper.GetTestRunParametersNode(runSettings, "TestRunParameters");
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 var runSettingsHelper = new RunSettingsHelper();12 var runSettings = runSettingsHelper.GetRunSettings(args[0]);13 var testSettings = runSettingsHelper.GetTestSettings(runSettings);14 var testSettingsNode = runSettingsHelper.GetTestSettingsNode(runSettings);15 var testRunParameters = runSettingsHelper.GetTestRunParameters(runSettings);16 var testRunParametersNode = runSettingsHelper.GetTestRunParametersNode(runSettings);17 var testRunParametersNode2 = runSettingsHelper.GetTestRunParametersNode(runSettings, "TestRunParameters");18 var testRunParametersNode3 = runSettingsHelper.GetTestRunParametersNode(runSettings, "TestRunParameters2");19 Console.WriteLine("RunSettings: " + runSettings);20 Console.WriteLine("TestSettings: " + testSettings);21 Console.WriteLine("TestSettingsNode: " + testSettingsNode);22 Console.WriteLine("TestRunParameters: " + testRunParameters);23 Console.WriteLine("TestRunParametersNode: " + testRunParametersNode);24 Console.WriteLine("TestRunParametersNode2: " + testRunParametersNode2);25 Console.WriteLine("TestRunParametersNode3: " + testRunParametersNode5);26 Console.ReadLine();27 }28 }29}30using Microsoft.VisualStudio.TestPlatform.ObjectModel;31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36{37 {38 static void Main(string[] args)39 {40 var runSettingsHelper = new RunSettingsHelper();41 var runSettings = runSettingsHelper.GetRunSettings(args[0]);42 var testSettings = runSettingsHelper.GetTestSettings(runSettings);43 var testSettingsNode = runSettingsHelper.GetTestSettingsNode(runSettings);44 var testRunParameters = runSettingsHelper.GetTestRunParameters(runSettings);45 var testRunParametersNode = runSettingsHelper.GetTestRunParametersNode(runSettings);46 var testRunParametersNode2 = runSettingsHelper.GetTestRunParametersNode(runSettings, "TestRunParameters");
RunSettingsHelper
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8{9 {10 static void Main(string[] args)11 {12 RunSettingsHelper helper = new RunSettingsHelper();13 var settings = helper.GetRunSettings(args[0]);14 var dataCollectionRunSettings = helper.GetDataCollectionRunSettings(settings);15 var dataCollectionSettings = dataCollectionRunSettings.DataCollectors;16 foreach (var dataCollectorSettings in dataCollectionSettings)17 {18 Console.WriteLine(dataCollectorSettings.FriendlyName);19 }20 Console.ReadLine();21 }22 }23}
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;
RunSettingsHelper
Using AI Code Generation
1{2 {3 public void TestMethod1()4 {5 }6 }7}8using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;9RunSettingsHelper runSettingsHelper = new RunSettingsHelper();10string runSettings = runSettingsHelper.GetRunSettings();11RunSettingsHelper runSettingsHelper = new RunSettingsHelper();12string value = runSettingsHelper.GetPropertyValue("MyProperty");13RunSettingsHelper runSettingsHelper = new RunSettingsHelper();14string value = runSettingsHelper.GetPropertyValue("MyProperty");15RunSettingsHelper runSettingsHelper = new RunSettingsHelper();16List<string> properties = runSettingsHelper.GetProperties();17{18{19static void Main(string[] args)20{21</RunSettings>";22RunSettingsHelper runSettingsHelper = new RunSettingsHelper(runSettingsXml);23Console.WriteLine("ResultsDirectory: " + runSettingsHelper.ResultsDirectory);24Console.ReadLine();25}26}27}
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;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 IRunSettingsHelper runSettingsHelper = new RunSettingsHelper();13 string runSettings = runSettingsHelper.GetRunSettings(@"C:\Users\Public\Documents\Visual Studio 2017\RunSettings\runsettings.runsettings");14 Console.WriteLine(runSettings);15 }16 }17}
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using System;3{4 {5 static void Main(string[] args)6 {7 var runSettings = new RunSettingsHelper();8 runSettings.LoadSettings(args[0]);9 Console.WriteLine("Hello World!");10 }11 }12}13var runSettings = new RunSettingsHelper();14runSettings.LoadSettings(args[0]);15var runSettings = new RunSettingsHelper();16runSettings.LoadSettings(args[0]);17var runSettingsData = runSettings.GetRunSettingsData();18var testSettings = runSettingsData.TestRunParameters;19var runSettings = new RunSettingsHelper();20runSettings.LoadSettings(args[0]);21var runSettings = new RunSettingsHelper();22runSettings.LoadSettings(args[0]);23var runSettingsData = runSettings.GetRunSettingsData();24var testSettings = runSettingsData.TestRunParameters;
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;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 IRunSettingsHelper runSettingsHelper = new RunSettingsHelper();13 string runSettings = runSettingsHelper.GetRunSettings(@"C:\Users\Public\Documents\Visual Studio 2017\RunSettings\runsettings.runsettings");14 Console.WriteLine(runSettings);15 }16 }17}
RunSettingsHelper
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;2using System;3{4 {5 static void Main(string[] args)6 {7 var runSettings = new RunSettingsHelper();8 runSettings.LoadSettings(args[0]);9 Console.WriteLine("Hello World!");10 }11 }12}13var runSettings = new RunSettingsHelper();14runSettings.LoadSettings(args[0]);15var runSettings = new RunSettingsHelper();16runSettings.LoadSettings(args[0]);17var runSettingsData = runSettings.GetRunSettingsData();18var testSettings = runSettingsData.TestRunParameters;19var runSettings = new RunSettingsHelper();20runSettings.LoadSettings(args[0]);21var runSettings = new RunSettingsHelper();22runSettings.LoadSettings(args[0]);23var runSettingsData = runSettings.GetRunSettingsData();24var testSettings = runSettingsData.TestRunParameters;
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!!