I'm using Python 2.7 to create a game. I want to use a test script to check the methods in my main file, but I'm running into a slight issue.
In the main script, I ask if the user wants to play the game, using raw_input()
. Unfortunately, this means that when I run the test script using Windows PowerShell, the console asks the user for input and I have to manually type an answer. With repeated testing, manual typing is getting tedious.
(MWE and output below: Here, the test script should generate a 'n' because it's only checking the method, and not the gameplay itself. The actual method does some calculation, prints a statement, and outputs a string.)
This brings me to my question:
Is there some way to write a test script that will automatically generate an input for raw_input()
?
Alternately, is there some other way to accept user input in the main game file that the test script can simulate?
Thoughts: While searching for an answer, I've seen some information about mock
...I haven't used that before and also mock seems to assert an outcome from a particular statement, but I just want the test file to effectively bypass that prompt. I could just remove that (y/n) prompt from the game script, but this seems like a good learning opportunity...
MWE.py (the game file)
def game_method(stuff):
"""Calculates stuff for game"""
stuff_out = 'foo'
return stuff_out
""" Check user wants to play the game """
startCheck = raw_input('Would you like to play the game? (y/n) > ')
if (startCheck.lower() == 'y'):
play = True
else:
play = False
""" Set up a game to play"""
while (play==True):
# Do stuff
stuff_out = game_method(stuff)
else:
print "\n\tGoodbye.\n\n"
MWE-test.py (the test script)
import MWE
def check_game(input):
string_out = MWE.game_method(input)
return string_out
""" Test code here """
input = 7
string_output = check_game(input)
print "===============\n%s\n===============\n\n\n" % (string_output == 'foo')
Console output:
PS C:\dir> python MWE-test.py
Would you like to play the game? (y/n) > n
Goodbye.
===True===
PS C:\dir>