我对 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()
编写单元测试以确认预期输出是从执行某些计算并返回值的任何函数或方法的输入范围中获得的。
如果你有一个函数来计算两个数字的总和:
def calc(first,second):
return first + second
要确认获得了正确的结果,您将执行以下操作:
self.assertEqual(calc(5,5), 10)
你期望 10 是 5 和 5 的结果,所以如果它是别的东西,它会产生一个错误。
我还注意到这是下周安排的 Andela 面试问题中的一个问题。请查看提供给您的文档,以更清楚地了解如何为不同情况编写测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句