【问题标题】:echo in phpunit tests [duplicate]phpunit测试中的回声[重复]
【发布时间】:2012-02-06 02:19:50
【问题描述】:

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

我阅读了有关 xml 配置文件的文档,显然 debug 参数是我正在寻找的。不幸的是,它仍然不起作用。无论如何,这是我的 xml 配置文件:

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

processIsolationverbose 都被接受,但 debug 不被接受。

当我像这样直接将它传递给 phpunit 时,该命令实际上工作得很好:

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

但是对于 xml 配置文件,它看起来被忽略了。

【问题讨论】:

    标签: php phpunit


    【解决方案1】:

    如果您的测试需要很长时间,并且您希望在此过程中看到输出,请将以下方法添加到您的测试类中:

    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-&gt;prontoPrint($variable) 时,它会立即在控制台中显示文本。我用这个php.net comment来写函数。

    PHPUnit 5.7.21PHP 5.6.31 with Xdebug 2.5.5

    【讨论】:

    • 不知何故此解决方案不会清理使用的缓冲区 - 也许您可以重写它以避免此消息:'测试代码或测试代码没有(仅)关闭其自己的输出缓冲区'
    • 现在已经修复了。
    【解决方案2】:

    processIsolation 正在抑制测试的输出,因此您必须禁用它。 与debug 标志的交互可能是由此引入的疏忽: https://github.com/sebastianbergmann/phpunit/pull/1489

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 我不明白,你的意思..所有相关信息都在答案中。
    • 查看已接受的答案,它提供了完整的描述和示例,您的受众是整个宇宙,而不仅仅是提问者
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      当前版本的 PHPUnit &gt;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 版本,只需升级 :)

      【讨论】:

      • 我已经在 3.6.3 上,所以我不能那样。您是如何启动 OutputTestCase 的?因为如果你用 phpunit --debug OutputTestCase.php 启动它,那么它可以工作,但否则它不会。
      • 就像我说的你需要 phpunit &gt; 3.6.4 才能工作。在 3.6.3 中,如果不运行 --debug,您将无法获得该输出。如果您升级,它将随着 PHPUnit 中的行为发生变化而工作 :)
      • 对于它的价值,这似乎在 4.1 版上根本不起作用,即使使用 --debug 选项...
      • 它在 4.8.26 中使用和不使用 --debug
      • 在 Windows Git Bash 中使用 PHPUnit 5.7.19,它适用于我(有或没有 --debug 选项)。但起初,我并没有注意到回显的字符串,因为它们被附加到我没想到它们的行中。所以我最好在前面加上一个像echo PHP_EOL . $msg; 这样的换行符。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 2013-06-20
      • 2018-05-12
      • 2020-12-09
      • 1970-01-01
      • 2012-03-25
      • 2013-11-20
      相关资源
      最近更新 更多