Ale*_*sen 6 python unit-testing mocking
我在模拟函数时遇到了一些麻烦。所述函数被导入并用于run_parsers.py,我得到
ImportError: 'No module named run_parsers'
Run Code Online (Sandbox Code Playgroud)
当我尝试mock.patch run_parsers.py.
这是我的测试代码 test_run_parsers.py
from .. import run_parsers # Used in all my other tests.
def test_node_data_parser_throws_exception(self):
def parser():
return NotImplementedError()
with mock.patch("run_parsers.get_node_paths") as node_paths:
node_paths.return_value = "node_1"
run_parsers.get_node_data(parser, "/a/path")
Run Code Online (Sandbox Code Playgroud)
这是我的存储库结构
control_scripts
??? __init__.py
??? README.md
??? run_all_parsers.py
??? run_parsers.py
??? tests
??? __init__.py
??? test_run_parsers.py
Run Code Online (Sandbox Code Playgroud)
根据本教程,我应该模拟导入函数的位置。这就是为什么我试图模拟调用模块而不是定义 get_node_paths 的模块
我不确定这是否完全复制了您的设置,但这里有一个对我有用的简单测试用例。
目录设置为:
c:\work
\control
__init__.py
scripts.py
\tests
__inti__.py
mytests.py
and c:\work is on sys.path
Run Code Online (Sandbox Code Playgroud)
在模块scripts.py中:
def identity(x):
return x
def do_identity(x):
return identity(x)
Run Code Online (Sandbox Code Playgroud)
在 mytests.py 中:
import unittest
from unittest.mock import patch
from control import scripts
class MyTest(unittest.TestCase):
def test_patch(self):
with patch('control.scripts.identity') as mymock:
mymock.return_value = 99
self.assertEqual(scripts.do_identity(1), 99)
def test_no_patch(self):
self.assertEqual(scripts.do_identity(1), 1)
if __name__ == "__main__":
unittest.main()
Run Code Online (Sandbox Code Playgroud)
所以我在这里尝试做的是模拟由函数“do_identity”调用的函数“identity”。这两个函数都在“脚本”模块中。此测试运行时没有错误或失败。
我可以从任何目录运行它:
c:\any_directory> python c:\work\control\tests\mytests.py
Run Code Online (Sandbox Code Playgroud)