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

Matheus Saraiva
  • 1,144
  • 1
  • 11
  • 33
  • What are you speaking about? What is your IDE ? – Laurent LAPORTE Jul 05 '18 at 21:30
  • 2
    I'm trying to make the self-running [feature](https://code.visualstudio.com/docs/python/unit-testing) of Visual Studio Code unit tests work. I'm using **Visual Studio Code** – Matheus Saraiva Jul 05 '18 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). – Jann Poppinga Jun 02 '21 at 06: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 '21 at 22:15
  • `python -m unittest discover -v -s tests/ -p '*_test.py'` thank you for providing this command – Felipe Alvarez Feb 01 '22 at 04:06

4 Answers4

10

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.

bondar
  • 484
  • 1
  • 3
  • 16
Matheus Saraiva
  • 1,144
  • 1
  • 11
  • 33
  • 1
    You can accept your own answer so that the question has an answer. – thoni56 Jan 15 '19 at 08:43
  • 7
    Visual Studio Code can not discover tests when Python syntax is broken – Andrey Semakin Jan 28 '19 at 10:46
  • Click the Test Status indicator (bottom left of VS Code) to show test syntax errors etc – TFD Aug 04 '19 at 02:32
  • @Andrey Semakin You save my day – Yiling Liu Aug 10 '20 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 07 '20 at 17:06
  • I had the same issue and as indicated by @AndreySemakin, the issue was due to underlying issues in the python code that was being tested. Once this was resolved, the tests were discovered fine. Also, relative imports worked for me without issue. – Nikhil Gupta Feb 24 '21 at 10:40
9

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.

Preston
  • 7,399
  • 8
  • 54
  • 84
6

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

float32
  • 61
  • 1
  • 3
  • 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 '22 at 09:21
  • This worked for me. Added __init__.py and tests were discovered. – Lizozom Jun 01 '23 at 16:22
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.

Jerry H
  • 105
  • 2
  • 7