Naatan
Naatan

Reputation: 3444

PHPUnit - Dumping variables

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'] ) { ?>

Upvotes: 22

Views: 42649

Answers (5)

Beto Aveiga
Beto Aveiga

Reputation: 3670

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

Upvotes: 4

user2594579
user2594579

Reputation:

Try print_r() works for me in the terminal.

Upvotes: 10

edorian
edorian

Reputation: 38961

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)

Upvotes: 35

Gordon
Gordon

Reputation: 316979

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.

Upvotes: 20

David Harkness
David Harkness

Reputation: 36542

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

Upvotes: 5

Related Questions