【问题标题】:When I have inputs inside functions/methods, how can I test it? [duplicate]当我在函数/方法中有输入时,我该如何测试它? [复制]
【发布时间】:2019-11-18 00:28:11
【问题描述】:

我正在学习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()

【问题讨论】:

  • 为什么不使用专用库,例如​​内置的argparseclick
  • @Adam.Er8 感谢亚当!我之前没有找到这个答案。我想这就是我需要的。
  • @olinox14 从未听说过它,现在将阅读它。谢谢

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


【解决方案1】:

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

【讨论】:

  • 我的 display_menu() 方法在 run() 方法中被调用。我已经调用 Menu().run() 来运行它。这不是我的问题的答案,我所做的工作正常。
猜你喜欢
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
相关资源
最近更新 更多