PHPUnit assertGreaterThanOrEqual()函数
assertGreaterThanOrEqual()函数是 PHPUnit 中的内置函数,用于断言实际获得的值是否大于或等于期望值。如果实际值大于或等于预期值,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。
句法:
assertGreaterThanOrEqual(mixed $expected, mixed $actual
[, string $message = ''])
参数:此函数接受三个参数,如上所述,如下所述:
- $expected:此参数是表示预期数据的任何类型的数值。
- $actual:此参数是代表实际数据的任何类型的数值。
- $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。
下面的例子说明了 PHPUnit 中的 assertGreaterThanOrEqual()函数:
示例 1:
PHP
assertGreaterThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
public function testNegativeForassertGreaterThanOrEqualWithGreaterVal()
{
$expected = 22;
$actual = 10;
// Assert function to test whether actual
// value is greater than or equal to expected or not
$this->assertGreaterThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
}
?>
PHP
assertGreaterThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
public function testPositiveForassertGreaterThanOrEqualWithGreaterVal()
{
$expected = 11;
$actual = 12;
// Assert function to test whether actual
// value is greater than or equal to expected or not
$this->assertGreaterThanOrEqual(
$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: 86 ms, Memory: 10.00 MB
There were 2 failures:
1) GeeksPhpunitTestCase::testNegativeForassertGreaterThanOrEqualWithEqualVal
actual value is greater than or equal to expected
Failed asserting that 10 is equal to 22 or is greater than 22.
/home/lovely/Documents/php/test.php:15
2) GeeksPhpunitTestCase::testNegativeForassertGreaterThanOrEqualWithGreaterVal
actual value is greater than or equal to expected
Failed asserting that 11 is equal to 22 or is greater than 22.
/home/lovely/Documents/php/test.php:32
FAILURES!
Tests: 2, Assertions: 4, Failures: 2.
示例 2:
PHP
assertGreaterThanOrEqual(
$expected,
$actual,
"actual value is greater than or equal to expected"
);
}
public function testPositiveForassertGreaterThanOrEqualWithGreaterVal()
{
$expected = 11;
$actual = 12;
// Assert function to test whether actual
// value is greater than or equal to expected or not
$this->assertGreaterThanOrEqual(
$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)
参考: https://phpunit.readthedocs.io/en/9.2/assertions.html#assertgreaterthanorequal