phpunit 测试

Warning “The attribute 'syntaxCheck' is not allowed.”

报错详情如下

Warning - The configuration file did not pass validation!
  The following problems have been detected:

  Line 11:
  - Element 'phpunit', attribute 'syntaxCheck': The attribute 'syntaxCheck' is not allowed.

  Test results may not be as expected.

意思就是phpunit下的syntaxCheck这个属性不允许存在(大概是这个意思),把这个属性删掉就可以了,其位置如下:

为什么先进行异常断言然后进行调用呢?

因为异常发生时代码就终止往下运行了,所以你需要事先告诉测试框架 " 下面的代码可能会发生异常,请帮忙检查是否为我断言的样子”,测试框架就会进行 try catch 操作啦。

// 检查 $type 参数
public function testGetWeaherWithInvalidType()
{
    $w = new Weather('mock-key');

    // 断言会抛出此异常类
    $this->expectException(InvalidArgumentException::class);

    // 断言异常消息为 'Invalid type value(base/all): foo'
    $this->expectExceptionMessage('Invalid type value(base/all): foo');

    $w->getWeather('深圳', 'foo');

    $this->fail('Failed to assert getWeather throw exception with invalid argument.');

}
posted @ 2021-04-25 16:34  caibaotimes  阅读(88)  评论(0编辑  收藏  举报