lundi 21 décembre 2015

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

I'm having some trouble mocking a functiog. Said function is imported and used in run_parsers.py and I'm getting

ImportError: 'No module named run_parsers'

When I'm attempting to mock.patch run_parsers.py.

Here's my test code in 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")

Here's my repository structure

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. This is why I'm attempting to mock the calling module rather than the module that defines get_node_paths

Aucun commentaire:

Enregistrer un commentaire