Kzq*_*qai 16 php phpunit unit-testing assertions
我想声明一个变量是phpunit中的一个(非空白)字符串,但我不想断言字符串必须匹配任何完全字符串.
例如,我想拉一个用户名,并确保我成功获得了一些非空白用户名,但我并不确切知道我的用户名.
我可以很容易断言它是一个非空变量,或者它是一个完全匹配某个字符串的字符串,或断言var是没有phpunit帮助的字符串:
$this->assertNotEmpty($username);
$this->assertSame('myusername', $username);
$this->assertTrue(is_string($username));
Run Code Online (Sandbox Code Playgroud)
这些都接近我所需要的,使用is_string实际上测试正确的条件,但是自己做is_string还不够好,因为当测试失败时我不能再得到有用的,信息丰富的消息了,而不是告诉我实际返回了什么类型的值,错误消息变得无用:
Failed asserting that false is true.
Run Code Online (Sandbox Code Playgroud)
那么如何使用phpunit的断言系统断言var是string类型和非空白?
vas*_*ite 35
您可以将自己的消息添加到所有PHPUnit断言中,这样的事情对您有用: -
$this->assertTrue(is_string($username), "Got a " . gettype($username) . " instead of a string");
Run Code Online (Sandbox Code Playgroud)
否则,你可以使用
$this->assertInternalType('string', $username, "Got a " . gettype($username) . " instead of a string");
Run Code Online (Sandbox Code Playgroud)
从 2018 年起,assertInternalType()和assertNotInternalType()被取消。
使用以下选项代替:
assertIsArray()
assertIsBool()
assertIsFloat()
assertIsInt()
assertIsNumeric()
assertIsObject()
assertIsResource()
assertIsString()
assertIsScalar()
assertIsCallable()
assertIsIterable()
assertIsNotArray()
assertIsNotBool()
assertIsNotFloat()
assertIsNotInt()
assertIsNotNumeric()
assertIsNotObject()
assertIsNotResource()
assertIsNotString()
assertIsNotScalar()
assertIsNotCallable()
assertIsNotIterable()
| 归档时间: |
|
| 查看次数: |
14420 次 |
| 最近记录: |