Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54924
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-09-27 21:52 -0700 |
| Message-ID | <bb6482de-ce4e-4dd2-845c-f71008123c03@googlegroups.com> (permalink) |
| Subject | Python Unit Tests |
| From | melwin9@gmail.com |
Hey,
How do i go about coming up/coding tests for this program below. Not sure how to even approach writing about 5 tests for it.
Initially I had this for the test but its not working well.
Test was name test_guess.py (Code Below)
[code]
from unittest import TestCase
import pexpect as pe
import guess as g
import random
random_number = random.randrange(1, 10)
correct = False
class GuessTest(TestCase):
def setUp(self):
self.intro = 'I have chosen a number from 1-10'
self.request = 'Guess a number: '
self.responseHigh = "That's too high."
self.responseLow = "That's too low."
self.responseCorrect = "That's right!"
self.goodbye = 'Goodbye and thanks for playing!'
def test_main(self):
#cannot execute main now because it will
#require user input
from guess import main
def test_guessing_hi_low_4(self):
# Conversation assuming number is 4
child = pe.spawn('python guess.py')
child.expect(self.intro,timeout=5)
child.expect(self.request,timeout=5)
child.sendline('5')
child.expect(self.responseHigh,timeout=5)
child.sendline('3')
child.expect(self.responseLow,timeout=5)
child.sendline('4')
child.expect(self.responseCorrect,timeout=5)
child.expect(self.goodbye,timeout=5)
def __init__(self):
self.number = random.randint(0,10)
HIGH = 1
LOW = 2
OK = 3
def guess(self, number):
if number > self.number:
return self.HIGH
if number < self.number:
return self.LOW
return self.OK
def test_guesstoolow(self):
while not correct:
guess = input("What could it be?")
if guess == random_number:
print "Congrats You Got It"
correct = True
elif guess > random_number:
print "To High"
elif guess < random_number:
print "To Low"
else:
print "Try Again" [/code]
Python code for game below
[code]
import random
intro = 'I have chosen a number from 1-10'
request = 'Guess a number: '
responseHigh = "That's too high."
responseLow = "That's too low."
responseCorrect = "That's right!"
goodbye = 'Goodbye and thanks for playing!'
print(intro)
def main():
guessesTaken = 0
number = random.randint(1, 10)
while guessesTaken < 5:
print(request)
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print(responseLow)
if guess > number:
print(responseHigh)
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print(responseCorrect + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print(goodbye + ' The number I was thinking of was ' + number)
##def main():
# print(intro)
# user_input = raw_input(request)
# print(responseHigh)
# print(request)
# user_input = raw_input(request)
# print(responseLow)
# user_input = raw_input(request)
# print(responseCorrect)
# print(goodbye)
if __name__ == '__main__':
main()[/code]
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Python Unit Tests melwin9@gmail.com - 2013-09-27 21:52 -0700
Re: Python Unit Tests Dave Angel <davea@davea.name> - 2013-09-28 06:11 +0000
Re: Python Unit Tests Terry Reedy <tjreedy@udel.edu> - 2013-09-28 14:47 -0400
Re: Python Unit Tests melwin9@gmail.com - 2013-09-29 18:46 -0700
Re: Python Unit Tests Steven D'Aprano <steve@pearwood.info> - 2013-09-30 02:55 +0000
Re: Python Unit Tests melwin9@gmail.com - 2013-09-29 21:19 -0700
Re: Python Unit Tests Terry Reedy <tjreedy@udel.edu> - 2013-09-30 02:08 -0400
Re: Python Unit Tests melwin9@gmail.com - 2013-09-30 12:54 -0700
Re: Python Unit Tests MRAB <python@mrabarnett.plus.com> - 2013-09-30 21:08 +0100
Re: Python Unit Tests Dave Angel <davea@davea.name> - 2013-09-30 20:20 +0000
Re: Python Unit Tests Terry Reedy <tjreedy@udel.edu> - 2013-09-30 20:06 -0400
csiph-web