如何在执行 PHP 单元测试期间在 CLI 中输出? [英] How to output in CLI during execution of PHP Unit tests?
问题描述
在运行 PHPUnit 测试时,我希望能够转储输出,以便调试一两件事.
我尝试了以下(类似于 PHPUnit 手册示例);
class theTest 扩展 PHPUnit_Framework_TestCase{/*** @outputBuffering 禁用*/公共函数 testOutput() {print_r("你好世界");打印平";echo "乒乓";$out = "Foo";var_dump($out);}}
结果如下:
PHPUnit @package_version@ 由 Sebastian Bergmann..时间:0 秒,内存:3.00MbOK(1 个测试,0 个断言)
注意没有任何预期的输出.
截至 2011 年 9 月 19 日,我使用的是 git repos 的 HEAD 版本.>
php -version
的输出:
$ php -versionPHP 5.2.9 (cli)(构建时间:2010 年 12 月 8 日 11:36:37)版权所有 (c) 1997-2009 The PHP GroupZend Engine v2.2.0,版权所有 (c) 1998-2009 Zend Technologies使用 Xdebug v2.1.0,版权所有 (c) 2002-2010,Derick Rethans
我做错了什么,或者这可能是 PHPUnit 的错误吗?
UPDATE
刚刚发现另一种比 --verbose
命令行选项更有效的方法:
class TestSomething 扩展 PHPUnit_Framework_TestCase {函数 testSomething() {$myDebugVar = 数组(1, 2, 3);fwrite(STDERR, print_r($myDebugVar, TRUE));}}
这让您可以随时将任何内容转储到您的控制台,而不会出现 --verbose
CLI 选项附带的所有不需要的输出.
正如其他答案所指出的,最好使用内置方法测试输出,例如:
$this->expectOutputString('foo');
但是,有时调皮并从测试用例中查看一次性/临时调试输出是有帮助的.但是,不需要 var_dump
hack/解决方法.这可以通过在运行测试套件时设置 --verbose
命令行选项来轻松完成.例如:
$ phpunit --verbose -c phpunit.xml
这将显示在 CLI 环境中运行时测试方法内部的输出.
When running a PHPUnit test, I would like to be able to dump output so I can debug one or two things.
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);
}
}
With the following result:
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.
I'm using the HEAD versions of the git repos as of September 19th, 2011.
Output of 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
Is there anything I'm doing wrong, or is this potentially a PHPUnit bug?
UPDATE
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));
}
}
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');
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
This will display output from inside your test methods when running in the CLI environment.
See: Writing Tests for PHPUnit - Testing Output.
这篇关于如何在执行 PHP 单元测试期间在 CLI 中输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!