Unittest framework : Automatic test discovery not working if test has a relative import
See original GitHub issueThis 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:
mypkg
--> foo.py
--> __init__.py
mypkg_test
--> __init__.py
--> base.py
--> test_foo.py
The files base.py
, and both __init__.py
are empty.
Here is foo.py
:
def goo():
return 5
Here is test_foo.py
:
import unittest
from . import base
from mypkg.foo import goo
class FooTest2(unittest.TestCase):
def test_foo(self):
x = goo()
self.assertTrue(5 == x)
if __name__ == '__main__':
unittest.main()
Here is settings.json
in the .vscode
folder in that directory:
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./mypkg_test",
"-p",
"test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
If you comment out from . import base
in test_foo.py
, then the test is discovered. Commenting and uncommenting that line makes the test discovered versus not discovered, as evidenced by seeing the buttons Run Test|Debug Test
in the editor.
If you rename mypkg_test
to test
, update the test settings to point to the newly named directory, and leave the source with the line from . 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:23 (1 by maintainers)
Top GitHub Comments
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:
python -m unittest discover -s ./test -p *test.py
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.
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
"python.testing.pytestEnabled": true
insetting.json
to discover my test files.