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'] ) { ?>
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:
Running
phpunit outputTest.phpRunning
phpunit --debug outputTest.php