Как я могу имитировать ошибку импорта модуля foo в модульном тесте?
# file: bla.py
try:
import foo
except ImportError:
print("error")
sys.exit(1)
Базовый уровень для модульного теста:
import bla
import unittest
class MyTest(unittest.TestCase):
# what to mock (or so) here in order to raise the 'ImportError' that then raises the `sys.exit(1)`
def test_1(self):
with self.assertRaises(SystemExit) as err:
importlib.reload(bla) # force reload, but fails as 'foo' is not importable
self.assertEqual(rt.exception.code,1)
Подробнее здесь: https://stackoverflow.com/questions/792 ... at-testime
Мобильная версия