How to debug features in Behat 3 with echo
Asked Answered
C

6

12

I am trying to debug a feature in Behat 3 to see what is going on.

Echo statements don't seem to work - I get no output.

The step I'm trying to use currently looks like this:

/**
 * @Then /^echo last request$/
 */

public function echoLastRequest() 
{
    echo ($this->_history->getLastRequest());
    echo 'test';
}
Chevy answered 20/8, 2014 at 9:49 Comment(2)
I ended up just writing to a log file, using Laravel's Log::info()Chevy
That would be the simplest if you don't need proper debugging.Burghley
P
16

You could use a simple print_r() or var_export():

var_export('DEBUG!!!');

Debug preview

Poacher answered 17/3, 2016 at 14:53 Comment(0)
B
6

AFAIK you can't. You can use regular debugging. In the latest PhpStorm EAP Behat comes with debugging support, though its still glitchy. You can initiate Xdebug yourself by passing the Xdebug cookie before Mink opens any pages. This should be added to the context.

/**
 * @beforeStep
 *
 * @param BeforeStepScope $scope
 */
public function synchroniseClientSession(BeforeStepScope $scope)
{
    $driver         = $this->getSession()->getDriver();

    // Cookies must be set on the domain we're testing, Chrome opens with 'data:,' in the url.

    if ($driver instanceof Selenium2Driver && $driver->getCurrentUrl() === 'data:,') {
        $driver->visit($this->getMinkParameter('base_url'));
    }

    $driver->setCookie('XDEBUG_SESSION', 'PHPSTORM');
}

Default Behat configuration must also receive the environment variable with the idekey in case you're testing the raw code:

XDEBUG_CONFIG="idekey=PHPSTORM"

enter image description here

Burghley answered 20/8, 2014 at 10:42 Comment(0)
S
4

The problem is that Behat uses output buffering. You can work around it by flushing the output buffers manually before Behat can intercept them:

var_dump('test');
ob_flush();
Stirps answered 9/8, 2017 at 15:39 Comment(0)
A
1

If you want to debug the actual Behat code this method always worked for me.

debugging Behat contexts

When PHP Storm / IntelliJ Idea doesn't like my Behat version I have to use something like this:

  • Create Run profile
  • Point to behat file
  • Set required Behat parameters (configuration file, feature folder, tags)
  • Specify custom directory (relatively to which all these paths are)

Note: I specify @wip tag to run tests that are tagged as wip (I'm currently working on).

configuration profile

Akerboom answered 18/3, 2016 at 10:14 Comment(0)
W
0

For some unknown reason I hope to workout soon, I can't debug behat with my current setting (vim + vdebug + xdebug).

In the meantime this little trick helped me :

passthru("echo \"foo\"");
Wife answered 30/1, 2015 at 0:3 Comment(2)
I know my suggestion is not best practice, that is why I stated it was a "trick". Downvoter, please tell me what's wrong with this.Wife
As of today, with behat 3.2.2, this little trick does not work anymore. I suggest the use of a proper logger instead. See github.com/Seldaek/monolog for example.Wife
F
0

This won't work, as Behat collect output buffor and clear it with own report.

Then you echo was send, but then rewrited in buffor by behat.

If you really, want to see that this was, you need simply:

  • use function die('MSG');
  • log to file

Best way, of course is to use xDebug - others answer show better description of that.

Firecure answered 12/5, 2017 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.