-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Python Environments Project Support for Unittest Execution #25779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python Environments Project Support for Unittest Execution #25779
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds project-based support for unittest execution/discovery by using the Python Environments “project” model, ensuring the correct interpreter is used per project and that debug sessions are launched with project context.
Changes:
- Pass
project.pythonProjectinto debug launch options for unittest (enables project-derived Python path/session naming). - Prefer
project.pythonEnvironmentovergetEnvironment(uri)when running discovery/execution via the env extension (pytest + unittest). - Enable project-based execution in the test controller whenever projects exist (not only when pytest is enabled), plus add a targeted unittest execution unit test.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts | Adds unit tests for project-aware unittest debug options and env-extension execution using the project environment. |
| src/client/testing/testController/unittest/testExecutionAdapter.ts | Passes project into debug launcher and uses project Python environment when useEnvExtension() is enabled. |
| src/client/testing/testController/unittest/testDiscoveryAdapter.ts | Uses project Python environment for env-extension-based unittest discovery. |
| src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts | Uses project Python environment for env-extension-based pytest discovery. |
| src/client/testing/testController/controller.ts | Switches to project-based execution whenever projects exist in the workspace. |
| src/client/testing/testController/common/testDiscoveryHandler.ts | Adds a unittest project-mode warning for likely nested-project import errors during discovery. |
| src/client/testing/common/debugLauncher.ts | Allows default debug cwd to come from the launch options (supports project-root defaulting). |
Comments suppressed due to low confidence (3)
src/client/testing/testController/controller.ts:805
- Project-based execution path bypasses the per-provider enablement checks. With this change, if
hasProjects()is true, tests will be executed even whensettings.testing.pytestEnabled/unittestEnabledare false (e.g. user disables a framework after discovery, but items still exist). Consider gating project execution on the relevant enabled setting(s) and/or filteringprojectsbyproject.testProvideragainst the current settings before callingexecuteTestsForProjects; otherwise fall back tounconfiguredWorkspacesbehavior.
// Check if we're in project-based mode and should use project-specific execution
if (this.projectRegistry.hasProjects(workspace.uri)) {
const projects = this.projectRegistry.getProjectsArray(workspace.uri);
await executeTestsForProjects(projects, testItems, runInstance, request, token, {
projectRegistry: this.projectRegistry,
pythonExecFactory: this.pythonExecFactory,
debugLauncher: this.debugLauncher,
});
return;
}
src/client/testing/testController/unittest/testDiscoveryAdapter.ts:104
- The new project-aware environment selection (
project?.pythonEnvironment ?? getEnvironment(uri)) in the env-extension execution path is not covered by unit tests for unittest discovery. Add a test that passes aProjectAdapterwith a knownpythonEnvironmentand assertsrunInBackgroundis invoked with that environment (and thatgetEnvironment(uri)is not used).
// Execute using environment extension if available
if (useEnvExtension()) {
traceInfo(`Using environment extension for unittest discovery in workspace ${uri.fsPath}`);
const pythonEnv = project?.pythonEnvironment ?? (await getEnvironment(uri));
if (!pythonEnv) {
traceError(
`Python environment not found for workspace ${uri.fsPath}. Cannot proceed with test discovery.`,
);
deferredTillExecClose.resolve();
return;
}
src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts:119
- The new project-aware environment selection (
project?.pythonEnvironment ?? getEnvironment(uri)) in the env-extension execution path is not covered by unit tests for pytest discovery. Add a test that passes aProjectAdapterwith a knownpythonEnvironmentand assertsrunInBackgroundis invoked with that environment (and thatgetEnvironment(uri)is not used).
// Execute using environment extension if available
if (useEnvExtension()) {
traceInfo(`Using environment extension for pytest discovery in workspace ${uri.fsPath}`);
const pythonEnv = project?.pythonEnvironment ?? (await getEnvironment(uri));
if (!pythonEnv) {
traceError(
`Python environment not found for workspace ${uri.fsPath}. Cannot proceed with test discovery.`,
);
deferredTillExecClose.resolve();
return;
}
src/client/testing/testController/common/testDiscoveryHandler.ts
Outdated
Show resolved
Hide resolved
src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts
Outdated
Show resolved
Hide resolved
f048ef4
into
microsoft:test-project-support
No description provided.