2012-01-09 102 views
11

我一直在尝试echo东西在我的phpunit测试中,但没有运气到目前为止。在phpunit测试中回显

我读了关于xml配置文件的文档,显然debug参数是我在找的。不幸的是它仍然不起作用。总之,这里是我的xml配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit colors="true" 
     processIsolation="true" 
     verbose="true" 
     debug="true"> 
</phpunit> 

两个processIsolationverbose被接受,但debug不是。

phpunit --debug MyTest.php # here stuff is echoed correctly 

,但使用XML配置文件,它看起来像它被忽略了:

,当我直接将它传递给PHPUnit的这样的命令实际上工作得很好。

回答

15

当前版本的PHPUnit >3.6.4(以及所有3.5.*版本)只会在测试用例中打印所有echo

<?php 

class OutputTestCase extends PHPUnit_Framework_TestCase { 

    public function testEcho() { 
     echo "Hi"; 
    } 
} 

生产:

phpunit foo.php 
PHPUnit 3.6.7 by Sebastian Bergmann. 

.Hi 

Time: 0 seconds, Memory: 3.25Mb 

OK (1 test, 0 assertions) 

所以,如果你是在一个旧版本3.6刚刚升级:)

+2

我在3.6.3已经让我不能说。你是如何启动OutputTestCase的?因为如果你推出它与 phpunit --debug OutputTestCase.php 然后是它的工作,但否则它不。 – nourdine 2012-01-11 08:56:10

+2

就像我说你**需要**'phpunit> 3.6.4'这个工作。使用3.6.3,你将不会得到那个输出,而不运行--debug。如果你升级它将会像PHPUnit中的行为改变一样工作:) – edorian 2012-01-11 09:01:30

+7

为什么它值得这个版本在4.1版上似乎没有任何作用,即使使用了--debug选项... – 2014-05-18 20:00:17

3

确保你不调用exit()或中止()。如果您的脚本在测试期间退出,PHPUnit将缓冲echo语句,并且缓冲区将丢失,并且不输出。

1

processIsolation正在抑制测试的输出,因此您必须将其禁用。 与debug标志的相互作用可能与此介绍一个疏忽: https://github.com/sebastianbergmann/phpunit/pull/1489

+2

虽然这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/13915236) – Serjik 2016-10-08 10:49:54

+0

我不明白,你的意思是什么.. 所有的相关信息都在答案中。 – 2016-10-09 13:02:26

+0

看看接受的答案,它充分描述和提供的例子,你的观众是整个宇宙不仅仅是问题提问者 – Serjik 2016-10-09 13:10:09

0

如果你的测试需要很长的时间,你想看到的过程中输出,添加以下方法来测试类:

protected function prontoPrint($whatever = 'I am printed!') 
{ 
    // if output buffer has not started yet 
    if (ob_get_level() == 0) { 
     // current buffer existence 
     $hasBuffer = false; 
     // start the buffer 
     ob_start(); 
    } else { 
     // current buffer existence 
     $hasBuffer = true; 
    } 

    // echo to output 
    echo $whatever; 

    // flush current buffer to output stream 
    ob_flush(); 
    flush(); 
    ob_end_flush(); 

    // if there were a buffer before this method was called 
    //  in my version of PHPUNIT it has its own buffer running 
    if ($hasBuffer) { 
     // start the output buffer again 
     ob_start(); 
    } 
} 

现在只要您拨打$this->prontoPrint($variable)您的测试类中,它会立即显示在控制台中的文本。我用这个php.net comment来编写函数。

PHPUnit 5.7.21PHP 5.6.31 with Xdebug 2.5.5

+0

不知何故,这个解决方案不清理使用的缓冲区 - 也许你可以重写它以避免这样的消息: '测试代码或经测试的代码没有(仅)关闭它自己的输出缓冲区' – serup 2017-11-28 07:33:27