unit testing - Python unittest failure when results appear equal -
i'm running series of unit tests on rpn calculator i've put together. calculator can mix integers, floats, , picas. need able calculate using picas typesetting work i'm doing.
for reason, 1 of unit tests failing:
failure traceback (most recent call last): file "/users/andrew/developer/pyrpn/test_rpn.py", line 112, in test_pica_subtracted_from_pica self.assertequal(self.calc.rpn(['10p3', '2p1', '-']), ['8p2']) assertionerror: lists differ: ['8p2'] != ['8p2'] first differing element 0: '8p2' 8p2 ['8p2']
i can't see why. simplify took list out of equation , compared directly string:
failure expected :'8p2' actual :'8p2' <click see difference> traceback (most recent call last): file "/users/andrew/developer/pyrpn/test_rpn.py", line 112, in test_pica_subtracted_from_pica self.assertequal(self.calc.rpn(['10p3', '2p1', '-']).pop(), '8p2') assertionerror: '8p2' != '8p2'
it still fails. cannot see why '8p2' != '8p2'
. running in pycharm if click see difference, tells me there no differences, , content identical, yet test fails. fails command line well.
i've put same test in doctest:
""" >>> rpn().rpn(['10p3', '2p1', '-']) ['8p2'] """
and passes without problem.
mcve:
import re import unittest class pica(object): def __init__(self, points): self.points = points def __repr__(self): whole_points = int(self.points / 12) sub_points = self.points - (whole_points * 12) return "'%rp%r'" % (whole_points, sub_points) def __sub__(self, other): if type(other) pica: return pica(self.points - other.points) class rpn: def __init__(self): self.oper_dict = {'-': rpn.pop_two_and_sub} self.pica_pattern = re.compile("(\d+)p(\d+)") def pop_two_and_sub(self, terms): terms.append(terms.pop() - terms.pop()) def rpn(self, terms): result = [] term in terms: if term in self.oper_dict: self.oper_dict[term](self, result) elif self.pica_pattern.match(term): match = self.pica_pattern.match(term) result.append(pica(int(match.group(1)) * 12 + int(match.group(2)))) else: raise syntaxerror return result class testpica(unittest.testcase): def test_pica_subtracted_from_pica(self): self.assertcountequal(rpn().rpn(['2p1', '10p3', '-']), ['8p2']) if __name__ == '__main__': unittest.main()
when run python 3.5, following error:
failure traceback (most recent call last): file "/users/andrew/developer/pyrpn/mvce.py", line 42, in test_pica_subtracted_from_pica self.assertcountequal(rpn().rpn(['2p1', '10p3', '-']), ['8p2']) assertionerror: element counts not equal: first has 1, second has 0: '8p2' first has 0, second has 1: '8p2'
ok, found difference. python 3 has more support unicode, doesn't show encoding in effect when printing unicode string, why printed same though had different types:
<class '__main__.pica'>
<class 'str'>
in order assertion work, either smarter compare needed, or else strings need put in common format before calling assert methods.
Comments
Post a Comment