Python Unit Tests

melwin9 at gmail.com melwin9 at gmail.com
Mon Sep 30 15:54:08 EDT 2013


Lol, im starting to get the hang out of, onto the next hurdle, i looked up the error and it says the data is none?

Traceback (most recent call last):
  File "guess.py", line 34, in <module>
    main(random.randint(1, 10)) 
  File "guess.py", line 27, in main
    guess, tries = getguess(target, allowed)
TypeError: 'NoneType' object is not iterable


On Saturday, September 28, 2013 12:52:26 AM UTC-4, mel... at gmail.com wrote:
> 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]




More information about the Python-list mailing list