为什么doctest没有检测到我的测试?

时间:2013-07-13 14:30:28

标签: python doctest

class Test:
    def __init__(self):
        pass

    '''
    This function parses a name according to following rules - 

    >>> Test().name('Kshitiz Sharma')
    Sharma, Kshitiz

    '''
    def name(self, name):
        name = name.split()
        return name[1] + ", " + name[0]

if __name__ == "__main__":
    import doctest
    doctest.testmod()

输出是 -

4 items had no tests:
    __main__
    __main__.Test
    __main__.Test.__init__
    __main__.Test.name
0 tests in 4 items.
0 passed and 0 failed.
Test passed.

我的考试在哪里?

1 个答案:

答案 0 :(得分:3)

将docstring放在方法中。

def name(self, name):
    '''
    This function parses a name according to following rules - 

    >>> name('Kshitiz Sharma')
    <BLANKLINE>

    '''
    pass

另外,要正确运行方法测试,您需要在测试过程中实际实例化该类以访问该方法:

    >>> t = Test(); t.name('Kshitiz Sharma')
    <BLANKLINE>