dragon321723 2016-10-09 01:57
浏览 109


When I run phpunit I get:

1) FooTests::testException assert(): Assertion "false" failed

I would like to expect the assert in the case I have.

class FooTests extends WP_UnitTestCase {

  protected $foo;

    public function setUp() {
        $this->foo = new Foo();

    function testException() {
        // I'd like to expect an assert in the class foo so the test should not fail.  

class Foo {
    public function __construct(){

    public function __destruct(){}

    public function test(){

  • 写回答

1条回答 默认 最新

  • duanrao3371 2016-10-09 07:48

    You can achieve in one of the following manner:

    1) Catch the PHPUnit warning exception

    PHP emit a warning for each failed assertion, so PHPUnit raise an exception of the type PHPUnit_Framework_Error_Warning. As described in the doc:

    By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception.


    PHPUnit_Framework_Error_Notice and PHPUnit_Framework_Error_Warning represent PHP notices and warnings, respectively.

    So you can simply catch in the following manner:

    public function testException() {

    2) Using a callback on failed assertion

    You could do something more clear using the assert_options, using as callback a custom exception and handle it as example:

    public function test_using_assert_options_PHP5()
        $fnc = function() {
            throw new \Exception('assertion failed', 500);
        $this->expectExceptionMessage('assertion failed');
        assert_options(ASSERT_CALLBACK, $fnc);

    3) Change the behaviour of the failing exception (only from PHP7)

    If you are using PHP7 you could implement this last behaviour with a new settings called assert.exception:

    public function test_using_assert_options_PHP7()
        assert_options(ASSERT_EXCEPTION, 1);

    Hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗