Occasionally when writing complicated steps in Behat it’s useful to debug them (perhaps to find out why your tests are failing!).
The Behat test runner is actually just a PHP script run from the command line. As such by declaring a special variable before running the test runner we can trigger a remote debugging session on your favorite IDE.
From the XDebug manual:
When running the script from the command line you need to set an environment variable, like:
You can also configure the xdebug.remote_host, xdebug.remote_port, xdebug.remote_mode and xdebug.remote_handler in this same environment variable. All those configurable settings can also be set with normal php.ini settings.
To trigger a session from the test runner we should use a command similar to the following:
XDEBUG_CONFIG="idekey=session_name" behat path/to/tests
For this to work a debugging session must be started in your IDE (eg – In Netbeans: Project Menu > debug).
Typing this over and over again can get irritating so the next logical step is to set up an alias.
Edit your .bash_profile or .bashrc file using your terminal like so:
> nano ~/.bash_profile // Then add the following line: alias behatxc='XDEBUG_CONFIG="idekey=session_name" behat'
Now when you want to run the steps and trigger a debugging session use the command: ‘bashxc’. When you want to run the test runner normally use the regular Behat command.