PHPUnit assertIsScalar()函数
assertIsScalar()函数是 PHPUnit 中的内置函数,用于断言标量变量是包含整数、浮点数、字符串或布尔值的变量。此函数不认为 null 是标量。如果实际值为标量,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。
句法:
assertIsScalar($actual[, $message = ''])
参数:此函数接受两个参数,如下所述:
- $actual:此参数是代表实际数据的任何类型的字符串或变量。
- $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。
下面的例子说明了 PHPUnit 中的 assertIsScalar()函数:
示例 1:
PHP
assertIsScalar(
$actualvalue,
"actual value is a scalar or not"
);
}
}
?>
PHP
assertIsScalar(
$actualvalue,
"actual value is a scalar or not"
);
}
public function testNegativeForassertIsScalar()
{
$actualvalue = 420;
// Assert function to test whether actual
// value is a integer or not
$this->assertIsScalar(
$actualvalue,
"actual value is a scalar or not"
);
}
}
?>
输出:
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 88 ms, Memory: 10.00 MB
There was 1 failure:
1) GeeksPhpunitTestCase::testNegativeForassertIsScalar
actual value is a scalar or not
Failed asserting that null is of type "scalar".
/home/lovely/Documents/php/test.php:14
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
示例 2:
PHP
assertIsScalar(
$actualvalue,
"actual value is a scalar or not"
);
}
public function testNegativeForassertIsScalar()
{
$actualvalue = 420;
// Assert function to test whether actual
// value is a integer or not
$this->assertIsScalar(
$actualvalue,
"actual value is a scalar or not"
);
}
}
?>
输出:
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 90 ms, Memory: 10.00 MB
OK (2 tests, 2 assertions)
参考: https://phpunit.readthedocs.io/en/9.2/assertions.html#assertisscalar