Skip to content

Improve first-time testing (& coverage) experienceΒ #24766

@joaomoreno

Description

@joaomoreno

I asked an LLM to give me an example Python unit test suite. I got this:

# calculator.py

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y == 0:
        raise ValueError("Cannot divide by zero")
    return x / y
# test_calculator.py

import unittest
from calculator import add, subtract, multiply, divide

class TestCalculator(unittest.TestCase):

    def test_add(self):
        self.assertEqual(add(1, 2), 3)
        self.assertEqual(add(-1, 1), 0)
        self.assertEqual(add(-1, -1), -2)

    def test_subtract(self):
        self.assertEqual(subtract(10, 5), 5)
        self.assertEqual(subtract(-1, 1), -2)
        self.assertEqual(subtract(-1, -1), 0)

    def test_multiply(self):
        self.assertEqual(multiply(4, 5), 20)
        self.assertEqual(multiply(-1, 1), -1)
        self.assertEqual(multiply(-1, -1), 1)

    def test_divide(self):
        self.assertEqual(divide(8, 4), 2)
        self.assertEqual(divide(-1, 1), -1)
        self.assertEqual(divide(-1, -1), 1)
        with self.assertRaises(ValueError):
            divide(1, 0)

if __name__ == '__main__':
    unittest.main()

I created these two files. I installed the Python extension. The Testing view appeared. But now the troubles start:


πŸ› The editor is unable to detect that I use unittest, so it asks me to configure stuff. The configuration is weird since it asks me for the root of the tests and for the right test glob to use (why aren't we just picking good defaults here, instead of forcing the user to make a decision?):

Image Image

πŸ› After configuring the tests I am immediately greeted with an error. The error is also weirdly shown as a list row (cc @connor4312).

Image

πŸ› After reloading I can now run the tests. I then run Run Tests with Coverage and immediately the next error comes up. Should the extension automatically take care of installing this for me?

Image

Metadata

Metadata

Assignees

Labels

area-testingbugIssue identified by VS Code Team member as probable bugneeds spikeLabel for issues that need investigation before they can be worked on.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions