# Nose Tests - Unit Test - Random Number Game

Page 1 of 1

## 6 Replies - 1608 Views - Last Post: 26 September 2013 - 09:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=330125&amp;s=e1b8801f6d81b01ffd95e6ea0e288d7b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 melwin

Reputation: 0
• Posts: 26
• Joined: 19-October 06

# Nose Tests - Unit Test - Random Number Game

Posted 26 September 2013 - 06:39 PM

Hello,

I am trying to write a few tests for a random input number game but not too sure how to proceed on. I have to come up with tests but not sure how to go on from that.

I am following the Python Game from http://inventwithpyt...m/chapter4.html

Started the tests with a file test_guess.py

```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"
```

and the guess.py file with

```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!'

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

Not sure How to proceed on with writing a few more tests with if statement to test if the value is low or high. I was told to try a command line switch like optparse to pass the number but not sure how to do that either.

Somewhat of a new person with Python, any guidance or assistance would be appreciated.

Is This A Good Question/Topic? 0

## Replies To: Nose Tests - Unit Test - Random Number Game

### #2 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: Nose Tests - Unit Test - Random Number Game

Posted 26 September 2013 - 06:58 PM

I'm not sure what you're trying to do here, but there are some odd issues with this code. For example, your guess.py file just prints a bunch of stuff to the command line. Is this meant to be a game?

As for your tests, they're sort of all over the map. Particularly "test_guessTooLow" - that's not a test, it's the closest thing you have to the actual guessing game.

I would suggest you review the material and try again. Start by reading the code for the guess.py file as given in the text, and try to understand what's going on there. If you don't understand what they're doing, put your finger on a particular thing you don't understand, and ask about that. When you get a guessing game that actually responds to your input, and it seems to be correct some of the time, then we can worry about writing tests.

### #3 melwin

Reputation: 0
• Posts: 26
• Joined: 19-October 06

## Re: Nose Tests - Unit Test - Random Number Game

Posted 26 September 2013 - 09:23 PM

I went through everything and got rid of the unnecessary stuff in the guess.py but am unable to run it. Also how do I go about writing tests for this/ or what kind or tests can I write.

```import random

guessesTaken = 0

number = random.randint(1, 10)

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

### #4 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: Nose Tests - Unit Test - Random Number Game

Posted 26 September 2013 - 09:29 PM

Help me out here. What does "I am unable to run it" mean? What happens when you try?

### #5 melwin

Reputation: 0
• Posts: 26
• Joined: 19-October 06

## Re: Nose Tests - Unit Test - Random Number Game

Posted 26 September 2013 - 09:35 PM

I ran the command python guess.py and got errors below

```[email protected]:~/guess_proj\$ python guess.py
I have chosen a number from 1-10
Traceback (most recent call last):
File "guess.py", line 52, in <module>
main()
File "guess.py", line 16, in main
while guessesTaken < 5:
UnboundLocalError: local variable 'guessesTaken' referenced before assignment
```

### #6 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: Nose Tests - Unit Test - Random Number Game

Posted 26 September 2013 - 09:44 PM

Quote

UnboundLocalError: local variable 'guessesTaken' referenced before assignment

You can probably work out what this means if you try. What it means is that the local variable guessesTaken hasn't been initialized.

The easiest way to fix this is to do your initialization in the method that's using the variable. You can work out how to handle global variables and persistent fields and stuff another day.

### #7 melwin

Reputation: 0
• Posts: 26
• Joined: 19-October 06

## Re: Nose Tests - Unit Test - Random Number Game

Posted 26 September 2013 - 09:57 PM

Thanks for that, I was able to get it to work with the code re-arranged. Can I proceed on with tests and if so how do I begin doing that?
```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()
```