Printing debug output to console in Codeception

50,379

Solution 1

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

Solution 2

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 

Solution 3

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

and run Codeception with --debug flag.

Solution 4

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).

Solution 5

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

Share:
50,379

Related videos on Youtube

Ayame__
Author by

Ayame__

Front and back end developer, UX specialist, Javascript Ninja, victim of the mobile web, Skaven enthusiast, mother of a single dragon.

Updated on March 25, 2022

Comments

  • Ayame__
    Ayame__ over 2 years

    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.

  • Alex Jegtnes
    Alex Jegtnes over 10 years
    I was starting to think I was a bit thick for not seeing an obvious way to do this. Thanks for sharing your solution!
  • pymarco
    pymarco about 10 years
    Why not just call var_dump, print_r, or print? It works for me from within tryToTest
  • Ayame__
    Ayame__ about 10 years
    Because neither of the above worked in my tests, perhaps there's a newer version now since the original post which made it work for you?
  • Gayan Hewa
    Gayan Hewa over 9 years
    Adding the -v or -vv , -vvv flags will make all your print_r and var_dumps visible in the output.
  • coviex
    coviex almost 9 years
    debug method does not matter. you can use var_dump(). and if you run phpunit tests even --debug is not necessary.
  • jerclarke
    jerclarke almost 3 years
    This works for me. Just wish I could have this custom and temporary debug output without seeing all the --debug stuff that will show for every single run.
  • ThaJay
    ThaJay almost 3 years
    What is $I supposed to represent?
  • ThaJay
    ThaJay almost 3 years
    Unbelievable how dozens of answers across multiple questions do not talk about this. Thank you for finally suggesting a good working solution. Can I do more than just upvote to get this seen by more people?
  • Robert Sinclair
    Robert Sinclair almost 3 years
    @ThaJay it's the default object name of the AcceptanceTester class
  • ThaJay
    ThaJay almost 3 years
    It's the only one that worked for me because I needed to log both in the test and the tested code.
  • ThaJay
    ThaJay almost 3 years
    Yeah I did that to all unhelpful answers on here so it helps get the good one on top. I am prepared to remove the downvote on this one if you explain in the answer when it does or does not work and why. In my opinion, most people are looking for something simple that always works. that means the correct answer is: "use any logging function and then ob_flush()". Your suggestion is meant to add comments to the output of a test which is usable in some cases but is not really an answer to this specific question. I'm just trying to make php less weird for everyone.
  • ThaJay
    ThaJay almost 3 years
    I see you already improved the answer based on my question from yesterday, nice :)
  • Robert Sinclair
    Robert Sinclair almost 3 years
    @ThaJay the question "is there any way to print your own debug messages to the console in Codeception?" my answer answers that question, how is that unhelpful? If it's unhelpful to you it doesn't mean it won't be helpful to other people that arrive here from Google. Flagged your account for review. Cheers
  • ThaJay
    ThaJay almost 3 years
    It's unhelpful because it specifies one single possibility out of many without mentioning the benefits and drawbacks of your specific suggestion. For someone new, there would be a lot of context missing so it would probably not work for them. The same goes for most of the other answers on this question, while one of the best answers was buried at the bottom.
  • Robert Sinclair
    Robert Sinclair almost 3 years
    @thejay "It's unhelpful because it specifies one single possibility out of many without mentioning the benefits and drawbacks of your specific suggestion."... the answer answers what is being asked, what you said is an improvement, it doesn't make it unhelpful...
  • jerclarke
    jerclarke almost 3 years
    Thanks, glad it helped, hopefully we can spread the word lol. Other than downvoting other answers, which would be really nasty, your comment is probably the best thing.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.