在运行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错误?


当前回答

更新:参见rdlowrey的更新下面关于使用fwrite(STDERR, print_r($myDebugVar, TRUE));作为一个简单得多的工作


这种行为是有意为之的(正如jasonbar所指出的)。手册的冲突状态已报告给PHPUnit。

一种变通方法是让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);
    }   
}

给:

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.

一定要禁用用于测试的任何其他断言,因为它们可能在测试输出断言之前失败(因此您将看不到输出)。

其他回答

我必须修改这段代码的源代码才能工作,所以你需要为这个fork repos添加URL到composer,这样才能工作

class TestCase extends \PHPUnit_Framework_TestCase
{
    /**
     *  Save last response
     * @var Response|null A Response instance
     */
    static $lastResponse;
    /**
     *  Modify to save response
     *
     * @param  string $method
     * @param  string $uri
     * @param  array $parameters
     * @param  array $files
     * @param  array $server
     * @param  string $content
     * @param  bool $changeHistory
     * @return \Illuminate\Http\Response
     */
    final public function call(
        $method,
        $uri,
        $parameters = [],
        $files = [],
        $server = [],
        $content = null,
        $changeHistory = true
    ) {

        $response = parent::call($method, $uri, $parameters, $files, $server, $content, $changeHistory);
        static::$lastResponse = $this->client->getResponse();
        return $response;
    }


    /**
     * Modify message to add response text
     *
     * @param mixed $value
     * @param PHPUnit_Framework_Constraint $constraint
     * @param string $message
     * @since  Method available since Release 3.0.0
     */
    final public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
    {
        $message .= PHP_EOL . static::$lastResponse . PHP_EOL;
        parent::assertThat($value, $constraint, $message);
    }
}

尝试使用——debug

如果您试图获得包含数据文件或源数据文件的正确路径,则此选项非常有用。

可以使用Symfony\Component\Console\Output\TrimmedBufferOutput,然后像这样测试缓冲的输出字符串:

use Symfony\Component\Console\Output\TrimmedBufferOutput;

//...
public function testSomething()
{
   $output = new TrimmedBufferOutput(999);
   $output->writeln('Do something in your code with the output class...');
   
   //test the output:
   $this->assertStringContainsString('expected string...', $output->fetch());   
}

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

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

另外:

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

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

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