如何在执行 PHP 单元测试期间在 CLI 中输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7493102/
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
How to output in CLI during execution of PHP Unit tests?
提问by Jess Telford
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);
我尝试了以下(类似于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);
}
}
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 reposas of September 19th, 2011.
截至 2011 年 9 月 19 日,我正在使用git 存储库的 HEAD 版本。
Output of php -version
:
的输出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?
有什么我做错了,或者这可能是一个 PHPUnit 错误?
回答by rdlowrey
UPDATE
更新
Just realized another way to do this that works much better than the --verbose
command line option:
刚刚意识到另一种比--verbose
命令行选项更有效的方法:
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.
这使您可以随时将任何内容转储到控制台,而无需--verbose
CLI 选项附带的所有不需要的输出。
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:
但是,有时调皮并从测试用例中查看一次性/临时调试输出是有帮助的。但是,不需要var_dump
黑客/解决方法。这可以通过--verbose
在运行测试套件时设置命令行选项来轻松完成。例如:
$ phpunit --verbose -c phpunit.xml
This will display output from inside your test methods when running in the CLI environment.
这将显示在 CLI 环境中运行时测试方法内部的输出。
回答by Jess Telford
Update:See rdlowrey's update belowregarding the use of fwrite(STDERR, print_r($myDebugVar, TRUE));
as a much simpler work around
更新:请参阅下面的 rdlowrey 更新,了解如何使用fwrite(STDERR, print_r($myDebugVar, TRUE));
更简单的解决方法
This behaviour is intentional (as jasonbarhas pointed out). The conflicting state of the manual has been reportedto PHPUnit.
此行为是故意的(如jasonbar已经指出的)。手册的冲突状态已报告给 PHPUnit。
A work-around is to have PHPUnit assert the expected output is empty (when infact there is output) which will trigger the unexpected output to be shown.
一种解决方法是让 PHPUnit 断言预期输出为空(当实际上有输出时),这将触发显示意外输出。
class theTest extends PHPUnit_Framework_TestCase
{
/**
* @outputBuffering disabled
*/
public function testOutput() {
$this->expectOutputString(''); // tell PHPUnit to expect '' as output
print_r("Hello World");
print "Ping";
echo "Pong";
$out = "Foo";
var_dump($out);
}
}
gives:
给出:
PHPUnit @package_version@ by Sebastian Bergmann.
F
Time: 1 second, Memory: 3.50Mb
There was 1 failure:
1) theTest::testOutput
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-''
+'Hello WorldPingPongstring(4) "Foo"
+'
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Be certain to disable any other assertions you have for the test as they may fail before the output assertion is tested (and hence you wont see the output).
一定要禁用测试的任何其他断言,因为它们可能会在测试输出断言之前失败(因此您不会看到输出)。
回答by chim
Try using --debug
尝试使用 --debug
Useful if you're trying to get the right path to an include or source data file.
如果您试图获得包含或源数据文件的正确路径,则很有用。
回答by jasonbar
It's not a bug, but very much intentional. Your best bet is to write to a log file of some kind and tail the log to watch for output.
这不是一个错误,而是非常有意的。最好的办法是写入某种日志文件并跟踪日志以观察输出。
If you are trying to TEST output, check thisout.
如果您正在尝试测试输出,请检查这一点。
Also:
还:
Note: Please note that PHPUnit swallows all output that is emitted during the execution of a test. In strict mode, a test that emits output will fail.
注意:请注意 PHPUnit 会吞下测试执行期间发出的所有输出。在严格模式下,发出输出的测试将失败。
回答by Bob Stein
I'm having some luck with VisualPHPUnit, and it does helpfully show output, among other things.
我对VisualPHPUnit有一些运气,它确实有助于显示输出等。
class TestHello extends PHPUnit_Framework_TestCase
{
public function test_Hello()
{
print "hello world";
}
}
回答by cweiske
You should really think about your intentions: If you need the information now when debugging to fix the test, you will need it next week again when the tests break.
你真的应该考虑一下你的意图:如果你现在在调试时需要这些信息来修复测试,那么下周测试中断时你将再次需要它。
This means that you will need the information alwayswhen the test fails - and adding a var_dump
to find the cause is just too much work. Rather put the data into your assertions.
这意味着当测试失败时您将始终需要这些信息- 添加一个var_dump
来查找原因只是太多的工作。而是将数据放入您的断言中。
If your code is too complex for that, split it up until you reach a level where one assertion (with a custom message) tells you enough to know where it broke, why and how to fix the code.
如果您的代码太复杂,请将其拆分,直到达到一个断言(带有自定义消息)告诉您足以知道它在哪里出错、为什么以及如何修复代码的级别。
回答by Branny Bk
回答by Fabricio
Just use the --verboseflag when execute phpunit.
执行phpunit时只需使用--verbose标志。
$ phpunit --verbose -c phpunit.xml
The advantage of this method is that you don't need to change the test code, you can print strings, var_dump's o anything you wish always and it will be shown in the console only when verbosemode is set.
这种方法的优点是你不需要更改测试代码,你可以打印字符串,var_dump 的 o 任何你想要的东西,并且只有在设置了详细模式时才会在控制台中显示。
I hope this helps.
我希望这有帮助。
回答by Matthias Rella
Hackish, but works: Throw an exception with the debug output as its message.
Hackish,但有效:使用调试输出作为其消息抛出异常。
class theTest extends PHPUnit_Framework_TestCase
{
public function testOutput() {
throw new \Exception("hello");
}
}
Yields:
产量:
...
There was 1 error:
1) theTest::testOutput
Exception: hello
回答by mkungla
For some cases one could use something like that to output something to the console
在某些情况下,可以使用类似的东西向控制台输出一些东西
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();
}
}