魔术 8 球游戏的测试用例

Test case for the magic-8-ball game

我对 python 和一般的 TDD 很陌生,我开发了一个神奇的 8 球游戏,我想了解如何为这个程序编写测试用例。测试应确保以下内容:

下面是我的代码。我知道我应该先写测试,但就像我说的,这对我来说是一个新领域。

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()

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

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

def calc(first,second):
    return first + second

要确认获得的结果是否正确,您可以执行以下操作:

self.assertEqual(calc(5,5), 10)

您期望 10 是 5 和 5 的结果,因此如果是其他结果,则会产生错误。

我也注意到这是下周安排的 Andela 访谈问题中的一个问题。请查看为您提供的文档,以更清楚地了解如何为不同情况编写测试。