当我在函数/方法中有输入时,如何测试?

时间:2019-07-08 13:16:06

标签: python python-3.x unit-testing

我正在研究OOP,并制作了一个命令行待办事项清单进行练习。我从 Menu 中使用的类测试了所有方法,但找不到解决方法来测试我的类 Menu
Menu 类中,这些方法要求用户键入输入,但我不知道如何对其进行测试。

我没有在此处发布所有代码,因为我仅将其用作问题的示例,因为我想要一个更通用的答案。

import sys

from todo.task import TaskContainer

class Menu:

    def __init__(self):
        self.tasks = TaskContainer()
        self.choices = {
                1 : self.create_new_task,
                2 : self.edit_task,
                3 : self.edit_task_description,
                4 : self.edit_task_status,
                5 : self.search_task_by_id,
                6 : self.search_task_by_word,
                7 : self.delete_task,
                8 : self.show_all_tasks,
                9 : self.show_task_description,
                10 : self.show_task_date,
                11 : self.quit}

    def display_menu(self):
        print("""
                1 : Create new task
                2 : Edit task
                3 : Edit task description
                4 : Edit task status
                5 : Search task by id
                6 : Search task by matching word
                7 : Delete task by id
                8 : Show all tasks
                9 : Show task description
                10 : Show task dates
                11 : Quit
                """)

    def run(self):
        while True:
            self.display_menu()
            choice = int(input("Enter an option: "))
            action = self.choices.get(choice)
            if action:
                action()
            else:
                print("{} is not a valid choice.".format(choice))

    def create_new_task(self):
        task = input("Task: ")
        due_date = input("What is the due date for this task? ")
        description = input("If you wish, add a description to this task:")
        self.tasks.new_task(task)

    def edit_task(self):
        id = int(input("Type the ID of the task you wish to edit: "))
        changed_task = input("New task: ")
        self.tasks.edit_task(id, changed_task)

    def edit_task_description(self):
        id = int(input("Type the id of the task you want to edit the description? "))
        description = input("Type description: ")
        self.tasks.edit_task_description(id, description)

    def edit_task_status(self):
        id = int(input("Type the ID of the task you wish to change status: "))
        status = input("Status: ")
        self.tasks.edit_task_status(id, status)

    def search_task_by_id(self):
        id = int(input("Type task ID: "))
        print(self.tasks.search(id))

    def search_task_by_word(self):
        word = input("Type the word to be matched: ")
        matches = self.tasks.search_word(word)
        [print(task) for task in matches]

    def delete_task(self):
        id = int(input("Type the ID of the task you wish to delete: "))
        self.tasks.delete_task(id)

    def show_all_tasks(self):
        self.tasks.show_all_tasks()

    def show_task_description(self):
        id = int(input("Type the ID you wish to see the description: "))
        print(self.tasks.show_description(id))

    def show_task_date(self):
        id = int(input("Type the ID you wish to see the due date: "))
        print(self.tasks.show_due_date(id))

    def quit(self):
        sys.exit(0)

if __name__ == "__main__":
    Menu().run()

1 个答案:

答案 0 :(得分:1)

在底部放置函数的名称(display__menu()),它将与程序的其余部分一起运行