4

I have written a python module that depends on a third-party package that isn't available on my CI testing machine, so I can't test my module remotely because I can't get past the import dependency statement.

If we assume there's no way I can just manually install the dependency (seems like a pain) on the CI host, what's the easiest way to fake/mock/whatever the missing third-party package so that I can test my code?

I'm only using a single class provided by the dependency, so I would happily just mock that object, if there's a way to just do that, instead of the whole module.

Duncan Macleod
  • 1,045
  • 11
  • 21

1 Answers1

6

You can always create a .py file in the same folder with the filename equivalent to the module name so it will be imported instead of the dependency.

If you want to use it only as a fallback solution when the dependency isn't available:

try:
    import dependency
except ImportError:
    import fakedependency as dependency

If your setup is more complex and the module is used in other modules as a dependency, mock it:

import sys, fakedependency
sys.modules['dependency'] = fakedependency

You can combine this with the fallback solution if you want.

user
  • 23,260
  • 9
  • 113
  • 101