php 在 phpunit 测试中回显

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8790271/
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 05:30:48  来源:igfitidea点击:

echo in phpunit tests

phpphpunit

提问by nourdine

I have been trying to echostuff in my phpunit tests but no luck so far.

我一直在尝试echo在我的 phpunit 测试中添加内容,但到目前为止没有运气。

I read the documentation about the xml config file and apparently the debugparameter is what I am looking for. Unfortunately it still doesn't work. Anyhow here is my xml config file:

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

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

Both processIsolationand verboseare accepted but debugis not.

这两个processIsolationverbose被接受,但debug并非如此。

The command actually works pretty fine when I directly pass it to phpunit like this:

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

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

but with the xml config file it looks like it is ignored.

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

回答by edorian

Current versions of PHPUnit >3.6.4(and all 3.5.*versions) will just print everything you echoin a test case.

当前版本的 PHPUnit >3.6.4(和所有3.5.*版本)只会打印echo测试用例中的所有内容。

<?php

class OutputTestCase extends PHPUnit_Framework_TestCase {

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

produces:

产生:

phpunit foo.php 
PHPUnit 3.6.7 by Sebastian Bergmann.

.Hi

Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 0 assertions)

So if you are on an old 3.6version just upgrade :)

因此,如果您使用的是旧3.6版本,只需升级:)

回答by Michael Morris

Make sure you don't call exit() or kill(). PHPUnit buffers the echo statements and that buffer will be lost with no output if your script exits during the test.

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

回答by Robotic-Brain

processIsolationis suppressing the output of tests so you have to disable it. The interaction with the debugflag was probably an oversight introduced with this: https://github.com/sebastianbergmann/phpunit/pull/1489

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

回答by hpaknia

In case your test takes a long time and you would like to see the output during the process, add the following method to your test class:

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

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();
    }
}

Now whenever you call $this->prontoPrint($variable)inside your test class, it will immediately show the text in the console. I used this php.net commentto write the function.

现在,每当您$this->prontoPrint($variable)在测试类中调用时,它都会立即在控制台中显示文本。我使用这个php.net 注释来编写函数。

PHPUnit 5.7.21PHP 5.6.31 with Xdebug 2.5.5

PHPUnit 5.7.21PHP 5.6.31 with Xdebug 2.5.5