在修补导入的模块时模拟返回 ImportError

Mock returning an ImportError when patching a module that's imported

我在模拟函数时遇到了一些问题。所述函数已导入并在 run_parsers.py 中使用,我得到

ImportError: 'No module named run_parsers'

当我尝试 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")

这是我的存储库结构

control_scripts
├── __init__.py
├── README.md
├── run_all_parsers.py
├── run_parsers.py
└── tests
    ├── __init__.py
    ├── test_run_parsers.py

According to this tutorial I'm supposed to mock where the function is imported. 这就是为什么我试图模拟调用模块而不是定义 get_node_paths

的模块

我不确定这是否与您的设置完全相同,但这是一个对我有用的简单测试用例。

目录设置为:

c:\work
    \control
        __init__.py
        scripts.py
        \tests
            __inti__.py
            mytests.py

and c:\work is on sys.path

模块中 scripts.py:

def identity(x):
    return x

def do_identity(x):
    return identity(x)

在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()

所以我想在这里做的是模拟函数 'identity' ,它由函数 'do_identity' 调用。这两个函数都在 'scripts' 模块中。此测试 运行 没有错误或失败。

而且我可以从任何目录 运行 将其设为:

c:\any_directory> python c:\work\control\tests\mytests.py

对于更复杂的项目结构(或者如果你想让模拟部分更短),我想出了一个棘手的解决方案,因为我需要在逻辑和 UI 之间进行分离。

我的结构看起来像这样:

├───sourceroot
│   ├───python_pkg
│   │   ├───__init__
│   │   └───subpkg
│   │       ├───__init__
│   │       ├───logic
│   │       │   ├───lpkg1
│   │       │   │   ├───__init__
│   │       │   │   ├───file1.py
│   │       │   │   └───file2.py
│   │       │   ├───lpkg2
│   │       │   │   ├───__init__
│   │       │   │   ├───file3.py
│   │       │   │   └───file4.py
│   │       │   ├───__init__
│   │       │   └───file.py
│   │       └───ui
│   │           ├───uipkg3
│   │           │   ├───__init__
│   │           │   ├───file_ui1.py
│   │           │   └───file_ui2.py
│   │           ├───uipkg4
│   │           │   ├───__init__
│   │           │   ├───file_ui3.py
│   │           │   └───file_ui4.py
│   │           ├───__init__
│   │           └───file_ui.py
│   └───packages
│       └───some_3rd_party_packages
├───srcfiles_from_3rd_parties
└───tests
    └───unit_tests_py
        ├───__init__
        └───test.py

我不得不参考 test.py 中的 file.py 和 file1.py。 要从 test.py 文件中查看 sourceroot,我将以下内容写入 sourceroot/tests/unit_test_py/__init__

import sys
import os
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')))

初始化并将 sourceroot 添加到路径后(Python 将在其中查找)test.py class 已准备好编辑:

之前:

import mock
from sourceroot.python_pkg.subpkg.logic import file
from sourceroot.python_pkg.subpkg.logic.lpkg1.file1 import SomeClassFromFile1 as SCF1

class Test_test1(object):
    def test_first(self, mocker):
        mocker.patch('sourceroot.python_pkg.subpkg.logic.lpkg1.file1.some_function_which_SCF1_calls')
        mocker.patch('sourceroot.python_pkg.subpkg.logic.file.SomeClassInFile.some_function_which_SomeClassInFile_calls')
        SCF1.some_function_in_SCF1()
        expected_information = True
        assert SCF1.is_something() == expected_information


之后:

import mock
from sourceroot.python_pkg.subpkg.logic import file
from sourceroot.python_pkg.subpkg.logic.lpkg1.file1 import SomeClassFromFile1 as SCF1

class Test_test1(object):
    def test_first(self, mocker):
        mocker.patch.object(SCF1, 'some_function_which_SCF1_calls')
        mocker.patch.object(file.SomeClassInFile, 'some_function_which_SomeClassInFile_calls')
        SCF1.some_function_in_SCF1()
        expected_information = True
        assert SCF1.is_something() == expected_information