Reputation: 1135
I have a python function that asks if user wants to use current file, or select a new one. I need to test it. Here is the function:
def file_checker(self):
file_change = raw_input('If you want to overwite existing file ?(Y/N) ')
if (file_change == 'y') or (file_change == 'Y') or (file_change == 'yes'):
logging.debug('overwriting existing file')
elif file_change.lower() == 'n' or (file_change == 'no'):
self.file_name = raw_input('enter new file name: ')
logging.debug('created a new file')
else:
raise ValueError('You must chose yes/no, exiting')
return os.path.join(self.work_dir, self.file_name)
I've donr the part when user selects yes, but I dont know how to test the 'no' part:
def test_file_checker(self):
with mock.patch('__builtin__.raw_input', return_value='yes'):
assert self.class_obj.file_checker() == self.fname
with mock.patch('__builtin__.raw_input', return_value='no'):
#what should I do here ?
Upvotes: 3
Views: 215
Reputation: 369064
According to mock
documentation:
If
side_effect
is an iterable then each call to the mock will return the next value from the iterable. The side_effect can also be any iterable object. Repeated calls to the mock will return values from the iterable (until the iterable is exhausted and a StopIteration is raised):
So the no
part can be expressed as follow:
with mock.patch('__builtin__.raw_input', side_effect=['no', 'file2']):
assert self.class_obj.file_checker() == EXPECTED_PATH
Upvotes: 2