vscode-python: Tests are not being discovered correctly - pytest

Type: Bug

Behaviour

Expected vs. Actual

Extension should discover all unit tests but it does not. In terminal it finds a lot more test cases that in the extension.

Steps to reproduce:

I think I found the pattern for this issue. When you have several classes inside a unit test file, the extension only discovers the last class tests.

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.12
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Default
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

No error shown

User Settings


languageServer: "Pylance"

testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true

Extension version: 2023.22.0 VS Code version: Code 1.85.0 (af28b32d7e553898b2a91af498b1fb666fdebe0c, 2023-12-06T20:48:09.019Z) OS version: Windows_NT x64 10.0.22621 Modes: Remote OS version: Linux x64 5.15.133.1-microsoft-standard-WSL2

System Info
Item Value
CPUs 11th Gen Intel® Core™ i7-11390H @ 3.40GHz (8 x 2918)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 31.74GB (7.88GB free)
Process Argv –crash-reporter-id 93887114-f95c-4ef0-96d1-9a3c94563709
Screen Reader no
VM 0%
Item Value
Remote WSL: Ubuntu
OS Linux x64 5.15.133.1-microsoft-standard-WSL2
CPUs 11th Gen Intel® Core™ i7-11390H @ 3.40GHz (8 x 2918)
Memory (System) 15.50GB (11.61GB free)
VM 0%
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242:30382549
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
vscod805cf:30301675
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593:30376534
pythonvs932:30410667
py29gd2263:30899288
vsclangdf:30486550
c4g48928:30535728
dsvsc012:30540252
azure-dev_surveyonecf:30548226
2e4cg342:30602488
89544117:30613380
showlangstatbar:30737416
962ge761:30917236
fixshowwlkth:30771522
showindicator:30805244
pythongtdpath:30769146
i26e3531:30792625
welcomedialog:30910333
pythonnosmt12:30797651
pythonidxpt:30866567
pythonnoceb:30805159
asynctok:30898717
dsvsc013:30795093
dsvsc014:30804076
dsvsc015:30845448
pythontestfixt:30902429
pyreplss1:30897532
pythonmypyd1:30879173
pythoncet0:30885854
pythontbext0:30879054
dsvsc016:30899300
dsvsc017:30899301
dsvsc018:30899302
aa_t_chat:30882232

About this issue

  • Original URL
  • State: open
  • Created 7 months ago
  • Reactions: 9
  • Comments: 22 (9 by maintainers)

Most upvoted comments

I’m also experiencing this same issue.

I’m facing the same issue. I’m using pytest and tests from only the last class are shown in sidebar. However, in the Output tab all of them are listed correctly.

Hi @Tinuxx, I am able to repro this issue. I will work on putting in a fix. Thanks!

My company updated pytest to 7.4.4 version and it’s working correctly now

This problem has been plaguing me for months. My temporary solution has been to edit the VSCode state file to disable the “pythonTestAdapter” experimental feature.

Now that I’ve looked through this page and the one for issue #22457, it seems the easiest solution is to just update my pytest plugin from 6.2.x to the latest 8.x.x version - it immediately fixed the issue.

No worries, thank you for the tech support. Testing a little bit, I found out that the issue arises when using classes with pytest. It works fine with Unittest, it does not when “converting” to pytest. When doing so, the extension only finds tests from the last class. The bug is consistent with other colleagues as well.

# First class to be tested
class Calculator:
    def add(self, a, b):
        return a + b

    def subtract(self, a, b):
        return a - b

# Second class to be tested
class StringManipulator:
    def capitalize(self, string):
        return string.capitalize()

    def reverse(self, string):
        return string[::-1]

# Test functions for Calculator
class TestCalculator:
    def test_add(self):
        calc = Calculator()
        assert calc.add(2, 3) == 5

    def test_subtract(self):
        calc = Calculator()
        assert calc.subtract(5, 3) == 2

# Test functions for StringManipulator
class TestStringManipulator:
    def test_capitalize(self):
        manipulator = StringManipulator()
        assert manipulator.capitalize("hello") == "Hello"

    def test_reverse():
        manipulator = StringManipulator()
        assert manipulator.reverse("hello") == "olleh"

In this image you can see that only the last two tests are shown: image