2121// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222// ***********************************************************************
2323
24- // #define LAUNCHDEBUGGER
2524
2625using System ;
2726using System . Collections . Generic ;
2827using System . Diagnostics ;
2928using System . IO ;
3029using System . Linq ;
3130using System . Reflection ;
31+
3232using Microsoft . VisualStudio . TestPlatform . ObjectModel ;
3333using Microsoft . VisualStudio . TestPlatform . ObjectModel . Adapter ;
34+
3435using NUnit . Engine ;
3536using NUnit . VisualStudio . TestAdapter . Dump ;
3637using NUnit . VisualStudio . TestAdapter . Internal ;
@@ -57,7 +58,8 @@ public enum RunType
5758
5859
5960 [ ExtensionUri ( ExecutorUri ) ]
60- public sealed class NUnit3TestExecutor : NUnitTestAdapter , ITestExecutor , IDisposable , INUnit3TestExecutor , IExecutionContext
61+ public sealed class NUnit3TestExecutor : NUnitTestAdapter , ITestExecutor , IDisposable , INUnit3TestExecutor ,
62+ IExecutionContext
6163 {
6264 #region Properties
6365
@@ -96,17 +98,15 @@ public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDispo
9698 /// <param name="frameworkHandle">Test log to send results and messages through.</param>
9799 public void RunTests ( IEnumerable < string > sources , IRunContext runContext , IFrameworkHandle frameworkHandle )
98100 {
99- #if LAUNCHDEBUGGER
100- if ( ! Debugger . IsAttached )
101- Debugger . Launch ( ) ;
102- #endif
103101 Initialize ( runContext , frameworkHandle ) ;
102+ CheckIfDebug ( ) ;
104103 TestLog . Debug ( "RunTests by IEnumerable<string>" ) ;
105104 InitializeForExecution ( runContext , frameworkHandle ) ;
106105
107106 if ( Settings . InProcDataCollectorsAvailable && sources . Count ( ) > 1 )
108107 {
109- TestLog . Error ( "Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration." ) ;
108+ TestLog . Error (
109+ "Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration." ) ;
110110 Unload ( ) ;
111111 return ;
112112 }
@@ -119,18 +119,22 @@ public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrame
119119 var vsTestFilter = VsTestFilterFactory . CreateVsTestFilter ( Settings , runContext ) ;
120120 filter = builder . ConvertVsTestFilterToNUnitFilter ( vsTestFilter ) ;
121121 }
122+
122123 filter ??= builder . FilterByWhere ( Settings . Where ) ;
123124
124125 foreach ( string assemblyName in sources )
125126 {
126127 try
127128 {
128- string assemblyPath = Path . IsPathRooted ( assemblyName ) ? assemblyName : Path . Combine ( Directory . GetCurrentDirectory ( ) , assemblyName ) ;
129+ string assemblyPath = Path . IsPathRooted ( assemblyName )
130+ ? assemblyName
131+ : Path . Combine ( Directory . GetCurrentDirectory ( ) , assemblyName ) ;
129132 RunAssembly ( assemblyPath , null , filter ) ;
130133 }
131134 catch ( Exception ex )
132135 {
133136 if ( ex is TargetInvocationException ) { ex = ex . InnerException ; }
137+
134138 TestLog . Warning ( "Exception thrown executing tests" , ex ) ;
135139 }
136140 }
@@ -156,11 +160,8 @@ private void SetRunTypeByStrings() =>
156160 /// <param name="frameworkHandle">The FrameworkHandle.</param>
157161 public void RunTests ( IEnumerable < TestCase > tests , IRunContext runContext , IFrameworkHandle frameworkHandle )
158162 {
159- #if LAUNCHDEBUGGER
160- if ( ! Debugger . IsAttached )
161- Debugger . Launch ( ) ;
162- #endif
163163 Initialize ( runContext , frameworkHandle ) ;
164+ CheckIfDebug ( ) ;
164165 TestLog . Debug ( "RunTests by IEnumerable<TestCase>" ) ;
165166 InitializeForExecution ( runContext , frameworkHandle ) ;
166167 RunType = RunType . Ide ;
@@ -170,7 +171,8 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
170171 var assemblyGroups = tests . GroupBy ( tc => tc . Source ) ;
171172 if ( IsInProcDataCollectorsSpecifiedWithMultipleAssemblies ( assemblyGroups ) )
172173 {
173- TestLog . Error ( "Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration." ) ;
174+ TestLog . Error (
175+ "Failed to run tests for multiple assemblies when InProcDataCollectors specified in run configuration." ) ;
174176 Unload ( ) ;
175177 return ;
176178 }
@@ -181,7 +183,9 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
181183 try
182184 {
183185 string assemblyName = assemblyGroup . Key ;
184- string assemblyPath = Path . IsPathRooted ( assemblyName ) ? assemblyName : Path . Combine ( Directory . GetCurrentDirectory ( ) , assemblyName ) ;
186+ string assemblyPath = Path . IsPathRooted ( assemblyName )
187+ ? assemblyName
188+ : Path . Combine ( Directory . GetCurrentDirectory ( ) , assemblyName ) ;
185189
186190 var filterBuilder = CreateTestFilterBuilder ( ) ;
187191 var filter = filterBuilder . FilterByList ( assemblyGroup ) ;
@@ -191,6 +195,7 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
191195 catch ( Exception ex )
192196 {
193197 if ( ex is TargetInvocationException ) { ex = ex . InnerException ; }
198+
194199 TestLog . Warning ( "Exception thrown executing tests" , ex ) ;
195200 }
196201
@@ -202,7 +207,8 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
202207 Unload ( ) ;
203208 }
204209
205- private bool IsInProcDataCollectorsSpecifiedWithMultipleAssemblies ( IEnumerable < IGrouping < string , TestCase > > assemblyGroups )
210+ private bool IsInProcDataCollectorsSpecifiedWithMultipleAssemblies (
211+ IEnumerable < IGrouping < string , TestCase > > assemblyGroups )
206212 => Settings . InProcDataCollectorsAvailable && assemblyGroups . Count ( ) > 1 ;
207213
208214 void ITestExecutor . Cancel ( )
@@ -244,7 +250,9 @@ public void InitializeForExecution(IRunContext runContext, IFrameworkHandle fram
244250
245251 if ( VsTestFilter . IsEmpty )
246252 {
247- if ( ! ( enableShutdown && ! runContext . KeepAlive ) ) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012
253+ if ( ! ( enableShutdown &&
254+ ! runContext
255+ . KeepAlive ) ) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012
248256 frameworkHandle . EnableShutdownAfterTestRun = enableShutdown ;
249257 }
250258
@@ -253,9 +261,7 @@ public void InitializeForExecution(IRunContext runContext, IFrameworkHandle fram
253261
254262 private void RunAssembly ( string assemblyPath , IGrouping < string , TestCase > testCases , TestFilter filter )
255263 {
256- string actionText = Debugger . IsAttached ? "Debugging " : "Running " ;
257- string selectionText = filter == null || filter == TestFilter . Empty ? "all" : "selected" ;
258- TestLog . Info ( actionText + selectionText + " tests in " + assemblyPath ) ;
264+ LogActionAndSelection ( assemblyPath , filter ) ;
259265 RestoreRandomSeed ( assemblyPath ) ;
260266 Dump = DumpXml . CreateDump ( assemblyPath , testCases , Settings ) ;
261267
@@ -279,9 +285,7 @@ private void RunAssembly(string assemblyPath, IGrouping<string, TestCase> testCa
279285 }
280286 else
281287 {
282- TestLog . Info ( discoveryResults . HasNoNUnitTests
283- ? " NUnit couldn't find any tests in " + assemblyPath
284- : " NUnit failed to load " + assemblyPath ) ;
288+ TestLog . InfoNoTests ( discoveryResults . HasNoNUnitTests , assemblyPath ) ;
285289 }
286290 }
287291 catch ( Exception ex ) when ( ex is BadImageFormatException || ex . InnerException is BadImageFormatException )
@@ -292,7 +296,8 @@ private void RunAssembly(string assemblyPath, IGrouping<string, TestCase> testCa
292296 catch ( FileNotFoundException ex )
293297 {
294298 // Probably from the GetExportedTypes in NUnit.core, attempting to find an assembly, not a problem if it is not NUnit here
295- TestLog . Warning ( " Dependent Assembly " + ex . FileName + " of " + assemblyPath + " not found. Can be ignored if not an NUnit project." ) ;
299+ TestLog . Warning ( " Dependent Assembly " + ex . FileName + " of " + assemblyPath +
300+ " not found. Can be ignored if not an NUnit project." ) ;
296301 }
297302 catch ( Exception ex )
298303 {
@@ -312,12 +317,18 @@ private void RunAssembly(string assemblyPath, IGrouping<string, TestCase> testCa
312317 // can happen if CLR throws CannotUnloadAppDomainException, for example
313318 // due to a long-lasting operation in a protected region (catch/finally clause).
314319 if ( ex is TargetInvocationException ) { ex = ex . InnerException ; }
320+
315321 TestLog . Warning ( $ " Exception thrown unloading tests from { assemblyPath } ", ex ) ;
316322 }
317323 }
318324 }
319325
320-
326+ private void LogActionAndSelection ( string assemblyPath , TestFilter filter )
327+ {
328+ string actionText = Debugger . IsAttached ? "Debugging " : "Running " ;
329+ string selectionText = filter == null || filter == TestFilter . Empty ? "all" : "selected" ;
330+ TestLog . Info ( actionText + selectionText + " tests in " + assemblyPath ) ;
331+ }
321332
322333
323334 private void RestoreRandomSeed ( string assemblyPath )
@@ -328,12 +339,7 @@ private void RestoreRandomSeed(string assemblyPath)
328339 }
329340
330341
331- private NUnitTestFilterBuilder CreateTestFilterBuilder ( )
332- {
333- return new ( NUnitEngineAdapter . GetService < ITestFilterService > ( ) , Settings ) ;
334- }
335-
336-
342+ private NUnitTestFilterBuilder CreateTestFilterBuilder ( ) => new ( NUnitEngineAdapter . GetService < ITestFilterService > ( ) , Settings ) ;
337343
338344
339345 private void CreateTestOutputFolder ( )
@@ -367,5 +373,13 @@ public void StopRun()
367373 }
368374
369375 public IDumpXml Dump { get ; private set ; }
376+
377+ private void CheckIfDebug ( )
378+ {
379+ if ( ! Settings . DebugExecution )
380+ return ;
381+ if ( ! Debugger . IsAttached )
382+ Debugger . Launch ( ) ;
383+ }
370384 }
371- }
385+ }
0 commit comments