Title: Unittest do not support datatime for AlmostEqual method
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.8
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: hildogjr, iritkatriel
Priority: normal Keywords:

Created on 2020-09-14 20:57 by hildogjr, last changed 2020-09-14 21:10 by hildogjr. This issue is now closed.

Messages (3)
msg376904 - (view) Author: Hildo Guillardi Júnior (hildogjr) Date: 2020-09-14 20:57
I am running a test

`self.assertAlmostEqual(date1, date2, 6)`

and getting the result:

ERROR: test_datetime_linspace (__main__.TestFunctions)
Check the linspace object output type and delta between them.
Traceback (most recent call last):
  File "/home/lab/Documents/UNICAMP/LabREI/web-visualization/tests/", line 87, in test_datetime_linspace
    self.assertAlmostEqual(time_list[-1], now, 6)
  File "/usr/lib/python3.8/unittest/", line 957, in assertAlmostEqual
    if round(diff, places) == 0:
TypeError: type datetime.timedelta doesn't define __round__ method

Despite the definition

    def assertAlmostEqual(self, first: datetime.datetime, second: datetime.datetime,
                          places: int = ..., msg: Any = ...,
                          delta: datetime.timedelta = ...) -> None: ...

on file case.pyi, the doesn't deal good with `if round(diff, places) == 0:`, with `diff` the difference between the two varaibles.
msg376907 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-14 21:04
You need to pass the delta in as a kwarg:

>>> unittest.TestCase().assertAlmostEqual(,1,5),,1,2), delta=datetime.timedelta(2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\src\cpython\lib\unittest\", line 893, in assertAlmostEqual
    raise self.failureException(msg)
AssertionError:, 1, 5) !=, 1, 2) within datetime.timedelta(days=2) delta (datetime.timedelta(days=3) difference)
>>> unittest.TestCase().assertAlmostEqual(,1,5),,1,2), delta=datetime.timedelta(3))
msg376908 - (view) Author: Hildo Guillardi Júnior (hildogjr) Date: 2020-09-14 21:10
Date User Action Args
2020-09-14 21:10:32hildogjrsetstatus: open -> closed

messages: + msg376908
stage: resolved
2020-09-14 21:04:46iritkatrielsetnosy: + iritkatriel
messages: + msg376907
2020-09-14 20:57:09hildogjrcreate