python3 文档测试

来源:互联网 发布:你瞒我瞒网络链接 编辑:程序博客网 时间:2024/06/03 16:53

在python交互命令行里面,最后加上  -v  会显示的更加全面

python3 test3.py -v

class Dict(dict):    '''    Simple dict but also support access as x.y style.    >>> d1 = Dict()    >>> d1['x'] = 100    >>> d1.x    100    >>> d1.y = 200    >>> d1['y']    200    >>> d2 = Dict(a=1, b=2, c='3')    >>> d2.c    '3'    >>> d2['empty']    Traceback (most recent call last):        ...    KeyError: 'empty'    >>> d2.empty    Traceback (most recent call last):        ...    AttributeError: 'Dict' object has no attribute 'empty'    '''    def __init__(self, **kw):        super(Dict, self).__init__(**kw)    def __getattr__(self, key):        try:            return self[key]        except KeyError:            raise AttributeError(r"'Dict' object has no attribute '%s'" % key)    def __setattr__(self, key, value):        self[key] = valueif __name__=='__main__':    import doctest    doctest.testmod()

显示结果

wulimindeMacBook-Pro:Python wulimin$ python3 test3.py -vTrying:    d1 = Dict()Expecting nothingokTrying:    d1['x'] = 100Expecting nothingokTrying:    d1.xExpecting:    100okTrying:    d1.y = 200Expecting nothingokTrying:    d1['y']Expecting:    200okTrying:    d2 = Dict(a=1, b=2, c='3')Expecting nothingokTrying:    d2.cExpecting:    '3'okTrying:    d2['empty']Expecting:    Traceback (most recent call last):        ...    KeyError: 'empty'okTrying:    d2.emptyExpecting:    Traceback (most recent call last):        ...    AttributeError: 'Dict' object has no attribute 'empty'ok4 items had no tests:    __main__    __main__.Dict.__getattr__    __main__.Dict.__init__    __main__.Dict.__setattr__1 items passed all tests:   9 tests in __main__.Dict9 tests in 5 items.9 passed and 0 failed.Test passed.


0 0
原创粉丝点击