Question

Very thick question, but is there any way to print your own debug messages to the console in Codeception? I mean messages that have nothing to do with assertions, purely for debugging the tests themselves (e.g. like you would var_dump() a variable in any regular PHP website)

I have already tried var_dump(), echo and print but to no avail. Using WebDebug's makeAResponseDump() doesn't produce the required results neither, I just want to be able to see my variable's content without having to run a debugger like xdebug.

Was it helpful?

Solution 3

I seem to have found a way around the issue by using a helper class:

class WebHelper extends \Codeception\Module
{
    public function seeMyVar($var){
        $this->debug($var);
    }
}

and calling the class as such:

$foo = array('one','two');
$I->seeMyVar($foo);

then I get the debug output I'm looking for

I see my var "lambda function"
  Array
  (
      [0] => one
      [1] => two
  )

I will accept this as a temporary solution however I would like to keep my assertions clean and not clutter them with var_dumps upgraded to test functions, so if anyone has a conceptually correct solution, please submit

OTHER TIPS

See Debugging which says

You may print any information inside a test using the codecept_debug function.

And I'm using it in my *Cept class:

codecept_debug($myVar);

Your debug output is only visible when you run with --debug (-v doesn't show it, but -vv and -vvv do):

codecept run --debug

And the output looked like:

Validate MyEntity table insert (MyCept) 
Scenario:
* I persist entity "AppBundle\Entity\MyEntity"

  AppBundle\Entity\MyEntity Object
  (
      [Id:AppBundle\Entity\MyEntity:private] => 1
      [Description:AppBundle\Entity\MyEntity:private] => Description
  )

 PASSED 
\Codeception\Util\Debug::debug($this->em);die();

and run Codeception with --debug flag.

Or you can use the verbosity controlling commands like:

codecept run -vvv

where each v increases the verbosity of the output (very silent by default).

Just call ob_flush() after outputting text

Example code:

    public function testDebugOutputToCli() {
        var_dump(new DateTime());
        ob_flush();
    }

Screenshot of code and output:

Screenshot of using ob_flush to echo out content that would otherwise have been hidden by PHPUnit

Why? PHPUnit is always output buffering, so we need to dump the buffer when debugging

I was struggling with all the answers above, especially because the selected answer –using codecept_debug() with --debug as the manual says– caused a huge wave of debug output that made it impossible to use for me.

I was reading the PHPUnit manual like a good nerd and stumbled onto this, which I think explains what causes this whole issue across all of PHPUnit, not just Codeception:

PHPUnit manual, Testing Output: “Sometimes you want to assert that the execution of a method, for instance, generates an expected output (via echo or print, for example). The PHPUnit\Framework\TestCase class uses PHP’s Output Buffering feature to provide the functionality that is necessary for this.”

This makes total sense and explains why we don't see the output. PHPUnit is saving it up in case we want to examine the comments! This is how it should always work in our actual tests, we of course don't want random stuff getting to the screen just because we called a function that uses echo.

But when we're debugging, we just want to see the text right away, and understanding all this, the solution is clear: Just use ob_flush() to print the contents of the output buffer on demand!

Three cheers for reading the fun manuals!

P.S. Also found this tip hidden in How to show var_dumps in phpunit or codeception by Julian on dev.to

By default Codeception says there was an error but doesn't show it in detail. However according to this blog post adding --debug shows the errors in detail.

codecept run --debug

Short, Nice and Easy way

Definitely codecept_debug with --debug option is one of the right way.

But --debug shows lot of verbosity which we might not need all time to see just a single variable's value. And sometimes we might need to scroll a lot at CLI to reach at our variable

However there is one other short, nice and easy way

Assert the variable with true or anything random using assertSame to display/var_dump the variable

Let's say I need to see what is inside $mango and I am certain that it is not true or 'something random'

$I->assertTrue($mango)
$I->assertSame($mango, 'something random') // I am pretty sure $mango does not equals to 'something random'

Above statements will throw error printing out $mango and the best part is that it will be printed at the bottom, so no need to scroll and no verbosity. Also in this way there is no need to add --debug in CLI command

Limitation:

Out of 10 data types in PHP

Four scalar types:


bool
int
float (floating-point number, aka double)
string


Four compound types:

array
object
callable
iterable


And finally two special types:

resource
NULL

Above my way only works well with 6:

Four scalar types:

bool
int
float (floating-point number, aka double)
string


One compound types:

array


And finally one special types:

NULL

Value of object, resource will not be printed very well.

As per OP's

I just want to be able to see my variable's content

If you have a variable like

$var="hello";

And you want to output it in the console then simply do

$I->comment($var);

$I is the default object name of the AcceptanceTester class

use Codeception\Lib\Console\Output;
$c = new Output([]);
$c->writeln("");
$c->writeln("<info>info</info>");
$c->writeln("<debug>debug</debug>");
$c->writeln("<comment>debug</comment>");
$c->writeln("<error>error</error>");

No need for noisy debug flag. All console control characters work such as \t tab etc A clean built in solution

Short version would be codecept run tests/acceptance/SomeCest.php -d
-d will show you steps and debug

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top