Home > database >  How to skip imports that are not needed while unittesting python
How to skip imports that are not needed while unittesting python

Time:12-13

Im trying to unittests methods in fileA.py. fileA.py contains imports to firebasehandler.py where Im setting a connection to firebase. The methods I'm trying to test have no relation or need at all with anything from firebasehandler.py, but when running the tests I don't want to go through the credentials checking phase. What can I do to skip that import when running the unittests?

CodePudding user response:

I recommend taking a look at unittest mocking:

https://docs.python.org/3/library/unittest.mock.html

You could for example do this in your test code:

from unittest.mock import MagicMock
sys.modules['firebasehandler'] = MagicMock()
import fileA

This should prevent loading of the actual firebasehandler module AND you can see if the mocked version was accessed etc if you wish.

CodePudding user response:

I guess you can mock the imported object or method from fileA.py in your UT.

  • Related