16

I'm trying to make the self-running feature of Visual Studio Code unit tests work. I recently made a change in the directory structure of my Python project that was previously like this:

myproje\
    domain\
        __init__.py
    repositories\
    tests\
        __init__.py
        guardstest.py
    utils\
        __init__.py
        guards.py
    web\

And my setup for unittest was like this:

    "python.unitTest.unittestArgs": [
    "-v",
    "-s",
    "tests",
    "-p",
    "*test*.py"
]

After the changes, the structure of the project was as follows:

myprojet\
    app\
        controllers\
            __init__.py
        models\
            __init__.py
            entities.py
            enums.py
        tests\
            res\
                image1.png
                image2.png
            __init__.py
            guardstest.py
        utils\
            __init__.py
            guards.py
        views\
            static\
            templnates\
        __init__.py         
    uml\

After that the extension does not discover my tests anymore. I've tried to change the '-s' parameter to "./app/tests", ".tests", "./tests", "app/tests", "/app/tests", "app.tests", unsuccessfully .

enter image description here

5
  • What are you speaking about? What is your IDE ? Jul 5, 2018 at 21:30
  • 2
    I'm trying to make the self-running feature of Visual Studio Code unit tests work. I'm using Visual Studio Code Jul 5, 2018 at 23:43
  • 3
    Visual Studio Code is a bit tight-lipped regarding error output from test discovery. To get an actual error message, run it manually on the CLI python -m unittest discover -v -s tests/ -p '*_test.py' (adapt for your directory and naming pattern). Jun 2, 2021 at 6:51
  • 1
    Funny enough, when I run "python -m unittest discover -v -s tests/ -p '*_test.py'" all my tests run without error, but they are still not discovered by VScode...
    – bricx
    Jul 22, 2021 at 22:15
  • python -m unittest discover -v -s tests/ -p '*_test.py' thank you for providing this command Feb 1, 2022 at 4:06

4 Answers 4

9

The problem was that I was using relative imports in the test module (from ..utils import guards). I just changed it to absolute import (from app.utils import guards) and it all worked again.

6
  • 1
    You can accept your own answer so that the question has an answer.
    – thoni56
    Jan 15, 2019 at 8:43
  • 7
    Visual Studio Code can not discover tests when Python syntax is broken Jan 28, 2019 at 10:46
  • Click the Test Status indicator (bottom left of VS Code) to show test syntax errors etc
    – TFD
    Aug 4, 2019 at 2:32
  • @Andrey Semakin You save my day
    – Yiling Liu
    Aug 10, 2020 at 18:54
  • I had a similar issue where I had a BaseTest class with various helper methods located in tests/__init__.py. If in test_a.py and test_b.py, I inherited BaseTest (e.g., class TestFoo(BaseTest)) and used a relative import from . import BaseTest, no tests would be discovered. The solution was to use an absolute import: from tests import BaseTest. VS Code then discovered the tests. Thanks, Matheus!
    – ramhiser
    Oct 7, 2020 at 17:06
6

There are 2 reasons that this might not work:

There is an error in the tests

The python Testing plugin won't find your tests if there is an error in the test script.

To check for a potential error, click the Show Test Output, then run the tests using Run All Tests (both buttons are located in the top left, just above where the tests should appear).

If there is an error, it will appear in the OUTPUT tab.

The tests aren't properly configured

Check your .vscode/settings.json, and take the python.testing.unittestArgs list.

You can debug the discovery of tests in the command line by adding args to the python3 -m unittest discover command in the command line.

So with this config:

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "*test*.py"
    ]
}

You would launch the command:

python3 -m unittest discover -v -s . -p "*test*.py"

You can play with the args unitl you discover your tests, and modify the args in .vscode/settings.json accordingly .

Here are the docs for unittest

Note

A common reason for this is that you're trying to run tests with dependencies. If this is the case, you can select your interpreter by running ctrl + shift + p and searching Python: Select Interpreter, then selecting the correct interpreter.

4

It is because some of the imports in the test is not discoverable. when running python -m unittest -h, the last line of the output is

For test discovery all test modules must be importable from the top level directory of the project.

it is likely that VSCode is running the command without the right PYTHONPATH and other environment variables.

I created __init__.py and put the following code into it.

import sys
import os
import unittest

# set module path for testing
sys.path.insert(0, "path_in_PYTHONPATH")
# repead to include all paths

class TestBase(unittest.TestCase):
    def __init__(self, methodName: str) -> None:
        super().__init__(methodName=methodName)

then in the test file, instead of extending unittest.TestCase, do

from test import TestBase 

class Test_A(TestBase):
    ...

1
  • Thanks! I have configured my repo as a python package and then I had to have __init__.py in every subfolder. Somehow i forgot it in my test folder, so I just imported every test class in the test folder's __init__.py and it works!
    – fratajcz
    Mar 23, 2022 at 9:21
0

In my case, I had same identical issue on Visual Studio Code, but my resolution was slightly different.
I looked at .vscode/settings.json file. I noticed unittestEnabled and pytestEnabled, but unittestEnabled=true and pytestEnabled=false. Didn't know what the difference was, but I used pytest in my CLI test. So, I turned pytestEnabled = true. Then I went to Testing icon on the left. I clicked a button to discover test again and had to choose couple of options like setting test folder. Now it discovered all my tests and working as expected. Hope this helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.