考虑下面的演示脚本:
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import unicode_literals
def myDivi():
"""
This is a small demo that just returns the output of a divison.
>>> myDivi()
0.5
"""
return 1/2
def myUnic():
"""
This is a small demo that just returns a string.
>>> myUnic()
'abc'
"""
return 'abc'
if __name__ == "__main__":
import doctest
extraglobs = {}
doctest.testmod(extraglobs=extraglobs)doctest在Python3.5上传递,但在Python2.7.9上失败。
奇怪的是,divison测试可以工作,但是unicode测试失败了。
我见过各种各样的问题,包括以下问题
但它们都有些不同(例如它们已经过时(指Py 2.6或Py 3.0),import语句在doctest而不是全局范围内,使用pytest而不是标准doctest,切换到不同的断言等等)
尽管如此,我还是尝试了基于这些问题的各种选择,包括。
if __name__ == "__main__":
import doctest
import __future__
extraglobs = {'unicode_literals': __future__.unicode_literals}
doctest.testmod(extraglobs=extraglobs)或
def myUnic():
"""
This is a small demo that just returns a string.
>>> myUnic()
u'abc' # doctest: +ALLOW_UNICODE
"""
return 'abc'但是,它仍然不能工作,无论是在Python 2或3上,还是给出其他错误。
有没有一种方法可以让3.5+和2.7.9+都通过,而不是丑陋的黑客呢?我还使用这些文档字符串来生成文档,所以我更愿意或多或少地保持它们的原样。
发布于 2017-02-10 14:12:03
根据Martijn在Multi version support for Python doctests中的评论,我建议使用一些真实的单元测试框架进行测试。
您仍然可以使用doctest字符串,因为它们可能对文档很好。思考未来,并为Python 3编写它们。同时,为另一个单元测试框架编写单元测试。应用程序/模块的Python2版本不要依赖doctest。
发布于 2019-06-08 15:52:00
这是用纯doctest完成的工作:
if __name__ == "__main__":
import doctest, sys, logging, re
from doctest import DocTestFinder, DocTestRunner
# Support print in doctests.
L_ = logging.getLogger(":")
logging.basicConfig(level=logging.DEBUG)
pr = print = lambda *xs: L_.debug(" ".join(repr(x) for x in xs))
# Make doctest think u"" and "" is the same.
class Py23DocChecker(doctest.OutputChecker, object):
RE = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)
def remove_u(self, want, got):
if sys.version_info[0] < 3:
return (re.sub(self.RE, r'\1\2', want), re.sub(
self.RE, r'\1\2', got))
else:
return want, got
def check_output(self, want, got, optionflags):
want, got = self.remove_u(want, got)
return super(Py23DocChecker, self).check_output(
want, got, optionflags)
def output_difference(self, example, got, optionflags):
example.want, got = self.remove_u(example.want, got)
return super(Py23DocChecker, self).output_difference(
example, got, optionflags)
finder = DocTestFinder()
runner = DocTestRunner(checker=Py23DocChecker())
for test in finder.find(sys.modules.get('__main__')):
runner.run(test)
runner.summarize()我大部分是从我不记得的地方偷来的。感谢这些互联网上的无名英雄。
https://stackoverflow.com/questions/42158733
复制相似问题