question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:23 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
jordanperrcommented, Jan 7, 2020

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.

3reactions
kmcmeartycommented, Jul 9, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Unittest: No tests discovered in Visual Studio Code
The problem was that I was using relative imports in the test module ( from ..utils import guards ). I just changed it...
Read more >
unittest — Unit testing framework
Test discovery loads tests by importing them. Once test discovery has found all the test files from the start directory you specify it...
Read more >
Unit test issues with relative imports : r/learnpython
Hello! I'm trying to do some unit testing, I'm having some issues with relative imports I think. Here I have the test, and...
Read more >
Python Unit Testing - LSST DM Developer Guide
See Software Unit Test Policy for an overview of LSST Stack testing. ... If scons has not been configured to use pytest in...
Read more >
Traps for the Unwary in Python's Import System
While Python 3.3+ is able to import the submodule without any problems: ... explicit relative imports to keep module tests independent of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found