【问题标题】:why error_log generate a RuntimeException only with phpunit's --process-isolation为什么 error_log 仅使用 phpunit 的 --process-isolation 生成 RuntimeException
【发布时间】:2013-04-19 06:13:53
【问题描述】:

我进行了一些测试,其中正在测试的代码输出了一些 error_log 信息(因为我也在测试失败,这是意料之中的)

例如

<?php
Class X {
    function dosomething(){
            error_log('did it');
            return true;
    }
}

和测试:

<?php
require_once 'X.php';
class xTest extends PHPUnit_Framework_TestCase {
    public function testDo(){
            $x = new X();
            $this->assertTrue( $x->dosomething(), 'dosomething returns true');
    }
}

在 php_unit 没有 --process-isolation 上运行时,. 给了我一个很好的 . 以及我正在测试的任何内容。

然而,当 with --process-isolation 运行时,我得到:

1) test::a with data set #1 'a'
RuntimeException: did it

为什么会这样?我被困在 3.4.12 版本上(对此无能为力),但在更新日志中没有发现任何有趣的内容。

这是一个示例会话:

xxx$ phpunit xTest.php
PHPUnit 3.4.12 by Sebastian Bergmann.

did it
.

Time: 0 seconds, Memory: 4.50Mb

OK (1 test, 1 assertion)

shell return code: 0
xxx$ phpunit --process-isolation xTest.php
PHPUnit 3.4.12 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 4.50Mb

There was 1 error:

1) xTest::testDo
RuntimeException: did it


FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

shell return code: 2

编辑:我正在搜索“phpunit runtimeexception error_log”,提交后 5 秒它已经是最热门的搜索结果了 :( 那里什么都没有。

但我来这里是为了编辑并这样说: 添加$this-&gt;setExpectedException('RuntimeException'); 绝对不能捕捉到这一点。发生同样的结果。

【问题讨论】:

    标签: php unit-testing exception testing phpunit


    【解决方案1】:

    我的猜测是error_log 配置在外部和内部过程中存在差异。默认情况下,当从 CLI 运行 error_log() 时,它将被写入 stderr 并导致测试失败。你需要做的是弄清楚为什么会有差异。我猜你有一些在外部进程中运行的代码改变了在内部没有运行的配置。

    【讨论】:

    • 我假设只有返回代码会导致测试失败。所以我也必须避开 stderr 输出?使用调试标志来测试我的代码很糟糕......我什至如何重载/模拟error_log?可能不得不在另一个类中添加一个抽象来取悦测试。
    • 事后添加测试可能会很痛苦。请记住,PHPUnit 旨在与测试驱动开发一起使用。
    • 在这种情况下,即使它是从零开始的 TDD。我永远无法测试具有 error_log 副作用的类。
    • 有不同级别的测试——单元、集成、系统等。我们在这里讨论单元测试。这意味着需要隔离被测类。在生产中,您很可能会注入一个调用error_log() 的对象。这意味着在测试用例中,您将创建一个模拟测试对象。当一个项目从一开始就使用 TDD 创建时,由于这些需求,它的结构会非常不同。由于现有代码的结构,事后添加单元测试可能非常困难。有时从集成或系统测试开始会更容易。
    猜你喜欢
    • 2012-04-06
    • 2017-11-12
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2016-11-18
    • 2019-12-27
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多