Search code examples
pythonvisual-studio-codepython-unittest

VScode does not count tests that throw exceptions (0/0 tests passed)


My dev environment is:

vscode 1.80.1
python 3.10
using the python extension for vscode v2023.12.0

My test file:

import unittest

class MyTestCase(unittest.TestCase):

    def test_say_hello(self) -> None:
        raise Exception("test")

My vscode settings:

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./runner",
        "-p",
        "test_*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true,
}

I would expect that when I run the test to show an error because of the raised exception, but it does not. Instead, the "testing" tab in vscode shows (0/0 tests passed):

enter image description here

Worst, if the tests was previously passing it would still show up with a green mark! The "Tests Results" tab simply shows: "Finished running tests!" with no other outputs. If I run the test in debug mode, I correctly get the output error, but the tests is still not marked as incorrect:

enter image description here

Running unittests manually outside vscode correctly display the error (just like the output in debug mode).

So how do I get vscode to display my failing tests?


Solution

  • I found out that it is a known issue and there is already a fix pushed to the main branch, but it is not present in the live version yet:

    https://github.com/microsoft/vscode-python/pull/21630