18

I have been trying to echo stuff in my phpunit tests but no luck so far.

I read the documentation about the xml config file and apparently the debug parameter is what I am looking for. Unfortunately it still doesn't work. Anyhow here is my xml config file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
         processIsolation="true"
         verbose="true"
         debug="true">
</phpunit> 

Both processIsolation and verbose are accepted but debug is not.

The command actually works pretty fine when I directly pass it to phpunit like this:

phpunit --debug MyTest.php # here stuff is echoed correctly

but with the xml config file it looks like it is ignored.

edorian
  • 38,542
  • 15
  • 125
  • 143
nourdine
  • 7,407
  • 10
  • 46
  • 58

4 Answers4

18

Current versions of PHPUnit >3.6.4 (and all 3.5.* versions) will just print everything you echo in a test case.

<?php

class OutputTestCase extends PHPUnit_Framework_TestCase {

    public function testEcho() {
        echo "Hi";
    }   
}

produces:

phpunit foo.php 
PHPUnit 3.6.7 by Sebastian Bergmann.

.Hi

Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 0 assertions)

So if you are on an old 3.6 version just upgrade :)

edorian
  • 38,542
  • 15
  • 125
  • 143
  • 2
    I am on 3.6.3 already so I cannot be that. How did you launch OutputTestCase? Cuz if you launched it with phpunit --debug OutputTestCase.php then yes it works but otherwise it doesn't. – nourdine Jan 11 '12 at 08:56
  • 2
    Like i stated you **need** `phpunit > 3.6.4` for this to work. With 3.6.3 you will NOT get that output without running --debug. If you upgrade it will work as the behavior in PHPUnit changed :) – edorian Jan 11 '12 at 09:01
  • 8
    For what it's worth this does not seem to work at all on version 4.1, even with the --debug option... – ellipse-of-uncertainty May 18 '14 at 20:00
  • It works in 4.8.26 with and without --debug – Jeff Puckett Jun 04 '16 at 18:26
  • Using PHPUnit 5.7.19 in Windows Git Bash, it works for me (with or without the `--debug` option). But at first, I hadn't been noticing the echoed strings because they were appended to lines where I didn't expect them to be. So it's better for me to prepend a newline like `echo PHP_EOL . $msg;`. – Ryan May 23 '17 at 15:13
  • I also realized that my `echo` needs to come before any assertions that might fail or have errors, and also there cannot be any `die` or `exit` or `kill` in the function. – Ryan Jul 14 '17 at 13:56
3

Make sure you don't call exit() or kill(). PHPUnit buffers the echo statements and that buffer will be lost with no output if your script exits during the test.

Michael Morris
  • 531
  • 5
  • 7
2

In case your test takes a long time and you would like to see the output during the process, add the following method to your test class:

protected function prontoPrint($whatever = 'I am printed!')
{
    // if output buffer has not started yet
    if (ob_get_level() == 0) {
        // current buffer existence
        $hasBuffer = false;
        // start the buffer
        ob_start();
    } else {
        // current buffer existence
        $hasBuffer = true;
    }

    // echo to output
    echo $whatever;

    // flush current buffer to output stream
    ob_flush();
    flush();
    ob_end_flush();

    // if there were a buffer before this method was called
    //      in my version of PHPUNIT it has its own buffer running
    if ($hasBuffer) {
        // start the output buffer again
        ob_start();
    }
}

Now whenever you call $this->prontoPrint($variable) inside your test class, it will immediately show the text in the console. I used this php.net comment to write the function.

PHPUnit 5.7.21 PHP 5.6.31 with Xdebug 2.5.5

hpaknia
  • 2,769
  • 4
  • 34
  • 63
  • somehow this solution does not cleanup the used buffer - perhaps you could rewrite it to avoid this message : 'Test code or tested code did not (only) close its own output buffers' – serup Nov 28 '17 at 07:33
  • It's fixed now. – hpaknia Dec 11 '20 at 02:31
1

processIsolation is suppressing the output of tests so you have to disable it. The interaction with the debug flag was probably an oversight introduced with this: https://github.com/sebastianbergmann/phpunit/pull/1489

Robotic-Brain
  • 13
  • 1
  • 5
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/13915236) – Serjik Oct 08 '16 at 10:49
  • I don't understand, what you mean.. All the relevant information is in the answer. – Robotic-Brain Oct 09 '16 at 13:02
  • look at accepted answer, it fully described and example provided, your audience is the whole universe not just the question asker – Serjik Oct 09 '16 at 13:10