jeudi 5 mars 2020

Is it possible to mock a library?

After dozens of research on the subject and a lot of thinking, I leave it to you in this new question: Is it possible to mock an entire library with Python? I would like the import of this library and all its packages / modules / etc to be done without having to define each element by hand, with mock and sys.module ... :(

In my case, I use a library specific to the job and I would like to be able to work on my code at home, without having to recode my imports, on code which is not dependent on this library.

Example:

"""Main file.

I define the mock here.
"""

mocked = MagicLibraryMock("mylib")  # the dream
"""File with lib imports.

I can import anything and use it as a mock.
"""

import mylib
from mylib.a import b
from mylib.z import c
from mylib.a.e.r import x

foo = x()
bar = c.a.e.r.t.d()
bar.side_effect = [1, 2, 3]
bar()

Aucun commentaire:

Enregistrer un commentaire