简体   繁体   中英

My doctest is giving me failed example even though the output is correct

https://gyazo.com/672475d1961538af601bcfa2781f3ef2 As you can see, the "Got" and "Expected" list is the same, but as the order of its objects are randomized, its giving me a doctest failure. My code cannot be changed, so this randomization is inevitable. How do i fix this doctest problem. Here's the function that is not working.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM