PHPUnit assertIsBool()函数
assertIsBool()函数是 PHPUnit 中的内置函数,用于断言实际获取的值是否为 Bool。如果实际值为 Bool,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。
句法:
assertIsBool($actual[, $message = ''])
参数:该函数接受上面提到的两个参数,如下所述:
- $actualvalue:这个参数是代表实际数据的任何类型。
- $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。
下面的例子说明了 PHPUnit 中的 assertIsBool()函数:
示例 1:
PHP
assertIsBool(
$actual,
"actual value is Bool or not"
);
}
public function testNegativeForassertIsNotBoolFalse()
{
$actual = 220;
// Assert function to test whether actual
// value is (true or false) or not
$this->assertIsBool(
$actual,
"actual value is Bool or not"
);
}
}
?>
PHP
assertIsBool(
$actual,
"actual value is Bool or not"
);
}
public function testPositiveForassertIsNotBoolFalse()
{
$actual = false;
// Assert function to test whether actual
// value is (true or false) or not
$this->assertIsBool(
$actual,
"actual value is Bool or not"
);
}
}
?>
输出:
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
FF 2 / 2 (100%)
Time: 93 ms, Memory: 10.00 MB
There were 2 failures:
1) GeeksPhpunitTestCase::testNegativeForassertIsNotBooltrue
actual value is false/true
Failed asserting that 420 is of type "bool".
/home/lovely/Documents/php/test.php:14
2) GeeksPhpunitTestCase::testNegativeForassertIsNotBoolFalse
actual value is false/true
Failed asserting that 220 is of type "bool".
/home/lovely/Documents/php/test.php:30
FAILURES!
Tests: 2, Assertions: 2, Failures: 2
示例 2:
PHP
assertIsBool(
$actual,
"actual value is Bool or not"
);
}
public function testPositiveForassertIsNotBoolFalse()
{
$actual = false;
// Assert function to test whether actual
// value is (true or false) or not
$this->assertIsBool(
$actual,
"actual value is Bool or not"
);
}
}
?>
输出:
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 88 ms, Memory: 10.00 MB
OK (2 tests, 2 assertions)
参考: https://phpunit.readthedocs.io/en/9.2/assertions.html#assertisbool