簡體   English   中英

如何使用輸入對這個函數進行單元測試?

[英]How do I unit test this function with inputs?

如何使用輸入對這個函數進行單元測試? search_by_choice 是一個接受用戶輸入的變量,但我如何對其進行單元測試?

def search(books_data):
    """
    Searches and print search result as numbered list.

    A function that takes in menu_selection (an integer) as user input from menu(), run search_function() and collects
    the result from search_function(). Print user the result and return to main menu page, ie. menu().

    :precondition: the integer must be in the range [1:8]
    :postcondition: print number of results and numbered list of results as dictionaries
    :return: menu()
    """
    print(SEARCH_OPTIONS())
    search_by_choice = int(input("Please enter your choice (in number): ").strip())
    if search_by_choice == 1:
        print("You chose search by: Author")
        return 1
    elif search_by_choice == 2:
        print("You chose search by: Title")
        return 2
    elif search_by_choice == 3:
        print("You chose search by: Publisher")
        return 3
    elif search_by_choice == 4:
        print("You chose search by: Shelf")
        return 4
    elif search_by_choice == 5:
        print("You chose search by: Category")
        return 5
    elif search_by_choice == 6:
        print("You chose search by: Subject")
        return 6
    elif search_by_choice == 7:
        print("You chose to Return to previous page (Main Menu)")
        menu(load_data())
        # return 7
    elif search_by_choice == 8:
        print("You chose to Quit")
        quit_books(books_data)
    else:
        print("Error! Please enter a valid integer")
        menu(load_data())

您可以使用:

def numbers_to_strings(argument): 
     switcher = { 
        0: "zero", 
        1: "You chose search by: Author", 
        2: "You chose search by: Title", 
        3:
        .
        .
    } 
    # get() method of dictionary data type returns  
    # value of passed argument if it is present  
    # in dictionary otherwise second argument will 
    # be assigned as default value of passed argument 
    return switcher.get(argument, "nothing") 


# Driver program 
if __name__ == "__main__": 
    argument=0
    print numbers_to_strings(argument) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM