Problem/Motivation

There are varying reports of xdebug breakpoints not stopping in web requests made from BrowserTestBase tests (eg, code executed via drupalGet, etc).

I am able to reproduce this locally running the latest 8.4.x, with PhpStorm xdebug connections set to 10, and xdebug.remote_autostart=1. Normal debugging works just fine (including stepping into WebTestBase web requests).

This is purportedly due to those tests utilizing PHPUnit's "run in separate process" functionality, which causes the code to be serialized php that is executed, and thus preventing IDEs from having appropriate code mappings.

There is a PR for PHPUnit to write these out to files so IDEs can do the appropriate mappings.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

jhedstrom created an issue. See original summary.

jhedstrom’s picture

Issue summary: View changes
Mile23’s picture

Status: Active » Postponed (maintainer needs more info)

run-tests.sh and Simpletest UI will launch all PHPUnit-based tests in a separate process, using exec(). run-tests.sh does this so we can have concurrent test runs.

If you use the phpunit tool, the test won't be running in a separate process unless it's annotated with @runInSeparateProcess or @runTestsInSeparateProcesses.

So for debugging, try making sure there's no process annotation and do this: $ SIMPLETEST_BASE_URL=http://localhost:8888/ ./vendor/bin/phpunit -c core/ --testsuite functional [specify your test here]

jhedstrom’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

Yep, this appears to have been a passing thing that resolved itself. Breakpoints within web requests made from a test now properly stop.

benjy’s picture

Just tested this again and it doesn't work for me in the sub-requests from BTB, I don't think i've ever had it work with BTB but it used to with Simpletest.

class MySampleTest extends BrowserTestBase {
  public function testXdebug() {
    $this->drupalGet('<front>');
  }
}

Breakpoint lands as expected on $this->drupalGet() but never lands in index.php, running with

./app/vendor/bin/phpunit path/to/MySampleTest.php

Inside of Vagrant, with the following vars set.

XDEBUG_CONFIG=idekey=PHPSTORM
PHP_IDE_CONFIG=serverName=d8.dev
SIMPLETEST_BASE_URL=http://d8.dev
SIMPLETEST_DB=mysql://drupal:drupal@localhost/local

Using Drupal 8.4.x at 2dc6d91d60

marcoscano’s picture

A workaround I've been using is to set xdebug.remote_autostart = 1 in apache's xdebug configuration whenever I want to debug drupal's code, and use it in cli's xdebug configuration, when I want to debug the test code itself.

With this distinction, I haven't encountered any problems anymore.

benjy’s picture

Status: Closed (cannot reproduce) » Active

That seems to imply then that the environment variables/cookies are being lost in the requests the tests make if autostart works, no?

Mile23’s picture

Just tested this again and it doesn't work for me in the sub-requests from BTB,

Running from run-tests.sh or phpunit?

See #3.

benjy’s picture

I gave an example of how I was running it in #5

./app/vendor/bin/phpunit path/to/MySampleTest.php

jibran’s picture

@benjy does it work if you hack sub-request to add xdbug info in PhpStorm?

benjy’s picture

I tried this, but still no luck. I can see the cookie sent in the request but xdebug never connects.

  public function testDebug() {
    $this->drupalGet('<front>', [
      'query' => [
        'XDEBUG_SESSION_START' => 'PHPSTORM',
      ]
    ]);
  }

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jibran’s picture

I faced the same issue as @benjy said. For all test suites where $runTestInSeparateProcess = TRUE; it ends up in \PHPUnit_Util_PHP_Default::runJob where even if you hack the param passed to the $runtime which is a path to binary the xdebug session will not start. At this point, I copied the $job param value pasted it into a file and save it in docroot and executeed it with PHP CLI and viola it hit the breakpoint in the tests and if you have allowed multiple connections for debugger you can see all the requests.

I think we need to fix phpunit test runner for runTestInSeparateProcess tests.

Mile23’s picture

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

pfrenssen’s picture

It looks like this problem is already solved in the latest version of PHPUnit: https://github.com/sebastianbergmann/phpunit/pull/2548

We are currently still on PHPUnit 4.x though which doesn't have this fix.

jibran’s picture

It looks like this problem is already solved in the latest version of PHPUnit: https://github.com/sebastianbergmann/phpunit/pull/2548

Sorry that PR is closed unmerged but in which PR this change is merged?

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Bart Vanhoutte’s picture

Is this problem the same or related to breakpoints in KernelTestBase not working?

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

penance316’s picture

I see no comments for a few years but it seems I am having this same issue.

drupal 8.9
PHP 7.4
phpunit 7.5.20

Is this still an issue for others?
My xdebug breakpoints are working fine for UNIT tests just not the tests extending BrowserTestBase.

Not sure if its helpful but when running UNIT tests I can use the xdebug function xdebug_break() fine but when trying that in the BrowserTestBase functional tests I get an error like "xdebug_break() is an undefined function" seeming to suggest that xdebug is not loaded correctly in that environment.

thanks all

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

gdeguzman’s picture

#2850891-23: xdebug breakpoints not working in web requests from BrowserTestBase @penance316 I'm also having this issue on Drupal 8.9, PHP 7.3.25, PHPUnit 7.5.20, and xdebug 3. I'm attempting to run the debug through PHPStorm 2020.3. Breakpoints hit prior to my test (such as if I set them in BrowserTestBase), but as soon as it gets to the test actually running, the breakpoints in my actual test aren't hit. However, PHPUnit does go on to say that my test passes.

Like you, my Unit tests are working fine. I do believe it has to do with separate processes, but PHPStorm is set to 19 max simultaneous connections. It seems that something is being lost here in terms of xdebug being able to connect. Setting xdebug 3 options such as discover_client_host=true or start_with_request=yes has had no effect either.

I'm unsure how widespread this issue is, as I seem to run into the same posts from a few years ago regarding this. I will post any updates I find here.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Tagging this for tomorrow's sprint

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

super_romeo’s picture

Any news?
There is no way to debug tests.