Nosetests
Roy Smith
roy at panix.com
Wed Sep 25 23:48:59 EDT 2013
In article <32f7bdcb-97e5-4a1c-a2c8-ab91e40527a8 at googlegroups.com>,
melwin9 at 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.
More information about the Python-list
mailing list