Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 573
Matheus Saraiva Avatar asked Jul 05 '18 19:07

Matheus Saraiva


People also ask

Why is VS Code not detecting Python?

If VS Code doesn't automatically locate the interpreter you're looking for, refer to Environments - Manually specify an interpreter. You can configure the Python extension through settings. Learn more in the Python Settings reference.

How do I run a unittest code in Visual Studio?

Tests can be run from Test Explorer by right-clicking in the code editor on a test and selecting Run test or by using the default Test Explorer shortcuts in Visual Studio.

How do I configure Python tests in Visual Studio code?

To customize settings for debugging tests, you can specify "purpose": ["debug-test"] in the launch. json file in the . vscode folder from your workspace. This configuration will be used when you run Test: Debug All Tests, Test: Debug Tests in Current File and Test: Debug Test at Cursor commands.


2 Answers

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.

like image 60
Matheus Saraiva Avatar answered Sep 17 '22 12:09

Matheus Saraiva


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):
    ...

like image 28
float32 Avatar answered Sep 20 '22 12:09

float32