简体   繁体   中英

Debugging Codeception tests with Xdebug

I wrote some API tests with Codeception's ApiGuy. Now I want to set breakpoints in my PhpStorm 7 for tests debugging, but have no idea how to start debug session after $ vendor/bin/codecept run . I know about --debug option, but it's not exactly what I want.

Do you have any idea? Thanks in advance!

I ran into the same problem. Seems that Codeception only comes with a command line tool, which cannot be debugged. I finally ended up writing my own PHP runner, which is basically a lite copy of the codeception command executable.

Actually all you would have to do in Linux is to remove the shebang from the codeception tool to run it as a PHP script. But since commands other then codeception run are much less likely to be a subject of debugging, I've prepared a separate PHP script. It contains only the run option:

<?php
/**
 * Codeception PHP script runner
 */

require_once dirname(__FILE__).'/../vendor/codeception/codeception/autoload.php';

use Symfony\Component\Console\Application;

$app = new Application('Codeception', Codeception\Codecept::VERSION);
$app->add(new Codeception\Command\Run('run'));

$app->run();

After you get this done you can set up your debugging script like any other in PHPStorm . So go to the Select Run/Debug Configuration > Edit Configurations... :

编辑配置...

Now Add New Configuration (Alt + Insert) > PHP Script . Then name the run configuration and select the file you created above. Remember to add the run argument:

在此处输入图片说明

And that's it. Now you can run your tests from within the IDE and debug them as ordinary scripts.

I use the codecept.phar file and I find this one does work. just type this command in the console and then run the codeception tests:

export XDEBUG_CONFIG="idekey=session_name remote_host=localhost profiler_enable=1"

我建议使用

php -dxdebug.remote_enable=1 -dxdebug.remote_autostart=On -dxdebug.idekey=YOUR_KEY -dxdebug.remote_host=YOUR_IP ../vendor/bin/codecept run

Alias could be used

alias xon="export XDEBUG_CONFIG=\"profiler_enable=1\""
alias xoff="export XDEBUG_CONFIG=\"profiler_enable=0\""

And then we could use xon in console to start debug and xoff to finish.

http://theaveragedev.com/debug-cli-scripts-with-phpstorm

另一种方法是配置 PhpStorm,如上所述,但为File设置bin/cept ,为Arguments run ,并将项目的根设置为Custom Working Directory

I don't think you actually need an extra script to debug Codeception at least in PHPStorm. I don't think you would in other IDEs either. You can set codecept.phar as the "File" in your configuration shown in Maciej Sz's answer and run it as you would any other command line script from PHPStorm, Eclipse, etc.

In PHPStorm you create a new PHP Script debug configuration pointing at codecept.phar as the "File". Set the arguments to be a basic run configuration (eg run acceptance --group mygroup --env myenv), but tell it to "Show this page" every time you want to debug. You can then change which test/group/environment you are running each time to run your test with breakpoints.

When you click the debug button from the pop-up the acceptance test starts in the debugger console.

The initial run that hits a break-point will be in the validation phase where Codeception attempts to ensure the code will run before running it. The second run will be when the test is actually using Selenium or whatever you have configured. This will allow you to see what is in memory, step over things in a controlled manner, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM