在运行PHPUnit测试时,我希望能够转储输出,以便调试一两个东西。

我已经尝试了以下(类似于PHPUnit手册的例子);

class theTest extends PHPUnit_Framework_TestCase
{
    /**
     * @outputBuffering disabled
     */
    public function testOutput() {
        print_r("Hello World");
        print "Ping";
        echo "Pong";
        $out = "Foo";
        var_dump($out);
    }   
}

结果如下:

PHPUnit @package_version@ by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.00Mb

OK (1 test, 0 assertions)

注意,这里没有预期的输出。

我使用的是2011年9月19日的git回购的HEAD版本。

php -version输出:

$ php -version
PHP 5.2.9 (cli) (built: Dec  8 2010 11:36:37) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

是否有什么我做错了,或者这是一个潜在的PHPUnit错误?


当前回答

简而言之,phpunit抑制了STDOUT。默认情况下,它会写入STDERR,除非你添加——verbose或——debug。你可以做这些事情中的一件:

相反,将调试输出打印到STDERR 像往常一样Var_dump调试,但是在phpunit命令行中添加——verbose Var_dump你的调试像往常一样,但添加一行ob_flush();下面 在phpunit中使用正确的命令来测试您在这里要测试的内容

显然,最后一件事是要做的好事,剩下的都是临时的小技巧。

其他回答

执行phpunit时只需使用——verbose标志。

$ phpunit --verbose -c phpunit.xml 

这种方法的优点是你不需要改变测试代码,你可以打印字符串,var_dump或任何你想要的东西,只有在设置verbose模式时,它才会显示在控制台上。

我希望这能有所帮助。

这不是漏洞,而是故意的。最好的方法是写入某种类型的日志文件,并跟踪日志以观察输出。

如果您正在尝试测试输出,请检查这个。

另外:

注意:请注意PHPUnit吞掉所有发出的输出 在测试执行期间。在严格模式下,发射的测试 输出将失败。

恶意的,但有效:抛出异常,并将调试输出作为其消息。

class theTest extends PHPUnit_Framework_TestCase
{
    public function testOutput() {
        throw new \Exception("hello");
    }   
}

收益率:

...
There was 1 error:

1) theTest::testOutput
Exception: hello

PHPUnit用ob_start()隐藏输出。我们可以暂时关闭它。

    public function log($something = null)
    {
        ob_end_clean();
        var_dump($something);
        ob_start();
    }

在某些情况下,可以使用类似的东西向控制台输出一些内容

class yourTests extends PHPUnit_Framework_TestCase
{
    /* Add Warnings */
    protected function addWarning($msg, Exception $previous = null)
    {
        $add_warning = $this->getTestResultObject();
        $msg = new PHPUnit_Framework_Warning($msg, 0, $previous);
        $add_warning->addWarning($this, $msg, time());
        $this->setTestResultObject($add_warning);
    }

    /* Add errors */
    protected function addError($msg, Exception $previous = null)
    {
        $add_error = $this->getTestResultObject();
        $msg = new PHPUnit_Framework_AssertionFailedError($msg, 0, $previous);
        $add_error->addError($this, $msg, time());
        $this->setTestResultObject($add_error);
    }

    /* Add failures */
    protected function addFailure($msg, Exception $previous = null)
    {
        $add_failure = $this->getTestResultObject();
        $msg = new PHPUnit_Framework_AssertionFailedError($msg, 0, $previous);
        $add_failure->addFailure($this, $msg, time());
        $this->setTestResultObject($add_failure);
    }

    public function test_messages()
    {
        $this->addWarning("Your warning message!");
        $this->addError("Your error message!");
        $this->addFailure("Your Failure message");
    }

    /* Or just mark test states! */
    public function test_testMarking()
    {
        $this->markTestIncomplete();
        $this->markTestSkipped();
    }
}