如何使用python截断断言的db结果?

2024-09-30 01:37:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个.json响应,并根据数据库值断言它,但是在测试中断言期间,我不断得到一个错误。最好的方法是什么? 我的错误:

 db_results = data_base.fetchall()
        assert len(db_results) == 1
        db_results = db_results[0]
        response = response.json
>       assert response['dealW2Calculations']['pitiPercentage'] == db_results.PITIPercentage
E       AssertionError: assert Decimal('11.762581090909090909090909090') == Decimal('11.76')
E        +  where Decimal('11.76') = (18966, 20506, 1, Decimal('11.76'), Decimal('11.76'), Decimal('17.217126545454545'), Decimal('403000.0000'), Decimal('...02000.0000'), Decimal('107000.0000'), True, False, None, False, F`enter code here`alse, None, b'\x00\x00\x00\x00\x00\x0f\x8f\xb0', None).PITIPercentage

Tags: 方法none数据库jsonfalsedbdataresponse
1条回答
网友
1楼 · 发布于 2024-09-30 01:37:51

有两种方法确实假设a = decimal.Decimal("11.762581090909090909090909090")b = decimal.Decimal("11.76")

  1. 将值舍入到给定精度:assert round(a, 2) == round(b, 2)
  2. 如果这在unittest单元测试中使用,您可以使用^{} method
  3. 如果您使用的是Python 3.5或更高版本,请使用^{} function

相关问题 更多 >

    热门问题