php - 浮点测试断言 - 为什么这些 "identical"数组会失败?

标签 php phpunit

我在 PHPUnit 中使用 assertSame() 将数据库结果与预期值进行比较。结果是 float 。

PHPUnit 返回此消息(但我无法发现任何差异):

Failed asserting that Array (
    '1_1' => 11.111111111111
    '1_2' => 33.333333333333
    '1_3' => 55.555555555556
    '1_4' => 0.0
    '1_5' => null
    '1_total' => 100.0
) is identical to Array (
    '1_1' => 11.111111111111
    '1_2' => 33.333333333333
    '1_3' => 55.555555555556
    '1_4' => 0.0
    '1_5' => null
    '1_total' => 100.0
)

为什么会失败?比较浮点值数组的正确方法是什么?

最佳答案

assertEquals 对于这种情况有一个 $floating_delta 参数:

$this->assertEquals($expected_array, $actual_array, '', 0.00001);

PHPUnit docs

关于php - 浮点测试断言 - 为什么这些 "identical"数组会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23734857/

相关文章:

php - 我如何在同一页面上有多个帖子,以便我可以根据每个帖子查询同一个表?

php - 省略非默认字段时,在 MariaDb 上插入失败

php - 如何将 .html() 值从 JavaScript 传递到 CodeIgniter Controller ?

php - 使用 PHPUnit 测试 error_log

unit-testing - 如何在功能测试中模拟文件复制

php - fatfree SQL 错误处理

php - twig form_widget 值变量不起作用

Laravel 4 模型单元测试与 Codeception - 未找到类 'Eloquent'

Phpunit新手-如何获取assertTrue方法

testing - Yii2:使用 composer 安装 codeception 和 phpunit 并使用它