79705471

Date: 2025-07-17 22:23:58
Score: 2
Natty:
Report link

I had the same issue with unit-test, testing a method that returns a dict containing floats.

assertAlmostEqual did almost what I needed and I made it work it by comparing the values of my returned dict with the values of the expected dict using a generator comprehension:

import unittest


class TestAlmostEqual(unittest.TestCase):

    def method_to_be_tested(self):
        return {"A":1.035, "B":3.074, "C":5.777}

    def test_almost_equal(self):
        result = self.method_to_be_tested().values()
        expected = {"A":1.030, "B":3.073, "C":5.779}.values()
        generator = (value for value in expected)

        for val in result:
            self.assertAlmostEqual(val, next(generator), places=2)

Should work from python 3.6+ since dictionaries became ordered.

I'm relatively new to python so please tell me if I'm mistaken or messing something up.

Reasons:
  • Blacklisted phrase (0.5): I need
  • Whitelisted phrase (-1): I had the same
  • RegEx Blacklisted phrase (2.5): please tell me
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: user31071242