I am using Python 2.7 to create a game. I want to use a test script to test methods in my main file, but I ran into a little problem.
In the main script, I ask if the user wants to play the game using raw_input(). Unfortunately, this means that when I run a test script using Windows PowerShell, the console asks the user for data input, and I have to manually enter the answer. With repeated testing, manual typing becomes tedious.
(MWE and the output is lower: here the test script should generate "n" because it only checks the method, not the game process itself. The actual method does some calculations, prints an instruction and prints a line.)
This leads me to my question:
Is there a way to write a test script that will automatically generate input for raw_input()? Alternatively, is there another way to accept user input in the main game file, which can simulate a test script?
Thoughts: while searching for an answer, I saw some information about mock... I have not used this before, and also scoffs, it seems to claim the result from a specific statement, but I just want the test to effectively get around this invitation. I could just remove this (y / n) invitation from the game script, but this seems like a good learning opportunity ...
MWE.py (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):
stuff_out = game_method(stuff)
else:
print "\n\tGoodbye.\n\n"
MWE-test.py (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.
PS C:\dir>