在 phpunit 测试中回显

2022-01-25 00:00:00 php phpunit

我一直在尝试在我的 phpunit 测试中 echo 东西,但到目前为止没有运气.

I have been trying to echo stuff in my phpunit tests but no luck so far.

我阅读了有关 xml 配置文件的文档,显然 debug 参数是我正在寻找的.不幸的是,它仍然不起作用.无论如何,这是我的 xml 配置文件:

I read the documentation about the xml config file and apparently the debug parameter is what I am looking for. Unfortunately it still doesn't work. Anyhow here is my xml config file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
         processIsolation="true"
         verbose="true"
         debug="true">
</phpunit> 

processIsolationverbose 都被接受,但 debug 不被接受.

Both processIsolation and verbose are accepted but debug is not.

当我像这样直接将它传递给 phpunit 时,该命令实际上工作得很好:

The command actually works pretty fine when I directly pass it to phpunit like this:

phpunit --debug MyTest.php # here stuff is echoed correctly

但是对于 xml 配置文件,它看起来被忽略了.

but with the xml config file it looks like it is ignored.

推荐答案

当前版本的 PHPUnit >3.6.4(以及所有 3.5.* 版本)将只是打印您在测试用例中echo 的所有内容.

Current versions of PHPUnit >3.6.4 (and all 3.5.* versions) will just print everything you echo in a test case.

<?php

class OutputTestCase extends PHPUnit_Framework_TestCase {

    public function testEcho() {
        echo "Hi";
    }   
}

产生:

phpunit foo.php 
PHPUnit 3.6.7 by Sebastian Bergmann.

.Hi

Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 0 assertions)

因此,如果您使用的是旧的 3.6 版本,只需升级 :)

So if you are on an old 3.6 version just upgrade :)

相关文章