recca0120 / vscode-phpunit

The VS Code Test Explorer extension for PHPUnit
MIT License
127 stars 44 forks source link

Debug test with xdebug #172

Open pgrzesiecki opened 11 months ago

pgrzesiecki commented 11 months ago

Can the test be executed with debug mode using xdebug extension to breakpoint test code?

I guess in most cases when we have configured XDEBUG it is required only to add XDEBUG_SESSION=1 at the beginning of the php execution command. I've tried config options like phpunit.php, phpunit.phpunit, phpunit.args but without real success.

I tested different extensions to execute php test in Tests Explorer and there were options to debug (even with a separate button to execute it in debug mode), but to be honest, your solution works much faster :)

ljubadr commented 11 months ago

On mac I'm able to debug my phpunit tests running in docker container with the setup below

My xdebug.ini

[xdebug]
xdebug.mode=debug,profile,trace,develop

xdebug.client_port=9003
xdebug.client_host=host.docker.internal
xdebug.start_with_request=trigger
xdebug.trigger_value=vscode
xdebug.log_level=0

In my settings.json I have

{
    "phpunit.command": "docker exec -t app /bin/sh -c",
    "phpunit.php": "XDEBUG_MODE=debug XDEBUG_TRIGGER=vscode XDEBUG_SESSION=vscode php",
    "phpunit.phpunit": "/var/www/html/vendor/phpunit/phpunit/phpunit",
    "phpunit.args": [
        "-c",
        "phpunit.xml"
    ],
    "phpunit.paths": {
        "${workspaceFolder}": "/var/www/html"
    },
}

app is container_name from docker-compose.yml

services:
  app:
    container_name: app

Note that because I use xdebug.trigger=vscode I have to set XDEBUG_* variables for "phpunit.php": "...".

I use xdebug/vscode-php-debug extension with following launch.json

{
    "name": "xdebug",
    "type": "php",
    "request": "launch",
    "pathMappings": {
        "/var/www/html": "${workspaceFolder}"
    },
    "skipFiles": [
        "**/vendor/composer/ClassLoader.php",
        "**/vendor/laravel/framework/src/Illuminate/Foundation/AliasLoader.php",
    ],
    "xdebugSettings": {
        "max_depth": 5,
        "max_children": 100,
        "max_data": 10000
    }
},

To start debugging test I first start the debugger with F5 and then I press play to execute test.

If debugging is still not working then go to Output panel in vscode and find phpunit in the dropdown. There you can see which command is being executed when you press play. You can copy the command from Output and run that manually in your terminal. You will have to wrap anything after /bin/sh -c ... in double quotes /bin/sh -c "...". Tweak the command until you get the breakpoint working and then copy the working command back into the settings above

I hope this helps