classification
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
process
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/test_interpreter.py", line 87, in test_datetime_linspace
    self.assertAlmostEqual(time_list[-1], now, 6)
  File "/usr/lib/python3.8/unittest/case.py", line 957, in assertAlmostEqual
    if round(diff, places) == 0:
TypeError: type datetime.timedelta doesn't define __round__ method


Despite the definition

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

on file case.pyi, the case.py 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:

https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertAlmostEqual

>>> unittest.TestCase().assertAlmostEqual( datetime.date(2020,1,5),  datetime.date(2020,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\case.py", line 893, in assertAlmostEqual
    raise self.failureException(msg)
AssertionError: datetime.date(2020, 1, 5) != datetime.date(2020, 1, 2) within datetime.timedelta(days=2) delta (datetime.timedelta(days=3) difference)
>>> unittest.TestCase().assertAlmostEqual( datetime.date(2020,1,5),  datetime.date(2020,1,2), delta=datetime.timedelta(3))
>>>
msg376908 - (view) Author: Hildo Guillardi Júnior (hildogjr) Date: 2020-09-14 21:10
Thanks.
History
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