Usually when I mock, I have the following type of setup
# my_script.py
import numpy as np
def my_func(x):
out = np.power(x, 2)
return out
then to test the numpy power
call in my_script
:
# test_myscript.py
import numpy as np
import unittest
import mock
from my_script import my_func
class TestMyScript(unittest.TestCase):
@mock.patch("my_script.np")
def test_my_func(self, mock_os):
"""Test that numpy.power was called"""
a = np.array([1, 2, 3])
my_func(a)
mock_os.power.assert_called_with(a, 2)
if __name__ == '__main__':
unittest.main()
This works fine.
But now if the situation changes, and say I give the numpy
module as an argument into my_func
; I don't know how to mock numpy
in this case.
How would I mock numpy
in the function below in the same way as it was mocked in test_myscript
above?
Note that numpy
will not be imported in my_script.py
but will instead be imported in a separate script that runs functions from my_script.py
.
# my_script.py
# numpy NOT imported in this script!
def my_func(x, numpy):
out = numpy.power(x, 2)
return out
Aucun commentaire:
Enregistrer un commentaire