1

I am trying to add unit tests to my python project and I can't get VS Code to discover my test. The problem is when I try to import the class that I am testing.

  • If I try to run the test file, it passes.
  • If I omit the from A.myfile import MyFile the test is discovered.

Question: What am I doing wrong here?


File structure:

root
├── A
│   ├── __init__.py
│   └── myfile.py
└── tests
    ├── __init__.py
    └── A
        ├── __init__.py
        └── test_myfile.py

myfile.py:

class MyFile(object):
    def __init__(self):
        self.value = 1

test_myfile.py:

import unittest
from A.myfile import MyFile

class Test_MyFile(unittest.TestCase):
    def test_my_file(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()

.env:

PYTHONPATH=PATH_TO_ROOT

settings.json:

{
    "python.pythonPath": "PATH_TO_PYTHON\python.exe",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py",
        "-t",
        "."
    ],
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}

Python test log:

start

VSCode version: 1.38.1
Python version: 2.7.14 (x64)

Chau
  • 5,540
  • 9
  • 65
  • 95

1 Answers1

0

It's probably because of the value set for -t. If unittest is interpreting that - to use the default setting then it's using -s which is the tests directory, and so your import A line is importing tests/A/__init__.py instead of A/__init__.py which is leading to an ImportError when A.myfile can't be found (check the test output to see the failure).

Change that - to . and it should hopefully fix things to anchor the top directory for your project.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • You're definitely right on the *dash*, but sadly it doesn't fix the issue. I have fixed the mistake in the question. I have been thinking about this during the evening, and is my problem, that python doesn't look into the `root/A/` folder when it has already found the `root/test/A` folder which is much closer to the test file? – Chau Sep 24 '19 at 05:29
  • @Chau I just noticed you're using Python 2 and not Python 3. Add `from __future__ import absolute_imports` and see if that fixes things. – Brett Cannon Sep 24 '19 at 20:59
  • I have tried that and even with a python 3 environment and still the test is not discovered :| – Chau Sep 25 '19 at 05:53
  • @Chau have you tried this in a terminal and had it working? Basically the extension isn't doing anything magical, so you can take those arguments from your config and play with them in the terminal until it works for you ([docs on unittest discovery](https://docs.python.org/3/library/unittest.html#test-discovery)). Otherwise I would just rename your `tests/A` directory to `tests/test_A` to avoid the package name collision or try using pytest to do the discovery (it will still let you use unittest for writing your tests). – Brett Cannon Sep 25 '19 at 17:06
  • It's completely ridiculous that in 2022, IDEs/extensions are still having trouble with finding unit tests. – Mike Sep 14 '22 at 16:28
  • The situation I encountered is that the directory containing the _test.py test file must contain an __init__.py file, even if it is an empty file, in order for the python vscode plugin to scan the test file in this directory – rxliuli Aug 28 '23 at 02:33