Mock a raw_input

Asked

Viewed 127 times

4

I have the following functions with raw input, which should receive a list [x,y]

def input_origem():
    origem = raw_input("Entre com o valor de x: ")
    origem = eval(origem)
    return origem

def input_saida():
    destino = raw_input("Entre com o valor de y  ")
    destino = eval(destino)
    return destino



def my func(origem, destino):

..
...
code 
..
...

print  myfunc(input_origem(), input_saida())

until then beauty works correctly, but I don’t know how to mock it

I was trying this way:

class TEste(base.TestCase):
    @base.TestCase.mock.patch('mypath.input_origem')
    @base.TestCase.mock.patch('mypath.input_saida')
    def test_movimento_cavalo(self, origem_mock, saida_mock):
        origem_mock = self.mock.MagicMock()
        saida_mock = self.mock.MagickMock()
        myfunc(origem_mock, saida_mock)
        myfunc.should.be.equal([1,1])

He keeps thinking, and when I cancel he rtorna:

origin = raw_input("Enter x: ") Keyboardinterrupt

  • Tries to put raw_input in a function and return raw_input, and so does the mock of the input wrapper function.

1 answer

1


It may be too late, but if anyone ever needs it, it is possible to mock built-in functions:

>>> with mock.patch('__builtin__.raw_input', return_value='abc'):
...     print raw_input('Entre com o valor de x: ')

abc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.