-
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
Test Discovery fails with E ModuleNotFoundError: No module named #10005
Comments
@zePanzerfaust, thanks for letting us know about this. We'll take a look as soon as we can. |
Test discovery should definitely be working for you. I was not able to reproduce the problem. I set up the following in my project:
With the above test discovery worked fine. So let's figure out what's going on. Please provide the content of your settings.json. Likewise for the "Python" output panel and the "Python test log" output panel. Also, do you have any pytest customizations (e.g. pytest.cfg)? Thanks! |
Hi, Settings:
Python test log:
Python Output:
|
@omershtivi1, thanks for the info. Keep in mind that while the outcome may be the same (ModuleNotFoundError), the cause may be different. Let's continue under the assumption that it is the same problem. If it turns out be something else then at that point we'll ask you to open a separate issue (for now there's no need). As to the info you gave us above, it looks like you put the content of you user settings.json. Please also provide your workspace settings.json ( I've also looked through the logs you provided and left a number of questions (and observations) below. In summary:
Python test log
This tells us how pytest was run. The adapter script the extension used is just a wrapper around pytest, which prints out a JSON summary of the discovered tests (which the extension consumes via stdout). The equivalent direct commands would be:
What happens when you run that in your terminal? The output matters. As noted, the extension expects the adapter script to only produce the JSON results, so any unexpected text will cause a problem. (The logs do not indicate a problem with that, but it's worth checking.) The "." at the end of the command is the directory where pytest will start looking for tests. What happens if you change it to "tests"? You can do so by putting the following in your settings.json:
The logs indicate you are using the latest version of pytest. Do you have this problem with earlier versions (e.g. 5.3.2, 5.1.3)? When did the problem start happening? The logs show that "rootdir" matches the command argument (which is your workspace root, right?). That is expected. The logs also indicate that you are using the "pylama" and "cov" pytest plugins. How did you enable them? What happens if you disable them? Also, what is in your pytest configuration? That's pretty much everything the test log tells us. Python OutputThe "Python output panel provides a few more bits of useful information. As you'll notice, I'm wondering if the problem is related to pylama. (It very well might not be related.)
These are the "experiments" you were randomly selected to be in. None of them are relevant to this issue though.
The language server provides things like code completion and navigation. It shouldn't impact pytest discovery though.
This is the first time test discovery is attempted. It matches all the other attempts and looks correct. Do you know if you were already seeing the problem at this point?
This is the first time the extension tried to lint your code (either because you just configured pylama or because you just saved a file or otherwise triggered the command). The extension could not find pylama. I expect it popped up a dialog asking if you wanted to install pylama.
This is roughly where pylama was installed.
The test discovery has changed at this point (nor will it later).
This is the first time pylama runs after it was installed. Note that it was able to find a module a few levels deep under the top-level "tests" directory.
This is the first place we see that pylama was able to find a module under a different top-level directory than "tests". (Salt is a pretty cool project, BTW. 😄)
Here we see that you started up the interactive window. That shouldn't have an impact on pytest discovery though.
This is the last time (in the log) that test discovery is attempted. It should correspond to the output you provided from the "Python test log". (It's also the last time pylama is run, but there doesn't seem to be much correlation with pytest problems.) |
There's also one specific thing you can do that may be helpful. In one of your test files put the following code: import sys
print(sys.path) Then during test discovery (which may fail due to unexpected output) you should see the module search path in your "Python test log" output. If that doesn't happen then put this instead (and look at the file): import sys
with open('/tmp/python-sys-path.txt', 'w') as outfile:
outfile.write(str(sys.path)) |
Hi,
I will start doing the other checks |
Settings file:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -m pytest --rootdir /Users/omer.shtivi/PycharmProjects/netops-cm -s .
Same error:
Earlier version: I don't have configuration for pytest (: |
Any idea how |
No clue. |
That sort of manipulation of sys.path is fragile and best avoided. The best solution is to figure out why |
Ok I feel a bit bad. It was a missing __init__.py in a subdirectory of the tests folder.
to the settings.json Now the test discovery works. |
@zePanzerfaust, well, I'm glad you were able to sort it out. 😄 |
Environment data
Version: 1.42.0
Commit: ae08d5460b5a45169385ff3fd44208f431992451
Date: 2020-02-06T10:51:33.119Z
Electron: 6.1.6
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Darwin x64 19.3.0
ms-python.python-2020.1.58038
Python 3.8.1
pytest 5.3.2
Expected behaviour
Test discovery finds tests and I'm able to run/debug them
Actual behaviour
Test discovery fails with "E ModuleNotFoundError: No module named 'mymodule'"
Steps to reproduce:
Project setup like this
Discovering tests using the command ">Python: Discover Tests"
python /<...>/ms-python.python-2020.1.58038/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /path/to/root/of/project -s --cache-clear tests
Fails with "E ModuleNotFoundError: No module named 'mymodule'"
Manual run with the following works fine
python -m pytest --rootdir /path/to/root/of/project -s --cache-clear tests
Difference describe here in the first section
https://docs.pytest.org/en/latest/usage.html#cmdline
But I'd prefer not to change sys.path manually if not necessary
PyCharm with the same project and venv works fine and without the need to modify anything.
The text was updated successfully, but these errors were encountered: