Nosetests

2013-09-25 Thread 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nosetests

2013-09-26 Thread 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()


On Wednesday, September 25, 2013 11:48:59 PM UTC-4, Roy Smith wrote:
> In article 
> 
>   wrote:
> 
> 
> 
> > 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.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nosetests

2013-09-26 Thread melwin9
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?

On Thursday, September 26, 2013 9:16:32 PM UTC-4, Roy Smith wrote:
> In article ,
> 
>  wrote:
> 
> 
> 
> > 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.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nosetests

2013-09-26 Thread 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()


On Thursday, September 26, 2013 9:37:39 PM UTC-4,  wrote:
> 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?
> 
> 
> 
> On Thursday, September 26, 2013 9:16:32 PM UTC-4, Roy Smith wrote:
> 
> > In article ,
> 
> > 
> 
> >  wrote:
> 
> > 
> 
> > 
> 
> > 
> 
> > > 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. 

Python Unit Tests

2013-09-27 Thread melwin9
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]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nosetests

2013-09-29 Thread 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()
[/code]

On Friday, September 27, 2013 12:46:01 AM UTC-4, Steven D'Aprano wrote:
> On Thu, 26 Sep 2013 21:20:52 -0700, melwin9 wrote:
> 
> 
> 
> > I modified  the guess.py file but am unable to run it, 
> 
> 
> 
> What does that mean?
> 
> 
> 
> How do you try to run it?
> 
> 
> 
> What happens when you do?
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Unit Tests

2013-09-29 Thread melwin9
Hi Terry & Dave,

Thanks for the suggestions. I am running Python 2.7 and when I tried the code 
above it said def main(target) <- invalid syntax. I was told to use pexpect by 
my professor which is why I started to use that for the tests. As for the test 
suggestions, I will try to come up wit those tests with my current code but 
again idk how to do it in pexpect which was asked of me.

On Saturday, September 28, 2013 2:47:20 PM UTC-4, Terry Reedy wrote:
> On 9/28/2013 12:52 AM, mel wrote:
> 
> [How can I test...]
> 
> 
> 
> > 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)
> 
> 
> 
> > if __name__ == '__main__':
> 
> >  main()
> 
> 
> 
> To expand on Dave's answer, I would refactor main() as below.
> 
> 
> 
> Note 1. I add 'allowed' so you can easily change the number or let the 
> 
> user decide the difficulty. One can always guess right in at most 4 tries.
> 
> 
> 
> Note 2. I am presuming that you are using 3.x.
> 
> 
> 
> allowed = 5
> 
> 
> 
> def getguess(target, allowed):
> 
>tries = 0
> 
>while tries < allowed:
> 
>  tries += 1
> 
>  guess = int(input(request))
> 
>  if guess < target:
> 
>print(response_low)
> 
>  elif guess > target:
> 
>print(response_high)
> 
>  else:
> 
>return guess, tries
> 
> 
> 
> def main(target)
> 
>guess, tries = getguess(target, allowed)
> 
>if guess == number:
> 
>  print(responseCorrect + '! You guessed my number in ' + tries + ' 
> 
> guesses!')
> 
>else:
> 
>  print(goodbye + ' The number I was thinking of was ' + number)
> 
> 
> 
> if __name__ == '__main__':
> 
>main(random.randint(1, 10))
> 
> 
> 
> To test a function, you must be able to control inputs and access 
> 
> outputs. Unfortunately, this makes testing simple beginner programs that 
> 
> turn user input and random numbers input into screen output harder, in a 
> 
> way, than testing complicated math functions, such as one that 
> 
> approximates the derivative of a function as a point.
> 
> 
> 
> One way to control user input for getguess is to put something like the 
> 
> following (untested) in your test module. (Ignore print for the moment.)
> 
> 
> 
> class IntInput:
> 
>"Replace input() that should return int as string."
> 
>def __init__(self, ints, print=None)
> 
>  "Ints must be a sequence of ints"
> 
>  self.i = -1  # so 0 after first increment
> 
>  self.ints = ints
> 
>  self.print = print
> 
>def input(prompt):
> 
>  "Maybe save prompt, return str(int)."
> 
>  if self.print:
> 
>self.print(prompt)
> 
>  i = self.i + 1
> 
>  self.i = i
> 
>  return str(self.ints[i])
> 
> 
> 
> In test methods, inject a mock input into the tested module with 
> 
> something like
> 
>  g.input = IntInput((5,3,2,1)).input
> 
> where the sequence passed is appropriate for the target and the response 
> 
> you want. This will be sufficient to test most of the operation of getguess.
> 
> 
> 
> (I am aware that some would say that IntInput should be a context 
> 
> manager with an exit method that restores g.input. I do not think that 
> 
> this complication is needed for this post.)
> 
> 
> 
> To test the getguess prompts and main output, collect output lines with 
> 
> something like
> 
> 
> 
> class Screen:
> 
>def __init__(self):
> 
>  self.lines = []
> 
>def print(self, line):
> 
>  self.lines.append(line)
> 
> 
> 
>screen = Screen()
> 
>g.input = IntInput((5,3,2,1), screen.print).input
> 
># Test that screen.lines is as it should be.
> 
># Be careful that actual and expected both have
> 
># or both do not have terminal \n.
> 
> 
> 
> For testing main, in test_xxx methods,
> 
>  screen = Screen
> 
>  g.print = screen.print
> 
>  # test screen.lines in
> 
> 
> 
> Another approach is to replace sys.stdin/out as

Re: Python Unit Tests

2013-09-29 Thread melwin9
Hi Dave,

Yeah I found the silly mistake lol thanks for that. making progress.


Guess a number: 5
That's too high.
Guess a number: 4
That's too high.
Guess a number: 3
Traceback (most recent call last):
  File "guess.py", line 34, in 
main(random.randint(1, 10)) 
  File "guess.py", line 29, in main
print(responseCorrect + '! You guessed my number in ' + tries + 'guesses!')
TypeError: cannot concatenate 'str' and 'int' objects

[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 correct!"
responseWrong = "Wrong, The correct number is "
guessTaken = "Your number of guesses were "
goodbye = ' Goodbye and thanks for playing!'

allowed = 5

def getguess(target, allowed):
   tries = 0
   while tries < allowed:
 tries += 1
 guess = int(input(request))
 if guess < target:
   print(responseLow)
 elif guess > target:
   print(responseHigh)
 else:
   return guess, tries

def main(target):
   guess, tries = getguess(target, allowed)
   if guess == target:
 print(responseCorrect + '! You guessed my number in ' + tries + 'guesses!')
   else:
 print(goodbye + ' The number I was thinking of was ' + number)

if __name__ == '__main__':
   main(random.randint(1, 10)) [/code]


On Sunday, September 29, 2013 10:55:19 PM UTC-4, Steven D'Aprano wrote:
> On Sun, 29 Sep 2013 18:46:30 -0700, melwin9 wrote:
> 
> 
> 
> > Hi Terry & Dave,
> 
> > 
> 
> > Thanks for the suggestions. I am running Python 2.7 and when I tried the
> 
> > code above it said def main(target) <- invalid syntax. 
> 
> 
> 
> 
> 
> Did you fix the invalid syntax? Until you fix it, you're blocked.
> 
> 
> 
> As a programmer, it is completely normal to occasionally make a typo or 
> 
> other trivial mistake that leads to invalid syntax. It's not worth 
> 
> mentioning. You just fix it and move on.
> 
> 
> 
> It's a bit like being a cook. The recipe says, "add one tablespoon of 
> 
> sugar", and you say "well I tried, but the spoon bangs on the lid of the 
> 
> jar." The solution is to take the lid off first. The recipe doesn't 
> 
> mention this, just like the recipe doesn't say "take the spoon out of the 
> 
> cutlery drawer". You just do it.
> 
> 
> 
> If you don't understand the syntax error you got, firstly try to compare 
> 
> the bad syntax
> 
> 
> 
> def main(target)
> 
> blah blah blah
> 
> 
> 
> 
> 
> with working functions that don't give syntax errors
> 
> 
> 
> def function(arg):
> 
> blah blah blah
> 
> 
> 
> 
> 
> Can you spot the difference? Hint: the syntax error will show an arrow ^ 
> 
> pointing at the spot where it notices a problem. Can you fix the problem? 
> 
> If so, great, move on! If not, ask for help, but remember to COPY AND 
> 
> PASTE the entire traceback, starting with the line
> 
> 
> 
> Traceback (most recent call last)
> 
> 
> 
> all the way to the end of the error message.
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Unit Tests

2013-09-30 Thread melwin9
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 
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...@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]

-- 
https://mail.python.org/mailman/listinfo/python-list