Skip to content

Pytest doesn't run in venv on Remote workstation and so fails #24841

Open
@strubbly

Description

@strubbly

Type: Bug

Behaviour

Some of my pytest tests rely on environment variables set during venv activation. They work when run in the integrated terminal (because the venv is activated) and under debug (that is set to run in a Debug terminal and that too gets the venv activated). But they don't run in the Test Explorer - they fail because the environment variables are not set correctly.

My settings include

   "python.testing.unittestEnabled": false,
   "python.testing.pytestEnabled": true,
   "python.terminal.activateEnvInCurrentTerminal": true,

I have tried various permutations of

     "python.experiments.optOutFrom": [
        "pythonTestAdapter",
        "pythonTerminalEnvVarActivation"
    ],
    "terminal.integrated.inheritEnv": false,

but none of those seem to make any difference.

I am aware that similar bugs have been reported before and that fixes have been released but this is happening with the latest VS code and extension versions. The test explorer used to work correctly with these settings and much earlier versions - specifically Python v2022.8.1

Steps to reproduce:

  1. Use Remote SSH to log in to remote machine.
  2. Create a venv.
  3. Edit the venv activate script to set a new environment variable.
  4. Create python code including a test that checks for the new environment variable.
  5. Choose the venv python interpreter
  6. Execute the test from the Test Explorer

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2025-02-24 15:13:54.853 [info] Discover tests for workspace name: design - uri: /users/bens/Sources/design
2025-02-24 15:13:54.891 [info] arg: --rootdir already exists in args, not adding.
2025-02-24 15:13:54.891 [info] All environment variables set for pytest discovery: {"WORKSPACE":"design", ... }
2025-02-24 15:13:54.899 [info] > ./.venv/bin/python -m pytest -p vscode_pytest --collect-only --rootdir=./src/design-code/src
2025-02-24 15:13:54.900 [info] cwd: ./src/design-code/src
2025-02-24 15:13:57.618 [info] ============================= test session starts ==============================
platform linux -- Python 3.9.16, pytest-8.3.4, pluggy-1.5.0
2025-02-24 15:13:57.618 [info] 

2025-02-24 15:13:57.627 [info] rootdir: /users/bens/Sources/design/src/design-code/src
configfile: pytest.ini
2025-02-24 15:13:57.628 [info] 
plugins: typeguard-4.4.2, subprocess-1.5.3, cov-6.0.0

2025-02-24 15:14:04.407 [info] collected 389 items / 65 errors

The third line of the output includes a listing of environment variables but these do not include the ones that would have been set had the
activation taken place. I have redacted the detail.

Extension version: 2025.0.0
VS Code version: Code 1.97.2 (e54c774e0add60467559eb0d1e229c6452cf8447, 2025-02-12T23:20:35.343Z)
OS version: Darwin arm64 24.3.0
Modes:
Remote OS version: Linux x64 4.18.0-553.32.1.el8_10.x86_64

  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.16
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Default
User Settings


languageServer: "Pylance"

testing
• cwd: "<placeholder>"
• pytestArgs: "<placeholder>"
• pytestEnabled: true

experiments
• optOutFrom: ["pythonTestAdapter","pythonTerminalEnvVarActivation"]

Installed Extensions
Extension Name Extension Id Version
debugpy ms- 2025.0.1
gitlens eam 16.3.0
isort ms- 2023.10.1
js-debug ms- 1.97.1
live-server ms- 0.4.15
python ms- 2025.0.0
ruff cha 2025.10.0
skill her 0.10.0
vscode-coverage-gutters rya 2.13.0
vscode-edit-csv jan 0.11.2
vscode-js-profile-table ms- 1.0.10
vscode-pylance ms- 2025.2.1
vsliveshare ms- 1.0.5948
System Info
Item Value
CPUs Apple M2 (8 x 2400)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) 2, 2, 2
Memory (System) 8.00GB (0.11GB free)
Process Argv --crash-reporter-id 1726e94e-8d6f-469e-8e9f-16be0da7f3fd
Screen Reader no
VM 0%
Item Value
Remote SSH: workstation3
OS Linux x64 4.18.0-553.32.1.el8_10.x86_64
CPUs AMD EPYC 7571 (2 x 0)
Memory (System) 7.36GB (5.72GB free)
VM 0%
A/B Experiments
vsliv368cf:30146710
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscod805cf:30301675
binariesv615:30325510
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
a9j8j154:30646983
962ge761:30959799
2e7ec940:31000449
pythontbext0:30879054
cppperfnew:31000557
dwnewjupyter:31046869
nativerepl2:31139839
pythonrstrctxt:31112756
nativeloc1:31192215
iacca1:31171482
5fd0e150:31155592
dwcopilot:31170013
stablechunks:31184530
6074i472:31201624
dwoutputs:31242946
customenabled:31241370
9064b325:31222308
copilot_t_ci:31222730
jda6j935:31233686
copilothoveroff:31240707

Metadata

Metadata

Labels

area-testingbugIssue identified by VS Code Team member as probable bugtriage-neededNeeds assignment to the proper sub-team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions