Nosetests

melwin9 at gmail.com melwin9 at gmail.com
Wed Sep 25 23:14:41 EDT 2013


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.



More information about the Python-list mailing list