简体   繁体   English

Python测试。 raw_input测试

[英]Python testing. raw_input testing

I have a python function that asks if user wants to use current file, or select a new one. 我有一个python函数,询问用户是否要使用当前文件,或选择一个新文件。 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 ? 

According to mock documentation : 根据mock文档

If side_effect is an iterable then each call to the mock will return the next value from the iterable. 如果side_effect是一个可迭代的,那么对mock的每次调用都将返回iterable中的下一个值。 The side_effect can also be any iterable object. side_effect也可以是任何可迭代对象。 Repeated calls to the mock will return values from the iterable (until the iterable is exhausted and a StopIteration is raised): 重复调用mock将返回iterable中的值(直到iterable耗尽并引发StopIteration):

So the no part can be expressed as follow: 所以no部分可以表达如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM