-
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
Unittest framework : Automatic test discovery not working if test has a relative import #6881
Comments
This comment has been minimized.
This comment has been minimized.
I am not able to confirm this. For me, tests are still not discovered if I rename As a workaround, I would ask you to try the
{
"python.testing.pytestEnabled": true,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": false,
"python.testing.pytestArgs": [
"<Test_directory_name>"
]
} |
@karrtikr We are working with an existing large code base that already uses |
I see. Just FYI the pytest framework also discovers tests discovered by unittest framework. (i.e the tests using unittest module) |
Having same problem with vscode not discovering tests if relative import is used :( |
By default the unittests top-level-directory is the start-directory
So I guess it's the "relative import beyond top-level package" problem, see https://stackoverflow.com/a/47030746/5955544, and should only happen with top level imports. Try
|
@C-nit I tried what you said with the example provided above and see no change. No tests discovered. |
I found some answers for a similar problem here. The problem is not the same, however, yours may have the same origin. |
I think I'm running in to this bug with VSCode 1.41.1, using remote-ssh 0.48.0 and Python 2020.1.57204 modules. I'm running it through Python 3.6.9 on CentOS 7. One of my test files is not discovered by VSCode, although it can be discovered just fine using the command line: I can get VSCode to discover the test by commenting out one of the import statements in the test file. Interestingly, the offending line is not itself a relative import. There could be a relative import hidden somewhere in that module. Hope this is fixed soon. |
Vscode 1.44.2 (user setup), Vscode Python module version 2020.4.74986, Python 3.7.3 (Win10). Testingtest/
Vscode says: "No tests discovered, please check the configuration settings for the tests."
Unittest discovery is working from terminal window:
Is this an unsupported organization of *.py files? Is the only supported way to test filename.py to have the test files in the same directory? |
In my case the problem seems to be solved by:
Weird but works... |
Hi markkuleinio, I set the configuration you said:
But still no lucky with that. Also, I've added the empyt init.py file in tests folder :( Is the configuration correct, or may I be forgetting something else... |
Do note that the correct filename is |
For the record: With the new Python extension version 2020.4.76186 (27-Apr-2020) there are no test discovery problems anymore in my projects. Thanks! |
Oh! Thank you for the update markkuleinio. I've checked that I'm using this version, but unittests are still not found :/ (also, I've created the |
There is some pretty significant refactoring done to keep things DRY and accommodate tooling: * Move existing test code from directory `test` to directory `gazoo_test` * Having tests live in a directory called `test` creates a package called `test`, but this conflicts with python's own `test` package (which exists only for internal python use, but it is still a namespace conflict). * A class `TempCwdTestCase` is created to accommodate testing with a clean, temporary working directory. * This is used in tests by absolute import (**`gazoo_`**`test.*`) instead of by relative import (`.*`) because vscode's python testing does not detect tests correctly when using relative imports (see microsoft/vscode-python#6881). I am loathe to make code changes for an editor, yet here I am...
running Python extension v2020.6.91350 and having issues with unittest discovery. I tried all of markkuleinio's suggestions and I could not get unittest working. Have to switch to |
I'm also facing this issue with Python extension v2020.7.96456 . At first it was working just well, and out of a sudden it stopped finding the unittests in files that included a relative import. |
Same here with 2020.9.114305. |
Hi, still encountering this bug in:
Im on Ubuntu 20.10. And Python version Unit test is NOT discovered when there is a relative import. If I comment the import out, it gets discovered. Unfortunate :(. |
I've ran into this issue and tried to narrow it down. What cause the bug for me is if I wrongly typehint and argument to a function, inside a file that im importing in the test. I.e. dummy.py
test_dummy.py
Will not work, meaning the test will not be discovered. If i however make the change (so i correctly typehint a Dict)
The test will be discovered |
Using 2021.1.0 Python extension I had same issue as described initially (relative imports preventing vscode from detecting tests). I was able to run the tests using Using absolute imports fixed my issue. I don't think that is ideal, though. But maybe that is the requirement? |
Tests work fine from command line, |
For those still running into this issue, here's an explanation from what I've found. In the original issue description, the start directory is set to the test directory. That means that unittest uses the inside of that directory as the starting location. By using The extension parses I am trying to see if I can fix it on my fork of the repo. I will create a PR if I am successful. |
Hello! We have just finished our testing rewrite and are beginning the roll out to users. If you are able, it would be very helpful to know if your issue still exists on the rewrite! To use the rewrite yourself just add Thanks! |
I don't see this as being fixed. The output in the python window is showing an error, and in the "testing" panel, it says "Unittest Discovery Error". Python output shows:
With that setting, if I uncomment |
If I opt into the I'm not sure which extension (pylance? python?) I should look at to determine if I have the right version installed or not. |
@Dr-Irv, it is the python extension. You should check to see if the extension is enabled (sometimes i have to just open a python file in the editor then it will enable), you can see the python interpreter selected in the bottom right of the screen. You can also try using the command palette to look for run test / discover test there. Let me know if that brings the icon back! |
Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on, then we will be happy to re-open this issue to pick up where we left off. Happy Coding! |
This is with Python Language Server version 0.3.43.0 on Windows 10, Anaconda python 3.7.3.
Have a folder with the following structure:
The files
base.py
, and both__init__.py
are empty.Here is
foo.py
:Here is
test_foo.py
:Here is
settings.json
in the.vscode
folder in that directory:If you comment out
from . import base
intest_foo.py
, then the test is discovered. Commenting and uncommenting that line makes the test discovered versus not discovered, as evidenced by seeing the buttonsRun Test|Debug Test
in the editor.If you rename
mypkg_test
totest
, update the test settings to point to the newly named directory, and leave the source with the linefrom . import base
present, then the test is discovered.So the name of the test folder seems to affect test discovery when doing a relative import inside one of the tests.
We have a big source code with this naming structure (and I don't control the naming) and
base.py
has a lot more stuff in it, and we can run tests just fine from the command line, but not from VS Code.The text was updated successfully, but these errors were encountered: