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