Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54778

Re: Nosetests

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Nosetests
Date 2013-09-25 23:48 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-7E91C9.23485925092013@news.panix.com> (permalink)
References <32f7bdcb-97e5-4a1c-a2c8-ab91e40527a8@googlegroups.com>

Show all headers | View raw


In article <32f7bdcb-97e5-4a1c-a2c8-ab91e40527a8@googlegroups.com>,
 melwin9@gmail.com 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Nosetests melwin9@gmail.com - 2013-09-25 20:14 -0700
  Re: Nosetests Roy Smith <roy@panix.com> - 2013-09-25 23:48 -0400
    Re: Nosetests melwin9@gmail.com - 2013-09-26 17:55 -0700
      Re: Nosetests Roy Smith <roy@panix.com> - 2013-09-26 21:16 -0400
        Re: Nosetests melwin9@gmail.com - 2013-09-26 18:37 -0700
          Re: Nosetests melwin9@gmail.com - 2013-09-26 21:20 -0700
            Re: Nosetests Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-27 04:46 +0000
              Re: Nosetests melwin9@gmail.com - 2013-09-29 14:28 -0700
                Re: Nosetests Terry Reedy <tjreedy@udel.edu> - 2013-09-29 17:45 -0400

csiph-web