gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:58:37 25 4
gpt4 key购买 nike

<分区>

我正在研究 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()

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com