class TestWithoutPandas(unittest.TestCase):

    def setUp(self):
        self._temp_pandas = None
if sys.modules.get('pandas'):
    self._temp_pandas = sys.modules['pandas']
sys.modules['pandas'] = None

def tearDown(self):
    if self._temp_pandas:
        pass
How to write unittests for an optional dependency in a python package?