web-dev-qa-db-ja.com

PHPUnit-変数のダンプ

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

更新:

この回答は、PHPUnit3.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);
    }   

}

実行中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)
23
edorian

ob_flush()の使用も同様に機能します。あなたのテストで

var_dump($foo);
ob_flush();

ただし、これにより、これまでにPHPUnitによって生成された出力もすべてフラッシュされることに注意してください。

12
Gordon

ターミナルでprint_r()が機能するようにしてください。

10
Paul Ciorogar

いいえ、実際、PHPUnit 3.6はテストからのすべての出力を飲み込みます(おそらく厳密モードでのみ)。

4
David Harkness

他の人が推奨するように、パラメータとして_--debug_を追加してから、dump($your_variable);を使用してその内容を調べます。 _Drupal 8_と_PHPUnit 6.5.13_に取り組んでいる場合に備えて。 dumpの出力例を次に示します。

enter image description here

2
Beto Aveiga