blob: c9e3f8492b6744bf1e3fcab46c0ea8372f6797d9 [file] [log] [blame]
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -07001# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import sys
16
17import mock
18import pytest
19
20
21@pytest.fixture
22def mock_non_existent_module(monkeypatch):
23 """Mocks a non-existing module in sys.modules.
24
25 Additionally mocks any non-existing modules specified in the dotted path.
26 """
27 def _mock_non_existent_module(path):
28 parts = path.split('.')
29 partial = []
30 for part in parts:
31 partial.append(part)
32 current_module = '.'.join(partial)
33 if current_module not in sys.modules:
34 monkeypatch.setitem(
35 sys.modules, current_module, mock.MagicMock())
36
37 return _mock_non_existent_module