testing-cabal / mock

The Python mock library

Home Page:https://docs.python.org/dev/library/unittest.mock.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to mock a exception class??

wencan opened this issue · comments

I need mock a exception class, and raise it.
my code:

class TestService(unittest.TestCase):
    def test_raise(self):
        class MyException(BaseException):
            pass
        
        mockedException = mock.create_autospec(spec=MyException, instance=True)
        self.assertIsInstance(mockedException, BaseException)
        raise mockedException

output:

TypeError: exceptions must derive from BaseException

This is not the right place to ask, please try the testing-in-python mailing list, and read the documentation for mock on python.org.