Python magic-8-ball游戏的测试用例

Python magic-8-ball游戏的测试用例,python,python-3.x,tdd,Python,Python 3.x,Tdd,我是python和TDD的新手,我已经开发了一个神奇的8球游戏,我想学习如何为这个程序编写测试用例。测试应确保以下各项: self.assertEqual(calc(5,5), 10) 允许用户输入他们的问题 显示正在进行的消息 创建10/20个响应并显示随机响应 允许用户提出其他问题/建议或退出游戏 下面是我的代码。我知道我应该先写测试,但就像我说的,这对我来说是一个新领域 RESPONSES = ("It is certain", "It is decidedly so", "Wit

我是python和TDD的新手,我已经开发了一个神奇的8球游戏,我想学习如何为这个程序编写测试用例。测试应确保以下各项:

self.assertEqual(calc(5,5), 10)
  • 允许用户输入他们的问题
  • 显示正在进行的消息
  • 创建10/20个响应并显示随机响应
  • 允许用户提出其他问题/建议或退出游戏
下面是我的代码。我知道我应该先写测试,但就像我说的,这对我来说是一个新领域

RESPONSES =  ("It is certain", "It is decidedly so", "Without a doubt", "Yes-definitely",
 "You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Yes", 
 "Signs point to yes", "Reply is hazy", "Ask again later", "Better not tell you now",
  "Cannot predict now", "Concentrate and ask again", "Don't count on it", 
  " My reply is no", "My sources say no", "Outlook not so good", "Very doubtful")

from time import sleep
from random import choice
class MagicBall:
   def input_question(self):
        play_again = 'yes'
        while play_again == 'yes':
            str(input('Enter your question: '))
            for i in range(3):
                print("Loading {}".format(".."*i))
                sleep(1)
            print(choice(RESPONSES))
            play_again = str(input("Would you like to ask another question? yes/no ")).lower()
            if play_again == 'no':
                print("Goodbye! Thanks for playing!")
                SystemExit()
magic = MagicBall()
magic.input_question()

编写单元测试是为了确认预期输出是从执行某些计算并返回值的任何函数或方法的一系列输入中获得的

如果您有一个计算两个数字之和的函数:

def calc(first,second):
    return first + second
要确认获得了正确的结果,请执行以下操作:

self.assertEqual(calc(5,5), 10)
您希望10是5和5的结果,因此如果是其他值,它将产生错误


我还注意到这是下周安第拉采访中的一个问题。请查看提供给您的文档,以便更清楚地了解如何针对不同情况编写测试。

我不确定您的问题是什么。如果你在做TDD,而你还没有编写测试,你是在要求我们为你编写吗?一些改进:1)在我看来,创建这样的类是滥用OOP。您不需要一个类和它的对象,只需要在python中执行一些语句。2) str(input())不是必需的,因为input本身返回str类型的对象(只需写input())。3) 而不是“加载{}”。格式(“…”*i)使用打印(“加载”、“.”*i)(打印将自动用空格分隔其参数)