doctest什么都没有

时间:2014-07-28 08:43:30

标签: python doctest

我对这个doctest问题感到困惑。

from collections import namedtuple
Match = namedtuple('Match', ['token_string', 'normalised_token',
                     'brand_name', 'brand_id',
                     'score'])

def make_match(tokens, normalised, brand, score):
    """
    Examples:
    >>> make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==Match('Jack Jones','JackJones','Jack Jones','X023',0.6)
    >>> True 
    >>> make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==('Jack Jones','JackJones','Jack Jones','X023',0.6)
    >>> True
    """
    return Match(token_string=tokens,
         normalised_token=normalised,
         brand_name=brand[0],
         brand_id=brand[1],
         score=score)

然后我得到了这样的测试失败:

Failed example:
make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==Match('Jack Jones','JackJones','Jack Jones','X023',0.6)
Expected nothing
Got:
True
**********************************************************************
File "utilization.py", line 15, in utilization.make_match
Failed example:
True 
Expected nothing
Got:
True
**********************************************************************
1 items had failures:
4 of   4 in utilization.make_match
***Test Failed*** 4 failures.

这很奇怪。我不应该已经宣布期待" True" ?

非常感谢

1 个答案:

答案 0 :(得分:3)

您的测试用例格式不正确,它与您在解释器中看到的格式不匹配。例如,而不是:

>>> make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==Match('Jack Jones','JackJones','Jack Jones','X023',0.6)
>>> True 

你应该:

>>> make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==Match('Jack Jones','JackJones','Jack Jones','X023',0.6)
True 

请注意,输出中不存在>>>。这也是为什么你看到四个测试而不是你期望的两个测试的原因; True被解释为新测试,而不是之前测试的预期结果。