Nosetests
melwin9 at gmail.com
melwin9 at gmail.com
Fri Sep 27 00:20:52 EDT 2013
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.
>
> >
>
> > 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.
More information about the Python-list
mailing list