You should patch the check_call
method for scripts.py
module. So the target to be patched should be scripts.check_call
. For more info, see where-to-patch
E.g.
scripts.py
:
from subprocess import check_call
def foo_method():
check_call(["ls"])
print("hello")
test_scripts.py
:
import unittest
from unittest.mock import patch
from scripts import foo_method
class TestScripts(unittest.TestCase):
@patch("scripts.check_call")
def test_foo_method(self, mock_check_call):
foo_method()
mock_check_call.assert_called_once_with(['ls'])
if __name__ == '__main__':
unittest.main()
unit test result:
⚡ coverage run /Users/dulin/workspace/github.com/mrdulin/python-codelab/src/stackoverflow/65601680/test_scripts.py && coverage report -m --include='./src/**'
hello
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Name Stmts Miss Cover Missing
--------------------------------------------------------------------------
src/stackoverflow/65601680/scripts.py 4 0 100%
src/stackoverflow/65601680/test_scripts.py 10 0 100%
--------------------------------------------------------------------------
TOTAL 14 0 100%