Python check. testing raw_input

I have a python function that asks if the user wants to use the current file or select a new one. I need to check this out. 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 don’t take part when the user selects “yes”, but I don’t know how to check 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 ? 
+3
source share
1 answer

According to the mockdocumentation :

If side_effectiterable, each layout call returns the next value from iterable. . Side_effect can also be any iterable object. Repeated calls to the layout will return values ​​from the iterable (until the iterable is exhausted, and StopIteration is raised):

, no :

with mock.patch('__builtin__.raw_input', side_effect=['no', 'file2']):
    assert self.class_obj.file_checker() == EXPECTED_PATH
+2

All Articles