PHPUnit - 转储变量

问题描述 投票:13回答:5

我刚刚开始使用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) {

并与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'] ) { ?>
php unit-testing phpunit
5个回答
22
投票

Update:

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

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


Original Answer

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

样品测试:

<?php

class OutputTest extends PHPUnit_Framework_TestCase {

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

}

运行phpunit outputTest.php

PHPUnit 3.6.2 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 1 assertion)

运行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)

11
投票

使用ob_flush()也可以,例如在你的测试中

var_dump($foo);
ob_flush();

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


9
投票

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


4
投票

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


0
投票

正如其他人建议添加--debug作为参数,然后使用dump($your_variable);检查其内容。以防万一我在Drupal 8PHPUnit 6.5.13工作。这是dump的输出示例:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.