How to mock a method called from a subprocess?

60 Views Asked by At

there is test, for example:

class TestBlaBla(TestCase):
    def test_lala(self):
         output = subprocess.check_output(some_python_script_file, shell=True)
         ...

Let's say in "some_python_script_file" there is a function "some_function" that return "string_1". This "string_1" used in other function inside "some_python_script_file".

How to mock "some_function" so that it returns "other_string"?

This method didn't work:

class TestBlaBla(TestCase):
    @mock.patch('some_python_script_file.some_function', mock.MagicMock(return_value="other_string"))
    def test_lala(self):
         output = subprocess.check_output(some_python_script_file, shell=True)
         ...
0

There are 0 best solutions below