【问题标题】:My doctest is giving me failed example even though the output is correct我的 doctest 给了我失败的例子,即使输出是正确的
【发布时间】:2015-12-04 01:33:14
【问题描述】:

https://gyazo.com/672475d1961538af601bcfa2781f3ef2 如您所见,“得到”和“预期”列表是相同的,但由于其对象的顺序是随机的,因此它给了我一个 doctest 失败。我的代码无法更改,因此这种随机化是不可避免的。我如何解决这个 doctest 问题。这是不起作用的功能。

def all_followers(data_dict, followed_user):
    """ {str: dict of {str: object}}, str -> list of str

    Returns a list containing the username of all the users in data_dict 
    that are following followed_user

    >>> all_followers(process_data(open("small_data.txt")), "katieH")
    ['tomCruise']

    >>> all_followers(process_data(open("rdata.txt")), "arrington")
    ['AccordionGuy', 'vkhosla', 'bhorowitz', 'peterfenton', 'mattcohler', 'michaelcvet', 'google', 'KatieS']

    """
    # The list to be returned, is created
    followers_list = []
    for key in data_dict:
        # Every username in data_dict and their "following" list is checked to
        # see if it matches the username of the followed_user
        if followed_user in data_dict[key]["following"]:
            # If key follows followed_user, the name of key is appended to the
            # followers_list
            followers_list.append(key)


    return followers_list

【问题讨论】:

  • 请避免使用链接和图片(尤其是图片链接),而是将代码(以及由此产生的错误消息)直接粘贴到您的问题中。

标签: python doctest


【解决方案1】:

如果您不需要测试订单,只需用sorted 包装即可。

【讨论】:

  • @user3444412 您应该将此答案标记为解决方案。这样,您就可以给 eph 他/她应得的回答您的问题的功劳。
猜你喜欢
  • 1970-01-01
  • 2023-01-24
  • 2021-12-22
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2011-07-22
  • 1970-01-01
相关资源
最近更新 更多