On 24 Jan 2024, at 17:11, Kerrick Staley via Python-list <python-list@python.org> wrote:
I think we should define a unittest.mock.NAN constant that can be used with Mock.assert_called_with() to assert that an argument passed to a Mock was NaN. NaNs are special in that math.nan != math.nan, so you can't just do assert_called_with(math.nan). The naming is meant to parallel unittest.mock.ANY.
Here is a reference implementation:
class _EqNaN:
def __eq__(self, other):
return math.isnan(other)
NAN = _EqNaN()
The alternative is that users can just define this EqNaN class themselves
as needed in test code. I encountered the need to test for a NaN argument today and was surprised to find that (as far as I can tell) there is no pre-built solution to this in unittest or pytest. It feels like it should
be included in some standard library.
- Kerrick
--
https://mail.python.org/mailman/listinfo/python-list
NaNs are special in that math.nan != math.nan, so you can't just do... def method(self):
assert_called_with(math.nan).
class ProductionClass:
real = ProductionClass()
real.something = MagicMock()
real.method()
real.something.assert_called_once_with( math.nan )
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (3 / 13) |
Uptime: | 30:12:12 |
Calls: | 10,391 |
Calls today: | 2 |
Files: | 14,064 |
Messages: | 6,417,093 |