Best Vstest code snippet using TestPlatform.Playground.Program.TestRunHandler
Program.cs
Source:Program.cs  
...122        var discoveryDuration = sw.ElapsedMilliseconds;123        Console.WriteLine($"Discovery done in {discoveryDuration} ms");124        sw.Restart();125        // Run with test cases and custom testhost launcher126        r.RunTestsWithCustomTestHost(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput), new DebuggerTestHostLauncher());127        //// Run with test cases and without custom testhost launcher128        //r.RunTests(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput));129        //// Run with sources and custom testhost launcher130        //r.RunTestsWithCustomTestHost(sources, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput), new DebuggerTestHostLauncher());131        //// Run with sources132        //r.RunTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput));133        var rd = sw.ElapsedMilliseconds;134        Console.WriteLine($"Discovery: {discoveryDuration} ms, Run: {rd} ms, Total: {discoveryDuration + rd} ms");135        Console.WriteLine($"Settings:\n{sourceSettings}");136    }137    public class PlaygroundTestDiscoveryHandler : ITestDiscoveryEventsHandler, ITestDiscoveryEventsHandler2138    {139        private int _testCasesCount;140        private readonly bool _detailedOutput;141        public PlaygroundTestDiscoveryHandler(bool detailedOutput)142        {143            _detailedOutput = detailedOutput;144        }145        public List<TestCase> TestCases { get; internal set; } = new List<TestCase>();146        public void HandleDiscoveredTests(IEnumerable<TestCase>? discoveredTestCases)147        {148            if (_detailedOutput)149            {150                Console.WriteLine($"[DISCOVERY.PROGRESS]");151                Console.WriteLine(WriteTests(discoveredTestCases));152            }153            _testCasesCount += discoveredTestCases.Count();154            if (discoveredTestCases != null) { TestCases.AddRange(discoveredTestCases); }155        }156        public void HandleDiscoveryComplete(long totalTests, IEnumerable<TestCase>? lastChunk, bool isAborted)157        {158            Console.WriteLine($"[DISCOVERY.COMPLETE] aborted? {isAborted}, tests count: {totalTests}");159            if (_detailedOutput)160            {161                Console.WriteLine("Last chunk:");162                Console.WriteLine(WriteTests(lastChunk));163            }164            if (lastChunk != null) { TestCases.AddRange(lastChunk); }165        }166        public void HandleDiscoveryComplete(DiscoveryCompleteEventArgs discoveryCompleteEventArgs, IEnumerable<TestCase>? lastChunk)167        {168            Console.WriteLine($"[DISCOVERY.COMPLETE] aborted? {discoveryCompleteEventArgs.IsAborted}, tests count: {discoveryCompleteEventArgs.TotalCount}, discovered count: {_testCasesCount}");169            if (_detailedOutput)170            {171                Console.WriteLine("Last chunk:");172                Console.WriteLine(WriteTests(lastChunk));173            }174            Console.WriteLine("Fully discovered:");175            Console.WriteLine(WriteSources(discoveryCompleteEventArgs.FullyDiscoveredSources));176            Console.WriteLine("Partially discovered:");177            Console.WriteLine(WriteSources(discoveryCompleteEventArgs.PartiallyDiscoveredSources));178            Console.WriteLine("Skipped discovery:");179            Console.WriteLine(WriteSources(discoveryCompleteEventArgs.SkippedDiscoveredSources));180            Console.WriteLine("Not discovered:");181            Console.WriteLine(WriteSources(discoveryCompleteEventArgs.NotDiscoveredSources));182            if (lastChunk != null) { TestCases.AddRange(lastChunk); }183        }184        public void HandleLogMessage(TestMessageLevel level, string? message)185        {186            Console.WriteLine($"[DISCOVERY.{level.ToString().ToUpper(CultureInfo.InvariantCulture)}] {message}");187        }188        public void HandleRawMessage(string rawMessage)189        {190            Console.WriteLine($"[DISCOVERY.MESSAGE] {rawMessage}");191        }192        private static string WriteTests(IEnumerable<TestCase>? testCases)193            => testCases?.Any() == true194                ? "\t" + string.Join("\n\t", testCases?.Select(r => r.Source + " " + r.DisplayName))195                : "\t<empty>";196        private static string WriteSources(IEnumerable<string>? sources)197            => sources?.Any() == true198                ? "\t" + string.Join("\n\t", sources)199                : "\t<empty>";200    }201    public class TestRunHandler : ITestRunEventsHandler202    {203        private readonly bool _detailedOutput;204        public TestRunHandler(bool detailedOutput)205        {206            _detailedOutput = detailedOutput;207        }208        public void HandleLogMessage(TestMessageLevel level, string? message)209        {210            Console.WriteLine($"[{level.ToString().ToUpper(CultureInfo.InvariantCulture)}]: {message}");211        }212        public void HandleRawMessage(string rawMessage)213        {214            if (_detailedOutput)215            {216                Console.WriteLine($"[RUN.MESSAGE]: {rawMessage}");217            }218        }...TestRunHandler
Using AI Code Generation
1TestPlatform.Playground.Program.TestRunHandler("3.cs");2TestPlatform.Playground.Program.TestRunHandler("4.cs");3TestPlatform.Playground.Program.TestRunHandler("5.cs");4TestPlatform.Playground.Program.TestRunHandler("6.cs");5TestPlatform.Playground.Program.TestRunHandler("7.cs");6TestPlatform.Playground.Program.TestRunHandler("8.cs");7TestPlatform.Playground.Program.TestRunHandler("9.cs");8TestPlatform.Playground.Program.TestRunHandler("10.cs");9TestPlatform.Playground.Program.TestRunHandler("11.cs");10TestPlatform.Playground.Program.TestRunHandler("12.cs");11TestPlatform.Playground.Program.TestRunHandler("13.cs");12TestPlatform.Playground.Program.TestRunHandler("14.cs");13TestPlatform.Playground.Program.TestRunHandler("15.cs");14TestPlatform.Playground.Program.TestRunHandler("16.cs");TestRunHandler
Using AI Code Generation
1var testPlatform = new TestPlatform.Playground.Program();2testPlatform.TestRunHandler();3var testPlatform = new TestPlatform.Playground.Program();4testPlatform.TestRunHandler();5var testPlatform = new TestPlatform.Playground.Program();6testPlatform.TestRunHandler();7var testPlatform = new TestPlatform.Playground.Program();8testPlatform.TestRunHandler();9var testPlatform = new TestPlatform.Playground.Program();10testPlatform.TestRunHandler();11var testPlatform = new TestPlatform.Playground.Program();12testPlatform.TestRunHandler();13var testPlatform = new TestPlatform.Playground.Program();14testPlatform.TestRunHandler();15var testPlatform = new TestPlatform.Playground.Program();16testPlatform.TestRunHandler();17var testPlatform = new TestPlatform.Playground.Program();18testPlatform.TestRunHandler();19var testPlatform = new TestPlatform.Playground.Program();20testPlatform.TestRunHandler();21var testPlatform = new TestPlatform.Playground.Program();22testPlatform.TestRunHandler();23var testPlatform = new TestPlatform.Playground.Program();24testPlatform.TestRunHandler();25var testPlatform = new TestPlatform.Playground.Program();26testPlatform.TestRunHandler();TestRunHandler
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.ObjectModel;7using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;9using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;10using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;11using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;12using Microsoft.VisualStudio.TestPlatform.Common;13using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;14using Microsoft.VisualStudio.TestPlatform.Common.Logging;15using Microsoft.VisualStudio.TestPlatform.Common.Telemetry;16using Microsoft.VisualStudio.TestPlatform.Common.Telemetry.EventHandlers;17using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing.Interfaces;18using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;19using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection;20using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution;21using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;22using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Resources;23using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.TestRunAttachmentsProcessing;24using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Utilities;25using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;26using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.TesthostProtocol;27using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;28using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;29using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;30using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;31using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;32using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine;33using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Adapter;34using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Discovery;35using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution;36using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;37using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Resources;38using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.TestRunAttachmentsProcessing;39using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Utilities;40using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection;41using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;42using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.TesthostProtocol;43using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;44using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Helpers;45using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing;46using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing.Interfaces;47using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;48using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager;49using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.Utilities;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!!
