Python doctests中的字符串引用问题

Python doctests中的字符串引用问题,python,testing,doctest,Python,Testing,Doctest,当我在不同的Python版本(2.5和2.6)和不同的平台格式(FreeBSD和Mac OS)上运行doctest时,字符串的引用方式不同: Failed example: decode('{"created_by":"test","guid":123,"num":5.00}') Expected: {'guid': 123, 'num': Decimal("5.00"), 'created_by': 'test'} Got: {'guid': 123, 'num': D

当我在不同的Python版本(2.5和2.6)和不同的平台格式(FreeBSD和Mac OS)上运行doctest时,字符串的引用方式不同:

Failed example:
    decode('{"created_by":"test","guid":123,"num":5.00}')
Expected:
    {'guid': 123, 'num': Decimal("5.00"), 'created_by': 'test'}
Got:
    {'guid': 123, 'num': Decimal('5.00'), 'created_by': 'test'}

因此,在一个框中,repr(decimal.decimal('5.00')表示“decimal('5.00')”),在另一个框中表示“decimal('5.00')”。有没有办法在不创建更复杂的测试逻辑的情况下解决这个问题?

这实际上是因为
decimal
模块的源代码已经更改:在python 2.4和python2.5中,
decimal.decimal.\u repr\u
函数包含:

return 'Decimal("%s")' % str(self)
return "Decimal('%s')" % str(self)
而在python2.6中,它包含:

return 'Decimal("%s")' % str(self)
return "Decimal('%s')" % str(self)
因此,在这种情况下,最好的办法就是打印出结果的
str()
,并在必要时单独检查类型…

在我在Python邮件列表上找到的D之后

我现在使用这样的方法:

import sys
if sys.version_info[:2] <= (2, 5):
    # ugly monkeypatch to make doctests work. For the reasons see
    # See http://mail.python.org/pipermail/python-dev/2008-July/081420.html
    # It can go away once all our boxes run python > 2.5
    decimal.Decimal.__repr__ = lambda s: "Decimal('%s')" % str(s)
导入系统 如果系统版本信息[:2]2.5 decimal.decimal.__repr___=lambda s:“十进制(“%s”)%str(s)
谢谢您的解释。不幸的是,我还有很多字典作为返回值,所以仅仅比较str()是不起作用的。我相应地编辑了一个问题。啊,在这种情况下,你们最好看看实际的单元测试,而不仅仅是博士测试。。。当你处理像这样的复杂问题时,医生会变得太复杂