Python doctest 说失败但 returns 正确值

Python doctest says failed but returns correct value

我在 PyCharm 中执行了一些文档测试,我 运行 在 python 控制台中进行了测试。它通过了一个而另一个失败了,但失败表明返回的值是正确的值。有谁知道我可能遗漏了什么,或者这是否是 PyCharm 等中的已知错误?

这是代码

def friend_date(a, b):
    """
    Returns True if they have any hobbies in common, False is not.

    >>> elmo = ('Elmo', 5, ['hugging', 'being nice'])
    >>> sauron = ('Sauron', 5000, ['killing hobbits', 'chess'])
    >>> gandalf = ('Gandalf', 10000, ['waving wands', 'chess'])

    >>> friend_date(elmo, sauron)
    False

    >>> friend_date(sauron, gandalf)
    True 
    """
    return bool(set(a[2]) & set(b[2]))

确实如@user2357112 所指出的那样,您的第二个测试用例的预期输出中有 space。我用这段代码进行了测试,它有效:

def friend_date(a, b):
    """
    Returns True if they have any hobbies in common, False is not.

    >>> elmo = ('Elmo', 5, ['hugging', 'being nice'])
    >>> sauron = ('Sauron', 5000, ['killing hobbits', 'chess'])
    >>> gandalf = ('Gandalf', 10000, ['waving wands', 'chess'])

    >>> friend_date(elmo, sauron)
    False

    >>> friend_date(sauron, gandalf)
    True
    """
    return bool(set(a[2]) & set(b[2]))