如何让PHPUnit打印内部异常?

如何让PHPUnit打印内部异常?,php,phpunit,Php,Phpunit,有没有办法让PHPUnit在出现问题时打印内部异常 下面是一个简单的例子: class TestTest extends \PHPUnit\Framework\TestCase { function test() { throw new Exception("Outer exception message", 0, new Exception("Inner exception message")); } } 我想看到这两条消息,但我只看到外部消息 装饰图案很有用

有没有办法让PHPUnit在出现问题时打印内部异常

下面是一个简单的例子:

class TestTest extends \PHPUnit\Framework\TestCase
{
    function test() {
        throw new Exception("Outer exception message", 0, new Exception("Inner exception message"));
    }
}
我想看到这两条消息,但我只看到外部消息

  • 装饰图案很有用
  • class OuterDecoratorException扩展\Exception{
    私人帐户例外;
    公共函数构造(字符串$message,\Exception$innerException){
    $this->message=$message;
    $this->innerException=$innerException;
    }
    公共函数\uuuToString():字符串{
    返回\sprintf(
    “外部:%s内部:%s”,
    $this->getMessage(),
    $this->innerException->getMessage()
    );
    }
    }
    抛出新的OuterDecoratorException(“外部消息”,new\Exception(“内部消息”);
    
    2。与装饰程序相比,更喜欢在每次测试中直接编译异常消息。

    类TestTest扩展\PHPUnit\Framework\TestCase
    {
    功能测试(){
    抛出新\异常(
    斯普林特(
    “内部:%s外部:%s”,
    “内在讯息”,
    (新建\异常(“外部消息”)->getMessage()
    ),
    0
    );
    }
    }
    
  • 否则,必须扩展库。请参阅手册-如何扩展框架
  • $cat test.php
    
    然后我将丢失内部异常的堆栈跟踪。如果您使用的是默认结果打印机,这也会严重破坏我在应用程序中处理异常的方式。“你还想看别的吗?”毕晓普说得对。它已经这样做了,但这样你就不能测试内部和外部异常消息了。@bishop,我在那行代码上放了一个断点,但它从未被调用过。我的配置文件中没有定义监听器。@RyanVincent,我有时有一个catch块,它捕获某个库或其他库引发的一般异常,并抛出一个包含更多信息的异常。当我这样做时,我仍然希望看到完整的堆栈跟踪。外部异常通常也是另一种类型。这让我一眼就能看出我的应用程序需要修复的部分。@RyanVincent,是的,我认为异常应该导致代码修复(如果适用)。
    $ cat TestTest.php
    <?php
    require_once 'vendor/autoload.php';
    class TestTest extends PHPUnit_Framework_TestCase {
        function test() {
            throw new Exception("Outer exception message", 0, new Exception("Inner exception message"));
        }
    }
    
    $ ./vendor/bin/phpunit TestTest.php
    PHPUnit 5.3.5 by Sebastian Bergmann and contributors.
    
    E                                                                   1 / 1 (100%)
    
    Time: 57 ms, Memory: 4.25MB
    
    There was 1 error:
    
    1) TestTest::test
    Exception: Outer exception message
    
    /tmp/TestTest.php:7
    
    Caused by
    Exception: Inner exception message
    
    /tmp/TestTest.php:7
    
    FAILURES!
    Tests: 1, Assertions: 0, Errors: 1.