[TIP] pytest: mocking module imports
Vinicius Kwiecien Ruoso
vinicius.ruoso at gmail.com
Fri Apr 17 10:35:17 PDT 2015
I've been using pytest and it is great. There is just one thing that
I'm not satisfied with the way I've implemented, that is module level
My scenario is as follows: libA and libB where libA depends on libB
(uses class definition as superclass). Let's assume this code:
from libB import ClassY
Those modules live on different packages/repositories. LibB can be
unit tested just fine, but we need to mock libB module to allow libA
to be unit tested (libB won't be available in the moment of the test).
In a conftest.py file in the tests directory of libA, I've managed to
mock the module import by mocking sys.modules dictionary, but I did
this at the module (global) scope. I was not able to do it with any
hook (like pytest_configure), because the tests files are imported
before the hook is called. So a test_libA.py file with "import libA"
would cause the test to fail.
Am I doing something wrong or is this really the way to go?
More information about the testing-in-python