50

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.

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
Ayame__
  • 980
  • 2
  • 9
  • 21

10 Answers10

74

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 
Bae
  • 7,516
  • 5
  • 36
  • 42
  • 2
    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. – jerclarke Jul 09 '21 at 19:24
54
\Codeception\Util\Debug::debug($this->em);die();

and run Codeception with --debug flag.

sennett
  • 8,014
  • 9
  • 46
  • 69
  • debug method does not matter. you can use `var_dump()`. and if you run **phpunit** tests even **--debug** is not necessary. – coviex Jul 10 '15 at 13:27
10

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

Ayame__
  • 980
  • 2
  • 9
  • 21
  • I was starting to think I was a bit thick for not seeing an obvious way to do this. Thanks for sharing your solution! – Alex Jegtnes Feb 23 '14 at 17:58
  • 1
    Why not just call `var_dump`, `print_r`, or `print`? It works for me from within `tryToTest` – pymarco May 08 '14 at 21:33
  • 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? – Ayame__ May 15 '14 at 14:18
  • Adding the -v or -vv , -vvv flags will make all your print_r and var_dumps visible in the output. – Gayan Hewa Feb 19 '15 at 04:37
7

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

Luca Tumedei
  • 87
  • 1
  • 3
4

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

jerclarke
  • 1,219
  • 1
  • 13
  • 23
  • 2
    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? – ThaJay Sep 14 '21 at 08:20
  • 1
    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. – jerclarke Sep 21 '21 at 17:26
  • 1
    This one works very well! Just remember to show the dumps before any assertions for Codeception. The output will be way up, but it will be there. Thank you for this solution. – WeidMaster Aug 19 '22 at 21:14
2

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

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46
  • What is `$I` supposed to represent? – ThaJay Sep 14 '21 at 08:19
  • 1
    @ThaJay it's the default object name of the AcceptanceTester class – Robert Sinclair Sep 14 '21 at 17:15
  • It's the only one that worked for me because I needed to log both in the test and the tested code. – ThaJay Sep 15 '21 at 08:19
  • 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 Sep 16 '21 at 09:44
  • I see you already improved the answer based on my question from yesterday, nice :) – ThaJay Sep 16 '21 at 09:50
  • @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 – Robert Sinclair Sep 16 '21 at 14:54
  • 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. – ThaJay Sep 17 '21 at 07:45
  • @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... – Robert Sinclair Sep 17 '21 at 18:46
1

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

Bibek Shrestha
  • 32,848
  • 7
  • 31
  • 34
1

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.

Sohel Ahmed Mesaniya
  • 3,344
  • 1
  • 23
  • 29
0
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

Stovesy
  • 1
  • 1
  • 1
    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](/help/how-to-answer). – Community Mar 24 '22 at 15:24
-1

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

Stipe
  • 480
  • 5
  • 18