vscode-python: Unittest framework : Automatic test discovery not working if test has a relative import

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:

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.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 12
  • Comments: 29 (4 by maintainers)

Commits related to this issue

Most upvoted comments

In my case the problem seems to be solved by:

  1. Creating an empty __init__.py in tests directory
  2. Configuring vscode-python to use the “. Root directory”, not the tests directory

Weird but works…

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 in setting.json to discover my test files.

@karrtikr We are working with an existing large code base that already uses unittest, so switching to pytest really isn’t an option. Hopefully, this bug will get fixed

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 from . import ..., the Python file is trying to access the test folder itself, but since unittest started inside the folder, Python doesn’t know about the folder itself. To resolve this, unittest provides a top level directory option that redirects Python to a different directory when starting relative import resolution. For the original issue, that would be -t ., the project’s root directory.

The extension parses unittestArgs and extracts the start directory and the test pattern. Every other argument is completely ignored (when running unit test discovery). The typescript code that handles discovery would need to add the top level directory argument when parsing the args. Additionally, <ms-python.python>/pythonFiles/testing_tools/unittest_discovery.py would need to be able to accept that top level directory argument, since that file is responsible for invoking unittest’s loader discovery.

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.

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 __init__.py files)

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!

Having same problem with vscode not discovering tests if relative import is used 😦

LS does not have any test discovery functionality. This looks like extension issue.