Test importing a module executes a statement in pytest

时间:2019-04-08 14:02:12

标签: python mocking pytest

I want to test that importing a module will create a class instance. The code should look like that:

mymodule.py

class Test:
    pass

Test()

But in pytest it seems impossible:

test.py

def test(mocker):
    mocker.patch("mymodule.Test.__init__")
    import mymodule
    mymodule.Test.__init__.assert_called()

This doesn't work and I kind of get why. I patch before importing, then import overrides the patched version. But is there a way I can test it?

Don't ask me why... :) it's a 3rd party library that allows you to write your own plugins. Plugins are just modules that are imported, object creation must happen on import. Then, in __init__ I do some initialization for this 3rd party lib.

0 个答案:

没有答案