魔术8球游戏的测试用例

时间:2018-09-15 08:34:35

标签: python python-3.x tdd

我一般都喜欢python和TDD,我已经开发了一个神奇的8球游戏,我想学习如何编写此程序的测试用例。该测试应确保满足以下条件:

  • 允许用户输入他们的问题
  • 显示正在进行中的消息
  • 创建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()

1 个答案:

答案 0 :(得分:0)

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

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

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

要确认获得正确的结果,请执行以下操作:

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

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

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