diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2018-12-04 22:49:53 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2018-12-04 23:57:01 +0900 |
commit | ab3eb1b61db816b25c5ceeb90762220798cf6da2 (patch) | |
tree | 9b10b32f558cd4a0d7950e91d9930ace52b9f286 /tests/test_ext_autodoc_importer.py | |
parent | de37ad76a10618bb930c936ed0b3d354c90fbd45 (diff) | |
download | sphinx-git-ab3eb1b61db816b25c5ceeb90762220798cf6da2.tar.gz |
Add testcases for mock()
Diffstat (limited to 'tests/test_ext_autodoc_importer.py')
-rw-r--r-- | tests/test_ext_autodoc_importer.py | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/tests/test_ext_autodoc_importer.py b/tests/test_ext_autodoc_importer.py index fe0c9f2bc..1f66298ca 100644 --- a/tests/test_ext_autodoc_importer.py +++ b/tests/test_ext_autodoc_importer.py @@ -9,7 +9,11 @@ :license: BSD, see LICENSE for details. """ -from sphinx.ext.autodoc.importer import _MockObject +import sys + +import pytest + +from sphinx.ext.autodoc.importer import _MockModule, _MockObject, mock def test_MockObject(): @@ -29,3 +33,31 @@ def test_MockObject(): assert isinstance(obj, SubClass) assert obj.method() == "string" assert isinstance(obj.other_method(), SubClass) + + +def test_mock(): + modname = 'sphinx.unknown' + submodule = modname + '.submodule' + assert modname not in sys.modules + with pytest.raises(ImportError): + __import__(modname) + + with mock([modname]): + __import__(modname) + assert modname in sys.modules + assert isinstance(sys.modules[modname], _MockModule) + + # submodules are also mocked + __import__(submodule) + assert submodule in sys.modules + assert isinstance(sys.modules[submodule], _MockModule) + + assert modname not in sys.modules + with pytest.raises(ImportError): + __import__(modname) + + +def test_mock_does_not_follow_upper_modules(): + with mock(['sphinx.unknown.module']): + with pytest.raises(ImportError): + __import__('sphinx.unknown') |