PHPUnit assertLessThanOrEqual()函数
assertLessThanOrEqual()函数是 PHPUnit 中的内置函数,用于断言实际获取的值是否小于或等于期望值。如果实际值小于或等于预期值,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。
句法:
assertLessThanOrEqual(mixed $expected, mixed $actual
[, string $message = ''])
参数:此函数接受三个参数,如上所述,如下所述:
- $expected:此参数是表示预期数据的任何类型的数值。
- $actual:此参数是代表实际数据的任何类型的数值。
- $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。
下面的例子说明了 PHPUnit 中的 assertLessThanOrEqual()函数:
示例 1:
PHP
assertLessThanOrEqual(
$expected,
$actual,
"actual value is Less than or equal to expected"
);
}
public function testNegativeForassertLessThanOrEqualWithLessVal()
{
$expected = 22;
$actual = 220;
// Assert function to test whether expected
// value is less than actual or not
$this->assertLessThanOrEqual(
$expected,
$actual,
"actual value is Less than or equal to expected"
);
}
}
?>
PHP
assertLessThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
public function testPositiveForassertLessThanOrEqualWithLessVal()
{
$expected = 22;
$actual = 22;
// Assert function to test whether expected
// value is less than actual or not
$this->assertLessThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
}
?>
输出:
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
FF 2 / 2 (100%)
Time: 91 ms, Memory: 10.00 MB
There were 2 failures:
1) GeeksPhpunitTestCase::testNegativeForassertLessThanOrEqualWithEqualVal
actual value is Less than or equal to expected
Failed asserting that 420 is equal to 22 or is less than 22.
/home/lovely/Documents/php/test.php:15
2) GeeksPhpunitTestCase::testNegativeForassertLessThanOrEqualWithLessVal
actual value is Less than or equal to expected
Failed asserting that 220 is equal to 22 or is less than 22.
/home/lovely/Documents/php/test.php:32
FAILURES!
Tests: 2, Assertions: 4, Failures: 2.
示例 2:
PHP
assertLessThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
public function testPositiveForassertLessThanOrEqualWithLessVal()
{
$expected = 22;
$actual = 22;
// Assert function to test whether expected
// value is less than actual or not
$this->assertLessThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
}
?>
输出:
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 87 ms, Memory: 10.00 MB
OK (2 tests, 4 assertions)
lovely@lovely:~/Documents/php$ ./phpunit test.php
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 87 ms, Memory: 10.00 MB
OK (2 tests, 4 assertions)
参考: https://phpunit.readthedocs.io/en/9.2/assertions.html#assertlessthanorequal