From 0b4285d93a69f5045edc55493da52838083ffc0c Mon Sep 17 00:00:00 2001 From: Bhuvansh Kataria Date: Sun, 29 Nov 2026 21:03:15 +0000 Subject: [PATCH] Add e2e debug mode tests for pytest and unittest execution adapters --- .../testing/common/testingAdapter.test.ts | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/src/test/testing/common/testingAdapter.test.ts b/src/test/testing/common/testingAdapter.test.ts index d73efba2a8ec..127e96156aa3 100644 --- a/src/test/testing/common/testingAdapter.test.ts +++ b/src/test/testing/common/testingAdapter.test.ts @@ -1230,4 +1230,121 @@ suite('End to End Tests: test adapters', () => { `Time per result should be minimal: ${timePerResult.toFixed(2)}ms per result (should be under 10ms)`, ); }); + + test('pytest execution adapter debug mode triggers debugLauncher with correct options', async () => { + // Arrange: wire up result resolver + resultResolver = new PythonResultResolver(testController, pytestProvider, workspaceUri); + workspaceUri = Uri.file(rootPathSmallWorkspace); + configService.getSettings(workspaceUri).testing.pytestArgs = []; + + const executionAdapter = new PytestTestExecutionAdapter(configService, resultResolver, envVarsService); + + const testRun = typeMoq.Mock.ofType(); + testRun + .setup((t) => t.token) + .returns(() => ({ onCancellationRequested: () => undefined } as any)); + testRun.setup((t) => t.appendOutput(typeMoq.It.isAny())).returns(() => false); + + // Stub the debug launcher so we don't need a real VS Code debug session. + let capturedLaunchOptions: unknown; + const debugLauncherStub = { + launchDebugger: sinon + .stub() + .callsFake((opts: unknown, _callback: () => void, _sessionOpts: unknown) => { + capturedLaunchOptions = opts; + _callback(); + return Promise.resolve(); + }), + }; + + // Act + await executionAdapter.runTests( + workspaceUri, + [`${rootPathSmallWorkspace}/test_simple.py::test_a`], + TestRunProfileKind.Debug, + testRun.object, + pythonExecFactory, + debugLauncherStub as any, + ); + + // Assert: debugLauncher must have been called exactly once + assert.strictEqual( + debugLauncherStub.launchDebugger.callCount, + 1, + 'Expected debugLauncher.launchDebugger to be called exactly once for a debug run', + ); + + // Assert: LaunchOptions carries correct test provider + const launchOpts = capturedLaunchOptions as Record; + assert.strictEqual( + launchOpts.testProvider, + PYTEST_PROVIDER, + `Expected testProvider to be '${PYTEST_PROVIDER}', got '${launchOpts.testProvider}'`, + ); + assert.ok(launchOpts.cwd, 'Expected cwd to be set in LaunchOptions'); + + // pytest debug mode must inject --capture=no so output is not swallowed + const args = launchOpts.args as string[]; + assert.ok(Array.isArray(args), 'Expected args to be an array in LaunchOptions'); + const hasCaptureDisabled = args.includes('-s') || args.some((a) => a.startsWith('--capture')); + assert.ok(hasCaptureDisabled, 'Expected pytest debug args to disable output capture (--capture=no or -s)'); + }); + + test('unittest execution adapter debug mode triggers debugLauncher with correct options', async () => { + // Arrange + resultResolver = new PythonResultResolver(testController, unittestProvider, workspaceUri); + workspaceUri = Uri.file(rootPathSmallWorkspace); + configService.getSettings(workspaceUri).testing.unittestArgs = ['-s', '.', '-p', '*test*.py']; + + const executionAdapter = new UnittestTestExecutionAdapter(configService, resultResolver, envVarsService); + + const testRun = typeMoq.Mock.ofType(); + testRun + .setup((t) => t.token) + .returns(() => ({ onCancellationRequested: () => undefined } as any)); + testRun.setup((t) => t.appendOutput(typeMoq.It.isAny())).returns(() => false); + + let capturedLaunchOptions: unknown; + const debugLauncherStub = { + launchDebugger: sinon + .stub() + .callsFake((opts: unknown, _callback: () => void, _sessionOpts: unknown) => { + capturedLaunchOptions = opts; + _callback(); + return Promise.resolve(); + }), + }; + + // Act + await executionAdapter.runTests( + workspaceUri, + ['test_simple.SimpleClass.test_simple_unit'], + TestRunProfileKind.Debug, + testRun.object, + pythonExecFactory, + debugLauncherStub as any, + ); + + // Assert: launcher called once + assert.strictEqual( + debugLauncherStub.launchDebugger.callCount, + 1, + 'Expected debugLauncher.launchDebugger to be called exactly once for a debug run', + ); + + // Assert: LaunchOptions carries correct test provider + const launchOpts = capturedLaunchOptions as Record; + assert.strictEqual( + launchOpts.testProvider, + UNITTEST_PROVIDER, + `Expected testProvider to be '${UNITTEST_PROVIDER}', got '${launchOpts.testProvider}'`, + ); + assert.ok(launchOpts.cwd, 'Expected cwd to be set in LaunchOptions'); + + // Assert: test IDs are forwarded through the pipe name + assert.ok( + launchOpts.runTestIdsPort, + 'Expected runTestIdsPort to be set so test IDs reach the debug session', + ); + }); });