Nosetests


M

melwin9

Hello,

I am trying to write a few tests for a random input number game but not too sure how to proceed on.

I am following the Python Game from http://inventwithpython.com/chapter4.html

Started the tests with a file test_guess.py

from unittest import TestCase
import pexpect as pe

import guess as g

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 test_guessing_low_hi_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('3')
child.expect(self.responseLow,timeout=5)
child.sendline('5')
child.expect(self.responseHigh,timeout=5)
child.sendline('4')
child.expect(self.responseCorrect,timeout=5)
child.expect(self.goodbye,timeout=5)

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.
 
Ad

Advertisements

R

Roy Smith

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.

One thing I would recommend is refactoring your game to keep the game
logic distinct from the I/O.

If I was designing this, I would have some sort of game engine class,
which stores all the state for a game. I imagine the first thing you
need to do is pick a random number and remember it. Then, you'll need a
function to take a guess and tell you if it's too high, too low, or
correct. Something like:

class NumberGuessingGame:
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

Now you've got something that's easy to test without all this pexpect
crud getting in the way. Then, your game application can create an
instance of NumberGuessingGame and wrap the required I/O operations
around that.

I'd also probably add some way to bypass the random number generator and
set the number you're trying to guess directly. This will make it a lot
easier to test edge cases.
 
M

melwin9

Initially I was shown pexpect, leaving that there, Can i make up 5 tests? I tried tests two different ways and no luck. What am I supposed to be writing up when I do a test and is there a particular way I can/should be referencing it back to its main file?

THis is what I have for the 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 is


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

Roy Smith

Initially I was shown pexpect, leaving that there, Can i make up 5 tests? I
tried tests two different ways and no luck. What am I supposed to be writing
up when I do a test and is there a particular way I can/should be referencing
it back to its main file?

I'm not sure I understand your question. Are you asking:

Q1: "What tests should I be writing?"

or

Q2: "Once I know what I want to test, how do I implement those tests?"

I'm guessing Q1, so that's what I'm going to base the rest of this post
on. Before you cat write a test, you have to understand what your code
is supposed to do. So, for example, let's say the specification for
your program runs something like this:

When you run the program, it will print, "I have chosen a number from
1-10", and then it will print, "Guess a number: ". It will then wait
for input. When you type an integer, it will print either, "That's too
high.", "That's too low.", or "That's right!".

Now, let's look at one of your tests:

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)

It looks pretty reasonable up to the point where you do:

child.sendline('5')
child.expect(self.responseHigh,timeout=5)

The problem is, you don't know what number it picked, so you can't
predict what response it will have to an input of 5. This goes back to
what I was saying earlier. You need some way to set the game to a known
state, so you can test its responses, in that state, to various inputs.

If you're going to stick with the pexpect interface, then maybe you need
a command line argument to override the random number generator and set
the game to a specific number. So, you can run:

$ python guess.py --test 4

and now you know the number it has picked is 4. If you send it 5, it
should tell you too high. If you send it 3, it should tell you too low.
And so on.

This is standard procedure in all kinds of testing. You need some way
to set the system being tested to a known state. Then (and only then)
can you apply various inputs and observe what outputs you get. This is
true of hardware was well. Integrated circuits often have a "test
mode", where you can set the internal state of the chip to some known
configuration before you perform a test.
 
M

melwin9

The question was more like what tests should I be writing, fine if I removethe pexpect test I tried the test_guess & test_guesstoolow and still unable to get it to work. So if i Want to ask for a number and typed a number which is at random indicated by the top of the code, how do I proceed on withmy tests?
 
M

melwin9

I modified the guess.py file but am unable to run it, how do I go about writing tests for this.

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


The question was more like what tests should I be writing, fine if I remove the pexpect test I tried the test_guess & test_guesstoolow and still unable to get it to work. So if i Want to ask for a number and typed a number which is at random indicated by the top of the code, how do I proceed on with my tests?















I'm not sure I understand your question. Are you asking:



Q1: "What tests should I be writing?"







Q2: "Once I know what I want to test, how do I implement those tests?"



I'm guessing Q1, so that's what I'm going to base the rest of this post

on. Before you cat write a test, you have to understand what your code

is supposed to do. So, for example, let's say the specification for

your program runs something like this:



When you run the program, it will print, "I have chosen a number from

1-10", and then it will print, "Guess a number: ". It will then wait

for input. When you type an integer, it will print either, "That's too

high.", "That's too low.", or "That's right!".



Now, let's look at one of your tests:



def test_guessing_hi_low_4(self):



# Conversation assuming number is 4

child = pe.spawn('python guess.py')





















It looks pretty reasonable up to the point where you do:









The problem is, you don't know what number it picked, so you can't

predict what response it will have to an input of 5. This goes back to

what I was saying earlier. You need some way to set the game to a known

state, so you can test its responses, in that state, to various inputs.



If you're going to stick with the pexpect interface, then maybe you need

a command line argument to override the random number generator and set

the game to a specific number. So, you can run:



$ python guess.py --test 4



and now you know the number it has picked is 4. If you send it 5, it

should tell you too high. If you send it 3, it should tell you too low..

And so on.



This is standard procedure in all kinds of testing. You need some way

to set the system being tested to a known state. Then (and only then)

can you apply various inputs and observe what outputs you get. This is

true of hardware was well. Integrated circuits often have a "test

mode", where you can set the internal state of the chip to some known

configuration before you perform a test.
[/QUOTE]
 
Ad

Advertisements

M

melwin9

I was actually able to fix the code and run it, now i need to run tests but idk what tests to write or how to even write it.

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

Advertisements

T

Terry Reedy

I was actually able to fix the code and run it, now i need to run tests but idk what tests to write or how to even write it.

Two of us already gave you suggestions. I gave you a detailed re-write.

[snip hard to test code]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top