simulating raw_input() in unittest module of python 2
input, python, unit-testing
Solution
One simple way to do this is to monkey-patch `raw_input`.
For example, in your testing module (since you should split your testee and tester into separate files), you might have:
import module_being_tested
... run tests ...
Before you run your tests, you can simply do:
import module_being_tested
def mock_raw_input(s):
return 'data.txt'
module_being_tested.raw_input = mock_raw_input
... run tests ....
Now, when your testee module calls `raw_input`, it will actually be calling `mock_raw_input`, and will always get `'data.txt'` back.
Problem
I'm trying to understand how to use unittest framework of python I have a piece of code that looks like this -- ``` while True: filename = raw_input('Enter file') if os.path.exists(filename): break else: print "That file does not exist" return filename ``` Can somebody help me out in developing the unittest module to test this. I'm asking this question in order to learn how to use unittesting (i'm trying to learn TTD: Test-Driven Development) So far I've come up with this ... import unittest import os.path class TestFunctions(unittest.TestCase): ``` def setUp(self): self.prompt = 'Enter filename: ' def test_get_file(self): # TODO make sure empty filename argument requests for new filename filename = find_author.get_valid_filename(self.prompt) self.assertTrue(<EXPRESSION?>) # TODO make sure valid filename returns the "filename" # TODO make sure invalid filename prompts that file does not exit and requests new filename ``` if name == "main": unittest.main()