i'm new on stack overflow and i'm taking an online assesment in python and the test requires that the function takes a dictionary as argument and also returns a dictionary. this is the test code which cannot be changed: def test_it_calculates_tax_for_several_people(self): income_input = {"James": 20500, "Mary": 500, "Evan": 70000} result = calculate_tax(income_input) self.assertEqual({"James": 2490.0, "Mary": 0, "Evan": 15352.5}, result, msg="Should return {} for the input {}".format( {"James": 2490.0, "Mary": 0, "Evan": 15352.5}, {"James": 20500, "Mary": 500, "Evan": 70000} ) )
my code is very long so i cant post it here but it works fine. the problem is that it returns the expected dictionary in the test but in a different order which is unsurprising given how python stores dictionaries internally. my question therefore is why the assertEqual() fails for the result. other posts suggest using collections.OrderedDict but that doesnt work either since the argument to the function is already a dict. {"James": 2490.0, "Mary": 0, "Evan": 15352.5} == {'Evan': 15352, 'James': 2490.0, 'Mary': 0} returns True which makes me wonder why the test fails.
here is the error message: test_it_calculates_tax_for_several_people Failure in line 19, in test_it_calculates_tax_for_several_people {"James": 20500, "Mary": 500, "Evan": 70000} AssertionError: Should return {'James': 2490.0, 'Evan': 15352.5, 'Mary': 0} for the input {'James': 20500, 'Evan': 70000, 'Mary': 500}
mercredi 7 septembre 2016
python unit test assertEqual() fails for dictionaries
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire