-
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: Unable to run tests for non-installed modules #15062
Comments
Hi @MKuranowski, thank you for reaching out and your thorough research! I can repro your issue, this seems to be a regression (it used to work), marking it as such. Thank you again for the report! |
Hi,
Further, adding this setting allows py.test tests to run from command line. Note, pytest is installed in my virtual environment.
However, I am unable to get the tests to run successfully using the testing interface in vs code. They all fail with import errors. I have gone round and round on this, but nothing seems to work. I initially tried adding a .venv file pointing PYTHONPATH to the current working folder which did not work. Next, I set the following settings:
Neither of which worked. Finally, I made a second copy of my project and pointed my .env file to the copied project folder and finally it worked. Therefore, it does seem that the .env is picked up, but there is no way to add the current working directory to the PYTHONPATH or that this is somehow overwritten. If it helps, I can provide a more complete example, but it would be quite similar to the one posted here. Note that the Python Text Explorer extension appears to find everything appropriately. Is there any update on this issue? |
Hi @nss350, No progress on this issue yet, feel free to 👍 it to help bumping up its priority. Thanks! |
I believe I have a related, but not identical issue. I have been downgrading to extension version |
Hey @MKuranowski, can you try adding a |
After some time I also found that workaround, so yes, adding |
Thanks for checking! |
Thanks @luabud I was having the same issue (exactly as described, but in Windows), adding the |
Forgive me if I don't understand, but is this because pytest run is not obeying the environment variables set in .vscode/.env when running the test? It seems case 3 seems to point to that. This seems related to how PYTHONPATH is set. |
@jaron-l I don't think so; |
@brettcannon What about "python.envFile"? Not being able to find packages is a python path issue so the different scenarios have different python paths. |
@jaron-l if you put the |
I am having the same issue with an editable install ( I am just wondering why i can easily run How does the extension invoke pytest? |
It sounds like it could be the same as this issue: #14579 |
As suggested in #14570 (comment) using the user setting |
Also having this issue in Version 1.55.0 on Windows x64 and python 3.7.
|
I'm using a devcontainer and I'm having the same issue with my modules. Our tests on the command line also use I got the test discovery to work as well by adding |
I wrestled with this today and ended up having a |
Hello! I have reviewed this issue and given the architectural changes of the testing rewrite, this issue should no longer exist on the rewrite. Therefore I am going to close this issue but please comment or open a new issue if you are still seeing a problem when you try this yourself! To use the rewrite yourself just add Thanks! |
Environment data
python.languageServer
setting:Pylance
Setup
Consider the following project layout:
The contents of those files don't really matter, expect that
tests/test_foo.py
importssomepkg
.Example
.vscode/settings.json
:Example
somepkg/__init__.py
:Example
tests/test_foo.py
:With such project layout, it is impossible to setup VS Code to run those tests without also installing
somepkg
(for example in a venv).Here are solutions that I have tried:
Case 0: Do nothing
Obviously, this doesn't work, because invoking bare
pytest
doesn't add current directory to sys.pathVS Code fails to discover tests:
And similarly command pytest fails:
Case 1: python -m pytest
This is the solution from pytest docs. I added
"python.testing.pytestPath": "python -m pytest"
to .vscode/settings.json and tried to launch tests.VS Code, however, still fails on discovery.
And running pytest manually works fine.
Case 2: Absolute PYTHONPATH
Here, two new keys were added to the original settings.json:
"python.envFile": "${workspaceFolder}/.vscode/.env"
and"terminal.integrated.env.linux": { "PYTHONPATH": "${workspaceFolder}" }
; and the.vscode/.env
file was created with the following content. The path is hard-coded to circumvent issue #13749.And now test discovery works! But then, running the tests errors out.
And of course, pytest works like a charm:
Case 3: relative PYTHONPATH
This time settings.json looks like this:
And .vscode/.env contains only
PYTHONPATH=.
VS Code now correctly discovers the tests, but crashes when trying to run them:
And, yet again, pytest works fine:
Expected behavior
I would expect that modifications from cases 1, 2 and 3 allow testing non-installed modules.
Related issues
#6891 - that's not the case. starting from case 3, I have created a separate test in
tests/test_env.py
:And it passes correctly.
#12420 - there "Run tests" work, here it doesn't
The text was updated successfully, but these errors were encountered: