Best Vstest code snippet using TestPlatform.Playground.Program.WriteTests
Program.cs
Source:Program.cs  
...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        }219        public void HandleTestRunComplete(TestRunCompleteEventArgs testRunCompleteArgs, TestRunChangedEventArgs? lastChunkArgs, ICollection<AttachmentSet>? runContextAttachments, ICollection<string>? executorUris)220        {221            Console.WriteLine($"[RUN.COMPLETE]: err: {testRunCompleteArgs.Error}, lastChunk:");222            if (_detailedOutput)223            {224                Console.WriteLine(WriteTests(lastChunkArgs?.NewTestResults));225            }226        }227        public void HandleTestRunStatsChange(TestRunChangedEventArgs? testRunChangedArgs)228        {229            if (_detailedOutput)230            {231                Console.WriteLine($"[RUN.PROGRESS]");232                Console.WriteLine(WriteTests(testRunChangedArgs?.NewTestResults));233            }234        }235        public int LaunchProcessWithDebuggerAttached(TestProcessStartInfo testProcessStartInfo)236        {237            throw new NotImplementedException();238        }239        private static string WriteTests(IEnumerable<TestResult>? testResults)240            => WriteTests(testResults?.Select(t => t.TestCase));241        private static string WriteTests(IEnumerable<TestCase>? testCases)242            => testCases?.Any() == true243                ? "\t" + string.Join("\n\t", testCases.Select(r => r.DisplayName))244                : "\t<empty>";245    }246    internal class DebuggerTestHostLauncher : ITestHostLauncher2247    {248        public bool IsDebug => true;249        public bool AttachDebuggerToProcess(int pid)250        {251            return true;252        }253        public bool AttachDebuggerToProcess(int pid, CancellationToken cancellationToken)254        {255            return true;...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!!
