-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Pytest running doesn't take into consideration env. #6891
Comments
Same here: |
Unfortunately I'm unable to replicate this.
import os
def test_case_one():
with open("log.log", "w") as fp:
fp.write(os.getenv("WOW", "not found"))
{
"python.testing.pytestArgs": ["tests"],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true
}
|
@singleheart What do you mean by the following.
|
How to replicate with Python extension version since 2019.6.22090:
Now test discovery failed with:
This problem did not happen before 2019.5.18875. |
@singleheart please could you create a separate issue for what you are facing. It completely different. |
I have created a new issue: #6915 <- This problem has been solved now. PYTHONPATH must be relative not absolute. |
@DonJayamanne Here is the results of testing this, which sadly result in the same behaviour. In the following screen you can see the new workspace I created with the file structure on the explorer, the test case, workspace settings, .env file and resulting log file. Also set a breakpoint in the test and checked the contents of
Edit: |
@CMLL Please could you test this once again with the current development build. When you run the tests how are you doing this? Are you debugging or just running them (what button/command are you using)? Or are you trying both options. I ask as we fixed a bug related to env variables in the debugger recently. |
@DonJayamanne I installed version 2019.10.37374-dev from the link provided but still the same test case fails. I'm using the test test panel to discover and execute the tests. Using either the upper run all tests button and the Run Test link above the specific test fails. I tried with the Debug Test link and it also fails, but I normally don't use this to run tests. |
|
Does test discovery take into account variables in |
@nickcointracker yes, it does when you specify the path to the .env file in your
Does that help you? |
Thanks @luabud |
@nickcointracker I believe it should, yes. Please do open an issue following the template there and providing repro steps. If this is the current expected behavior, then the team can just change the label to a feature request :) |
I'm facing the same issue here. I change both PATH and PYTHONPATH in my env file on Windows to add few runtime dependencies. Somehow the PYTHONPATH change in the env file is making it through the debugger startup but not PATH. My suspicion is that the PATH forwarding is lost somehow in the child process layers to start up the debugger. |
@brgirgis is the issue you're facing related to debugging tests or "regular" debugging? |
From debug tests. Either from the annotations above the tests or from the dedicated GUI button to debug tests |
I use a combination of conda and poetry to create virtual environments. Pytest is always listed as a development dependency in my I noticed that when I change my interpreter, the I wonder if others facing this problem are seeing the same thing. |
@nealkruis check the logs to see if you are in the Is your |
@karthiknadig how will this appear in the logs? Is it in the Extension Host Log? |
Thanks! Yes, I "belong to experiment group 'DeprecatePythonPath - experiment'". That said, what behavior should I expect related to pytest when I change interpreters? I would hope that VS Code would be smart enough to use the pytest from the corresponding interpreter's environment. If I can manually confirm that setting the pytestPath to be the same directory as the python interpreter resolves my issue. (Again, not clear it is the same issue described by the OP.) |
@nealkruis When in that experiment group, the selected python path is saved by the extension in an internal cache. Python extension should use the pytest that is installed in the selected environment. The only reasons I can think of why this does not work is: 1) an explicit path is set for the 'pytestPath' setting 2) For some reason the environment is not activated. One way you can work around this is start VS Code from an activated environment. That should allow the extension to pick the correct pytest, assuming no explicit path has been set using |
@CMLL I ran the command And ready! |
I use pytest-env and tox.ini
|
Thanks for this. It is a better solution for me, with the benefits that it ports over nicely when used for dev and CI/CD processes. |
What if it's a typed-in variable at run time that doesn't come from a file? How do I get the test runner to inject this? i.e. |
Hello, I am facing the same issue. On one computer it works, on another it doesn't. Both run VS Code insiders, updated daily. Both have exact same .vscode/settings.json. one loads the .env variables correctly in integrated python (python test runnter), the other doesn't. Any hints for debugging this? EDIT: also installing python-dotenv does not alleviate this. I can confirm that python-dotenv works (when running pytest from commandline), but when I run the tests using the sidebar, it seems like VS Code is overwriting all env variables with some internal state, hence also resetting PYTHONPATH to "." |
I'd say this has been a substantial time sink in my time using VS Code and configuring dev containers. I need a config specifically for the container, and this would be a simple solution if it actually worked correctly! It's dead trivial to reproduce. Stick a library root anywhere non-standard and then try to use it with PYTHONPATH via the I would say the terminal activating your virtual environment but not respecting |
I am facing a similar issue to #6891 (comment) where the I am able to replicate the issue like this:
|
As far as I am aware, PYTHONPATH has never supported relative paths...? |
It does. I have a bunch of repos with PYTHONPATH=. in the environment file |
Hello! Can you try this on the rewrite for testing and if it is still broken please attach logs from the To use the rewrite yourself just add Thanks! |
@eleanorjboyd I tried this from the project described in #6891 (comment), but the issue persists. First the following log messages are printed: 2023-12-12 20:31:57.077 [info] Discover tests for workspace name: python-vscode-bug - uri: /path/to/project
2023-12-12 20:31:57.077 [info] Running discovery for pytest using the new test adapter. Then the following log messages are printed repeatedly: 2023-12-12 20:32:38.173 [debug] Clearing context for python dependencies not installed: ms-python.python.Python
2023-12-12 20:32:38.174 [debug] Found cached env for /path/to/project/.venv |
looks like it might be related to this: #22618 |
@eleanorjboyd I installed the pre-release version and test discovery is working for me now. Thanks! Which release is expected to include this fix? |
@alsuarez there is a point release out on stable now with the fix, it is version 2023.22.1. Glad that fixed it! |
Environment data
"python.jediEnabled"
set to; more info How to update the language server to the latest stable version #3977): LanguageServerExpected behaviour
Test discovery and running have the enviroment variables declared in the
.env
file or theterminal.integrated.env.linux
settings before executing.Actual behaviour
Tests that depend on a specific environment variable being set to run fail.
Steps to reproduce:
.env
file or interminal.integrated.env.linux
Logs
Output for
Python
in theOutput
panel (View
→Output
, change the drop-down the upper-right of theOutput
panel toPython
)In this output I'm running a test that depends on a variable
PANOPTA_INTEGRATION_TESTS_CONFIG
to point to a file, and if not found it defaults to this file/etc/panopta_tests_support_data.yaml
, which doesn't exist.I have declared said variable in an .env file at my workspace root.
PANOPTA_INTEGRATION_TESTS_CONFIG=/home/cllamach/Panopta/classic/src/models/tests/integration/support_data_config.yaml
But when adding log statements to pytest run module I see it tries to load the default file instead of the one declared in the .env file. I ran into a similar problem with the PYTHONPATH variable not being taken into account when declared into the .env file and was able to bypass it by installing pytest-pythonpath to declare its values there.
Output from
Console
under theDeveloper Tools
panel (toggle Developer Tools on underHelp
; turn on source maps to make any tracebacks be useful by runningEnable source map support for extension debugging
)The text was updated successfully, but these errors were encountered: