22

I've just started using PHPUnit and am wondering if there is a build in way of dumping the contents of a variable?

The use-case being that since I am already talking to the code I'm developing, I can use PHPUnit not only to test the stability of that code but also to output debug info while being in development.

I know xdebug can fill this gap for me, but sometimes it's just easier to dump some info in the output rather than fiddling with my IDE debugger, which is more useful for tracing back the cause of a bug.

I know I can just do a regular var_dump, I'm simply wondering if PHPUnit has an interface for this.

Thanks!

Edit:

Decided to hack it together following David's answer.

By no means a perfect solution, but it does the job for me. If anyone is interested:

*** PHPUnit-3.6.3/PHPUnit/Framework/TestCase.php    2011-11-09 12:25:38.000000000 -0500
--- PHPUnit/Framework/TestCase.php  2011-11-09 15:27:02.193317219 -0500
***************
*** 291,296 ****
--- 291,298 ----
       * @var boolean
       */
      private $outputBufferingActive = FALSE;
+   
+   public static $ob_output = array();

      /**
       * Constructs a test case with the given name.
***************
*** 913,921 ****
--- 915,927 ----
          }

          try {
+           ob_start();
              $testResult = $method->invokeArgs(
                $this, array_merge($this->data, $this->dependencyInput)
              );
+           
+           Static::$ob_output[ $method->name ] = ob_get_contents();
+           ob_end_clean();
          }

          catch (Exception $e) {

And for use with VisualPHPUnit:

*** NSinopoli-VisualPHPUnit-b7ba91a/ui/test.html    2011-11-08 15:38:44.000000000 -0500
--- ui/test.html    2011-11-09 15:38:44.797329455 -0500
***************
*** 3,15 ****
                                  <div class="name" title="Test Status: <?php echo ucfirst($test['status']);?>"><?php echo $test['name'];?></div> 
                                  <div class="stats"><?php echo $test['message'];?></div>
                                  <div class="expand button"><?php echo $test['expand'];?></div>
!                                 <div class="more test <?php echo $test['display'];?>"> 
                                      <div class="variables rounded <?php echo $test['variables_display'];?>"> 
                                          <pre><?php echo $test['variables_message'];?></pre> 
!                                     </div> 
                                      <div class="stacktrace rounded <?php echo $test['trace_display'];?>"> 
                                          <pre><?php echo $test['trace_message'];?></pre> 
!                                     </div> 
                                  </div> 
                              </div> 
                              <?php if ( $test['separator_display'] ) { ?>
--- 3,21 ----
                                  <div class="name" title="Test Status: <?php echo ucfirst($test['status']);?>"><?php echo $test['name'];?></div> 
                                  <div class="stats"><?php echo $test['message'];?></div>
                                  <div class="expand button"><?php echo $test['expand'];?></div>
!                                 <div class="more test <?php echo $test['display'];?>">
                                      <div class="variables rounded <?php echo $test['variables_display'];?>"> 
                                          <pre><?php echo $test['variables_message'];?></pre> 
!                                     </div>
                                      <div class="stacktrace rounded <?php echo $test['trace_display'];?>"> 
                                          <pre><?php echo $test['trace_message'];?></pre> 
!                                     </div>
!                                     <?php if (isset(PHPUnit_Framework_TestCase::$ob_output[$test['name']])) { ?>
!                                     <h3>OB Output</h3>
!                                     <div class="variables rounded">
!                                         <pre><?php echo PHPUnit_Framework_TestCase::$ob_output[$test['name']]; ?></pre>
!                                     </div>
!                                     <?php } ?>
                                  </div> 
                              </div> 
                              <?php if ( $test['separator_display'] ) { ?>
Naatan
  • 3,424
  • 4
  • 32
  • 51
  • 1
    Possible duplicate of [How to output in CLI during execution of PHP Unit tests?](https://stackoverflow.com/questions/7493102/how-to-output-in-cli-during-execution-of-php-unit-tests) – Jannie Theunissen Aug 02 '18 at 11:08

5 Answers5

35

Update:

Note that this answer is only relevant to PHPUnit 3.6.0 through 3.6.3.

When PHPUnit 3.6.4 is released it will not allow any output by default anymore.


Original Answer

If you want to see the swallowed output you can use phpunit --debug. This will turn all output offering and show your var_dumps.

Sample Test:

<?php

class OutputTest extends PHPUnit_Framework_TestCase {

    public function testOutput() {
        var_dump("HI!");
        $this->assertTrue(true);
    }   

}

Running phpunit outputTest.php

PHPUnit 3.6.2 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 1 assertion)

Running phpunit --debug outputTest.php

PHPUnit 3.6.2 by Sebastian Bergmann.


Starting test 'OutputTest::testOutput'.
.string(3) "HI!"


Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 1 assertion)
Community
  • 1
  • 1
edorian
  • 38,542
  • 15
  • 125
  • 143
  • 1
    Hmm the only difference this made for me is that it shows what test it's running at the time that it's running it. It did not show me the dumped output. – Naatan Nov 10 '11 at 13:50
  • @Naatan I've added and example to show of how it works. Did you add the '--debug' after your path maybe? Than it doesn't work :) – edorian Nov 10 '11 at 14:04
  • 1
    Thanks edorian, my mistake was I was trying it with my workaround, which was obviously already capturing (stealing) the output. Your method works fine and doesn't require any PHPUnit modifications. Thanks! :) – Naatan Nov 10 '11 at 15:44
20

using ob_flush() will work as well, e.g. in your test

var_dump($foo);
ob_flush();

but note that this will also flush any output generated by PHPUnit so far as well.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Thanks, I had not thought of that. But as you said this can cause some problems with PHPUnit outputting data of it's own. I think edorian's method will provide a less error-prone solution. – Naatan Nov 10 '11 at 15:46
10

Try print_r() works for me in the terminal.

5

No, and in fact PHPUnit 3.6 will swallow all output coming from a test (perhaps only in strict mode).

David Harkness
  • 35,992
  • 10
  • 112
  • 134
  • Yeah I just noticed this too. So there's no possible way of getting the output? Aside from hacking PHPUnit .. – Naatan Nov 09 '11 at 20:05
  • *just to note it*: Output swallowing is not influenced by strict mode – edorian Nov 10 '11 at 14:04
  • @DavidHarkness Only --debug will allow you to view the output. But you'll get the other debug stuff too as shown above – edorian Nov 10 '11 at 19:00
  • Ah, nice to see it can be disabled with `--debug`. I thought I remembered there being a setting that affected it. – David Harkness Nov 10 '11 at 19:01
3

As others recommended add --debug as a parameter and then use dump($your_variable); to inspect its contents. Just in case I'm working on Drupal 8 and PHPUnit 6.5.13. Here is an output example of dump:

enter image description here

Beto Aveiga
  • 3,466
  • 4
  • 27
  • 39