4

I have this function that 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())

This code works well, but I don't know how to mock this function.

I tried 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])

But the test never completes. When I cancel, I get:

origem = raw_input("Entre com o valor de x: ") KeyboardInterrupt

1 Answer 1

8

You should be able to mock __builtin__.raw_input using whatever facilities you normally use to mock things.

A very simple example using unittest and mock looks like this:

import unittest
import mock
import __builtin__

def test_raw_input():
    return raw_input()

class Test(unittest.TestCase):
    @mock.patch.object(__builtin__, 'raw_input')
    def test_stuff(self, mock_raw_input):
        mock_raw_input.return_value = 7
        self.assertEqual(test_raw_input(), 7)

unittest.main()
Sign up to request clarification or add additional context in comments.

2 Comments

Can you provide some examples of this?
@PythonMaster -- Added a simple example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.