您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python doctest中的对象重用

5b51 2022/1/14 8:20:52 python 字数 3572 阅读 501 来源 www.jb51.cc/python

我有一个像这样的样本doctest.''' This is the 'iniFileGenerator' module. >>> hintFile = './tests/unit_test_files/hint.txt' >>> f = iniFileGenerator(hintFile) >>> pri

概述

我有一个像这样的样本doctest.

"""
This is the "iniFileGenerator" module.
>>> hintFile = "./tests/unit_test_files/hint.txt"
>>> f = iniFileGenerator(hintFile)
>>> print f.hintFilePath
./tests/unit_test_files/hint.txt
"""
class iniFileGenerator:
    def __init__(self,hintFilePath):
        self.hintFilePath = hintFilePath
    def hello(self):
        """
        >>> f.hello()
        hello
        """
        print "hello"
if __name__ == "__main__":
    import doctest
    doctest.testmod()

当我执行此代码时,我收到此错误.

Failed example:
    f.hello()
Exception raised:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py",line 1254,in __run
        compileflags,1) in test.globs
      File "
  

错误是由访问测试hello()方法时无法访问的’f’引起的.

有没有办法分享之前创建的对象?没有它,人们需要在必要时始终创建对象.

def hello(self):
    """
    hintFile = "./tests/unit_test_files/hint.txt"
    >>> f = iniFileGenerator(hintFile)
    >>> f.hello()
    hello
    """
    print "hello"

正如doctest doc所说,

extraglobs gives a dict merged into the globals used to execute examples. This works like dict.update()

但我曾经在所有方法之前测试类的__doc__中的所有方法.

class MyClass(object):
    """MyClass
    >>> m = MyClass()
    >>> m.hello()
    hello
    >>> m.world()
    world
    """

    def hello(self):
        """method hello"""
        print 'hello'

    def world(self):
        """method world"""
        print 'world'

总结

以上是编程之家为你收集整理的python doctest中的对象重用全部内容,希望文章能够帮你解决python doctest中的对象重用所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶