php PHPUnit - 转储变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8070354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:53:50  来源:igfitidea点击:

PHPUnit - Dumping variables

phpunit-testingphpunit

提问by Naatan

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

我刚刚开始使用 PHPUnit,想知道是否有一种构建方式可以转储变量的内容?

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.

用例是,因为我已经在与我正在开发的代码交谈,所以我不仅可以使用 PHPUnit 来测试该代码的稳定性,还可以在开发过程中输出调试信息。

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.

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

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

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

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:

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

回答by edorian

Update:

更新:

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

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

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

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



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.

如果您想查看吞下的输出,可以使用phpunit --debug. 这将打开所有输出产品并显示您的 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 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 --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)

回答by Gordon

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

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

var_dump($foo);
ob_flush();

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

但请注意,到目前为止,这也将刷新 PHPUnit 生成的任何输出。

回答by Paul Ciorogar

Try print_r() works for me in the terminal.

在终端中尝试 print_r() 对我有用。

回答by David Harkness

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

不,事实上 PHPUnit 3.6 会吞下来自测试的所有输出(也许只在严格模式下)。

回答by Beto Aveiga

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

正如其他人推荐的那样添加--debug为参数,然后用于dump($your_variable);检查其内容。以防万一我正在处理Drupal 8PHPUnit 6.5.13。这是一个输出示例dump

enter image description here

在此处输入图片说明