PHPUnit - 转储变量

Naa*_*tan 14 php phpunit unit-testing

我刚刚开始使用PHPUnit,并且想知道是否存在转储变量内容的构建方式?

用例是因为我已经在谈论我正在开发的代码,所以我可以使用PHPUnit来测试代码的稳定性,还可以在开发时输出调试信息.

我知道xdebug可以为我填补这个空白,但有时候只是更容易在输出中转储一些信息而不是摆弄我的IDE调试器,这对于追溯错误的原因更有用.

我知道我可以做一个常规的var_dump,我只是想知道PHPUnit是否有一个接口.

谢谢!

编辑:

决定按照大卫的回答一起破解它.

绝不是一个完美的解决方案,但它能为我完成这项工作.如果有人有兴趣:

*** 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) {
Run Code Online (Sandbox Code Playgroud)

并与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'] ) { ?>
Run Code Online (Sandbox Code Playgroud)

edo*_*ian 22

更新:

请注意,此答案仅与PHPUnit 3.6.0到3.6.3相关.

当PHPUnit 3.6.4发布时,它将不再允许任何默认输出.


原始答案

如果要查看吞下的输出,可以使用phpunit --debug.这将打开所有输出产品并显示你的var_dumps.

样品测试:

<?php

class OutputTest extends PHPUnit_Framework_TestCase {

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

}
Run Code Online (Sandbox Code Playgroud)

运行 phpunit outputTest.php

PHPUnit 3.6.2 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 1 assertion)
Run Code Online (Sandbox Code Playgroud)

运行 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)
Run Code Online (Sandbox Code Playgroud)


Gor*_*don 11

使用ob_flush()也会起作用,例如在你的测试中

var_dump($foo);
ob_flush();
Run Code Online (Sandbox Code Playgroud)

但请注意,这也将刷新PHPUnit生成的任何输出.


小智 9

尝试print_r()在终端中为我工作.


Dav*_*ess 5

不,事实上 PHPUnit 3.6 将吞掉来自测试的所有输出(可能仅在严格模式下)。