pythonvisual-studio-code

Python Unittest: No tests discovered in Visual Studio Code


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


Solution

  • 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.