@@ -269,27 +269,42 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
269269 if ( settings . testing . pytestEnabled ) {
270270 if ( pythonTestAdapterRewriteEnabled ( this . serviceContainer ) ) {
271271 traceInfo ( `Running discovery for pytest using the new test adapter.` ) ;
272- const testAdapter =
273- this . testAdapters . get ( uri ) || ( this . testAdapters . values ( ) . next ( ) . value as WorkspaceTestAdapter ) ;
274- testAdapter . discoverTests (
275- this . testController ,
276- this . refreshCancellation . token ,
277- this . pythonExecFactory ,
278- ) ;
272+ if ( workspace && workspace . uri ) {
273+ const testAdapter = this . testAdapters . get ( workspace . uri ) ;
274+ if ( testAdapter ) {
275+ testAdapter . discoverTests (
276+ this . testController ,
277+ this . refreshCancellation . token ,
278+ this . pythonExecFactory ,
279+ ) ;
280+ } else {
281+ traceError ( 'Unable to find test adapter for workspace.' ) ;
282+ }
283+ } else {
284+ traceError ( 'Unable to find workspace for given file' ) ;
285+ }
279286 } else {
280287 // else use OLD test discovery mechanism
281288 await this . pytest . refreshTestData ( this . testController , uri , this . refreshCancellation . token ) ;
282289 }
283290 } else if ( settings . testing . unittestEnabled ) {
284291 if ( pythonTestAdapterRewriteEnabled ( this . serviceContainer ) ) {
285292 traceInfo ( `Running discovery for unittest using the new test adapter.` ) ;
286- const testAdapter =
287- this . testAdapters . get ( uri ) || ( this . testAdapters . values ( ) . next ( ) . value as WorkspaceTestAdapter ) ;
288- testAdapter . discoverTests (
289- this . testController ,
290- this . refreshCancellation . token ,
291- this . pythonExecFactory ,
292- ) ;
293+ traceInfo ( `Running discovery for pytest using the new test adapter.` ) ;
294+ if ( workspace && workspace . uri ) {
295+ const testAdapter = this . testAdapters . get ( workspace . uri ) ;
296+ if ( testAdapter ) {
297+ testAdapter . discoverTests (
298+ this . testController ,
299+ this . refreshCancellation . token ,
300+ this . pythonExecFactory ,
301+ ) ;
302+ } else {
303+ traceError ( 'Unable to find test adapter for workspace.' ) ;
304+ }
305+ } else {
306+ traceError ( 'Unable to find workspace for given file' ) ;
307+ }
293308 } else {
294309 // else use OLD test discovery mechanism
295310 await this . unittest . refreshTestData ( this . testController , uri , this . refreshCancellation . token ) ;
0 commit comments