在执行PHP单元测试期间如何在CLI中输出

在执行PHP单元测试期间如何在CLI中输出

本文介绍了在执行PHP单元测试期间如何在CLI中输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When running a PHPUnit test, I would like to be able to dump output so I can debug one or two things.

我尝试了以下操作(类似于 PHPUnit手册示例);

I have tried the following (similar to the PHPUnit Manual example);

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)

请注意,没有预期的输出.

Notice there is none of the expected output.

自2011年9月19日起,我正在使用 git存储库的HEAD版本.

I'm using the HEAD versions of the git repos as of September 19th, 2011.

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错误?

Is there anything I'm doing wrong, or is this potentially a PHPUnit bug?

推荐答案

更新

刚刚意识到实现此目的的另一种方法比--verbose命令行选项好得多:

Just realized another way to do this that works much better than the --verbose command line option:

class TestSomething extends PHPUnit_Framework_TestCase {
    function testSomething() {
        $myDebugVar = array(1, 2, 3);
        fwrite(STDERR, print_r($myDebugVar, TRUE));
    }
}

这使您可以随时将任何内容转储到控制台,而不会附带--verbose CLI选项附带的所有不需要的输出.

This lets you dump anything to your console at any time without all the unwanted output that comes along with the --verbose CLI option.

正如其他答案所指出的那样,最好使用以下内置方法来测试输出:

As other answers have noted, it's best to test output using the built-in methods like:

$this->expectOutputString('foo');

但是,有时调皮并从测试用例中查看一次性的/临时的调试输出有时会很有帮助.但是,无需var_dump hack/解决方法.通过在运行测试套件时设置--verbose命令行选项,可以轻松实现此目的.例如:

However, sometimes it's helpful to be naughty and see one-off/temporary debugging output from within your test cases. There is no need for the var_dump hack/workaround, though. This can easily be accomplished by setting the --verbose command line option when running your test suite. For example:

$ phpunit --verbose -c phpunit.xml

在CLI环境中运行时,这将显示测试方法内部的输出.

This will display output from inside your test methods when running in the CLI environment.

请参阅:为PHPUnit编写测试-测试输出.

这篇关于在执行PHP单元测试期间如何在CLI中输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:14