Skip to content

VSCode "Debug Test" from Test Explorer debugs all tests in test class rather than just the selected test(s) #7120

@majellin24

Description

@majellin24

Summary

When I right click a test and select "Debug Test" the extension attempts to run and debug all tests in the test class rather than just the selected test. For large test classes, this can take a very long time and makes writing and debugging tests overly time consuming.

Steps To Reproduce:

  1. Create a test class
  2. Add several test methods
  3. Open test explorer, right click on just one test method name and select "Debug Test"

Expected result

The selected test, and only that test, should run and start debugging.

Actual result

All tests in the test class run and eventually start debugging

Additional information

When "Debug Test" is selected, ApexTestController.runTests executes with isDebug set to true and calls ApexTestController.debugTests with testsToRun containing a single item with id similar to method:BugTest.myUnitTest2. In this case, the test class is called BugTest and the method is called myUnitTest2.

On line 603 the controller iterates through the array and extracts the test name and class name. If it finds a class name, it groups the method by class in a map methodsToDebug. If multiple items are in the array, this map builds up with classes and tests. However, the map is never used. On line 635 the controller simply iterates all the class names it found and executes all tests in that class.

My thought would be that the controller should use the methodsToDebug map it built to execute each method it found for each class, rather than just throwing away this map without ever using it.

Something like:

for (const className of classesToDebug) {
      try {
        const methods = methodsToDebug.get(className);

        if(methods && methods.length > 0){
            for(const method of methods){
                await vscode.commands.executeCommand('sf.test.view.debugSingleTest', { name: method });
            }
        }
        else{
            await vscode.commands.executeCommand('sf.test.view.debugTests', { name: className });
        }        
      } catch (error) {
        const friendlyMessage = toUserFriendlyApexTestError(error);
        for (const test of testsToDebug) {
          if (isClass(test.id) && getTestName(test) === className) {
            run.errored(test, new vscode.TestMessage(nls.localize('apex_test_debug_failed_message', friendlyMessage)));
          } else if (isMethod(test.id) && extractClassName(test.id) === className) {
            run.errored(test, new vscode.TestMessage(nls.localize('apex_test_debug_failed_message', friendlyMessage)));
          }
        }
      }
    }

Salesforce Extension Version in VS Code: 66.3.2

Salesforce CLI Version: @salesforce/cli/2.128.5 win32-x64 node-v22.22.1

OS and version: Microsoft Windows 11 Education 10.0.26100 Build 26100

VS Code version: 1.114.0

Most recent version of the extensions where this was working: I have not worked on this for several months so I cannot answer this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions