1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+ using Microsoft . TestPlatform . VsTestConsole . TranslationLayer ;
5+ using Microsoft . VisualStudio . TestPlatform . ObjectModel ;
6+ using Microsoft . VisualStudio . TestPlatform . ObjectModel . Client ;
7+ using Microsoft . VisualStudio . TestPlatform . ObjectModel . Client . Interfaces ;
8+ using Microsoft . VisualStudio . TestPlatform . ObjectModel . Logging ;
9+
10+ using System ;
11+ using System . Collections . Generic ;
12+ using System . Diagnostics ;
13+ using System . IO ;
14+ using System . Linq ;
15+ using System . Reflection ;
16+ using System . Threading ;
17+
18+ namespace TestPlatform . Playground
19+ {
20+ internal class Program
21+ {
22+ static void Main ( string [ ] args )
23+ {
24+ // This project references TranslationLayer, vstest.console, TestHostProvider, testhost and MSTest1 projects, to make sure
25+ // we build all the dependencies of that are used to run tests via VSTestConsoleWrapper. It then copies the components from
26+ // their original build locations, to $(TargetDir)\vstest.console directory, and it's subfolders to create an executable
27+ // copy of TestPlatform that is similar to what we ship.
28+ //
29+ // The copying might trigger only on re-build, if you see outdated dependencies, Rebuild this project instead of just Build.
30+ //
31+ // Use this as playground for your debugging of end-to-end scenarios, it will automatically attach vstest.console and teshost
32+ // sub-processes. It won't stop at entry-point automatically, don't forget to set your breakpoints, or remove VSTEST_DEBUG_NOBP
33+ // from the environment variables of this project.
34+
35+ var thisAssemblyPath = Assembly . GetEntryAssembly ( ) . Location ;
36+ var here = Path . GetDirectoryName ( thisAssemblyPath ) ;
37+ var playground = Path . GetFullPath ( Path . Combine ( here , ".." , ".." , ".." , ".." ) ) ;
38+
39+ var console = Path . Combine ( here , "vstest.console" , "vstest.console.exe" ) ;
40+ var consoleOptions = new ConsoleParameters
41+ {
42+ LogFilePath = Path . Combine ( here , "logs" , "log.txt" ) ,
43+ TraceLevel = TraceLevel . Verbose ,
44+ } ;
45+
46+ var r = new VsTestConsoleWrapper ( console , consoleOptions ) ;
47+
48+ var sourceSettings = @"
49+ <RunSettings>
50+ <RunConfiguration>
51+ <InIsolation>true</InIsolation>
52+ </RunConfiguration>
53+ </RunSettings>
54+ " ;
55+ var sources = new [ ] {
56+ Path . Combine ( playground , "MSTest1" , "bin" , "Debug" , "net472" , "MSTest1.dll" )
57+ } ;
58+
59+ var options = new TestPlatformOptions ( ) ;
60+ r . RunTestsWithCustomTestHost ( sources , sourceSettings , options , new TestRunHandler ( ) , new DebuggerTestHostLauncher ( ) ) ;
61+ }
62+
63+ public class TestRunHandler : ITestRunEventsHandler
64+ {
65+
66+ public TestRunHandler ( )
67+ {
68+ }
69+
70+ public void HandleLogMessage ( TestMessageLevel level , string message )
71+ {
72+ Console . WriteLine ( $ "[{ level . ToString ( ) . ToUpper ( ) } ]: { message } ") ;
73+ }
74+
75+ public void HandleRawMessage ( string rawMessage )
76+ {
77+ Console . WriteLine ( $ "[MESSAGE]: { rawMessage } ") ;
78+ }
79+
80+ public void HandleTestRunComplete ( TestRunCompleteEventArgs testRunCompleteArgs , TestRunChangedEventArgs lastChunkArgs , ICollection < AttachmentSet > runContextAttachments , ICollection < string > executorUris )
81+ {
82+ Console . WriteLine ( $ "[COMPLETE]: err: { testRunCompleteArgs . Error } , lastChunk: { WriteTests ( lastChunkArgs ? . NewTestResults ) } ") ;
83+ }
84+
85+ public void HandleTestRunStatsChange ( TestRunChangedEventArgs testRunChangedArgs )
86+ {
87+ Console . WriteLine ( $ "[PROGRESS - NEW RESULTS]: { WriteTests ( testRunChangedArgs . NewTestResults ) } ") ;
88+ }
89+
90+ public int LaunchProcessWithDebuggerAttached ( TestProcessStartInfo testProcessStartInfo )
91+ {
92+ throw new NotImplementedException ( ) ;
93+ }
94+
95+ private string WriteTests ( IEnumerable < TestResult > testResults )
96+ {
97+ return WriteTests ( testResults ? . Select ( t => t . TestCase ) ) ;
98+ }
99+
100+ private string WriteTests ( IEnumerable < TestCase > testCases )
101+ {
102+ return testCases == null ? null : "\t " + string . Join ( "\n \t " , testCases . Select ( r => r . DisplayName ) ) ;
103+ }
104+ }
105+
106+ internal class DebuggerTestHostLauncher : ITestHostLauncher2
107+ {
108+ public bool IsDebug => true ;
109+
110+ public bool AttachDebuggerToProcess ( int pid )
111+ {
112+ return true ;
113+ }
114+
115+ public bool AttachDebuggerToProcess ( int pid , CancellationToken cancellationToken )
116+ {
117+ return true ;
118+ }
119+
120+ public int LaunchTestHost ( TestProcessStartInfo defaultTestHostStartInfo )
121+ {
122+ return 1 ;
123+ }
124+
125+ public int LaunchTestHost ( TestProcessStartInfo defaultTestHostStartInfo , CancellationToken cancellationToken )
126+ {
127+ return 1 ;
128+ }
129+ }
130+ }
131+ }
0 commit comments