您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertEqualsWithDelta
发布时间:2021-10-02 17:41:49编辑:雪饮阅读()
assertEqualsWithDelta断言呢主要是断言预期值与实际值的差值的绝对值是否大于某个值,若大于这某个值,则断言失败,否则成功。
EqualsWithDeltaTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class EqualsWithDeltaTest extends TestCase
{
public function testFailure()
{
$this->assertEqualsWithDelta(1.0, 1.5, 0.1);
}
public function testSuccess()
{
$this->assertEqualsWithDelta(1.0, 1.5, 0.5);
}
public function testThree()
{
$this->assertEqualsWithDelta(1.0, 1.5, 2);
}
}
use PHPUnit\Framework\TestCase;
final class EqualsWithDeltaTest extends TestCase
{
public function testFailure()
{
$this->assertEqualsWithDelta(1.0, 1.5, 0.1);
}
public function testSuccess()
{
$this->assertEqualsWithDelta(1.0, 1.5, 0.5);
}
public function testThree()
{
$this->assertEqualsWithDelta(1.0, 1.5, 2);
}
}
运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\EqualsWithDeltaTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F.. 3 / 3 (100%)
Time: 00:00.010, Memory: 4.00 MB
There was 1 failure:
1) EqualsWithDeltaTest::testFailure
Failed asserting that 1.5 matches expected 1.0.
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\EqualsWithDeltaTest.php:8
FAILURES!
Tests: 3, Assertions: 3, Failures: 1.
关键字词:phpunit,assertEqualsWithDelta