Coverage not seeing modules imported with importlib

274 views
Skip to first unread message

robert tingirica

unread,
Sep 15, 2014, 8:30:59 AM9/15/14
to nose-...@googlegroups.com
Hello,
I am working on a python 2.7 and python 3.4 cross-platform project (windows/linux) that has some os specific dependencies.
To make the tests run on both platforms I am using mock.patch on sys.modules like so:

def setUp(self):
  self._win32com_mock = mock.MagicMock()
 self._module_patcher = mock.patch.dict('sys.modules',
{'win32com': self._win32com_mock}
self._module_patcher.start()

module_name
= importlib.
import_module('<path to module>')

           

def tearDown(self):
   
self._module_patcher.stop()


Now whenever I run "nosetests --with-coverage --cover-package=<package name>" the modules that use this kind of imports are not appearing in the coverage

Thank you,
Robert Tingirica

Ned Batchelder

unread,
Sep 16, 2014, 6:59:00 AM9/16/14
to nose-...@googlegroups.com
I have not heard of this problem before, but I have also never seen this style of mocking before.  Have you tried using the more conventional style of mocking the name "win32com" in the modules that import it?

--Ned.

Thank you,
Robert Tingirica
--
You received this message because you are subscribed to the Google Groups "nose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nose-users+...@googlegroups.com.
To post to this group, send email to nose-...@googlegroups.com.
Visit this group at http://groups.google.com/group/nose-users.
For more options, visit https://groups.google.com/d/optout.

robert tingirica

unread,
Sep 16, 2014, 7:43:39 AM9/16/14
to nose-...@googlegroups.com

Hello Ned,
Thank you for replying. The module that imports the said non-existing module, is the one that I import using "importlib", the one with the code that I have to test, not a test file. I cannot use mock it there.
It looks something like this:
<module that needs testing> imports <win32com>
<test module that has the mocking i posted> imports <module that needs testing>

sys.modules needs to be patched and not win32com directly, since it does not exist on Linux.
 
Thank you,
Robert Tingirica

Ned Batchelder

unread,
Sep 16, 2014, 9:16:43 AM9/16/14
to nose-...@googlegroups.com

On 9/16/14 7:43 AM, robert tingirica wrote:

Hello Ned,
Thank you for replying. The module that imports the said non-existing module, is the one that I import using "importlib", the one with the code that I have to test, not a test file. I cannot use mock it there.
It looks something like this:
<module that needs testing> imports <win32com>
<test module that has the mocking i posted> imports <module that needs testing>

sys.modules needs to be patched and not win32com directly, since it does not exist on Linux.

I see.  Can you show how you're running coverage?  I find it best to run nose under coverage rather than to use the nose-cover plugin:

    $ coverage run nosetests.py ....

--Ned.

(BTW: top-posting makes it harder to follow the conversation, interleaved style is best.)

robert tingirica

unread,
Sep 17, 2014, 7:56:06 AM9/17/14
to nose-...@googlegroups.com
On Tue, Sep 16, 2014 at 4:16 PM, Ned Batchelder <n...@nedbatchelder.com> wrote:

On 9/16/14 7:43 AM, robert tingirica wrote:

Hello Ned,
Thank you for replying. The module that imports the said non-existing module, is the one that I import using "importlib", the one with the code that I have to test, not a test file. I cannot use mock it there.
It looks something like this:
<module that needs testing> imports <win32com>
<test module that has the mocking i posted> imports <module that needs testing>

sys.modules needs to be patched and not win32com directly, since it does not exist on Linux.

I see.  Can you show how you're running coverage?  I find it best to run nose under coverage rather than to use the nose-cover plugin:

    $ coverage run nosetests.py ....

--Ned.

(BTW: top-posting makes it harder to follow the conversation, interleaved style is best.)

I apologize,
I`m still getting used to google groups. Hope this is better.
Indeed I am running it in the project root with nosetests: "nosetests . --with-coverage --cover-package=<my project>

James Shaw

unread,
Jan 27, 2015, 2:56:42 PM1/27/15
to nose-...@googlegroups.com
I also have the same problem: coverage doesn't show code run through importlib.import_module as covered: we have a factory class that uses reflection to instantiate classes.  Tried both "coverage run --timid" and "nosetests --with-coverage".

Ned Batchelder

unread,
Jan 27, 2015, 10:01:35 PM1/27/15
to nose-...@googlegroups.com

On 1/27/15 2:56 PM, James Shaw wrote:
I also have the same problem: coverage doesn't show code run through importlib.import_module as covered: we have a factory class that uses reflection to instantiate classes.  Tried both "coverage run --timid" and "nosetests --with-coverage".

Can you provide a pointer to a reproducible test case?  I don't understand why importlib would interfere with coverage.

--Ned.

PS: As I mentioned to the original poster, interleaved style makes it much easier to follow the thread of a conversation...

James Shaw

unread,
Feb 2, 2015, 6:05:39 PM2/2/15
to nose-...@googlegroups.com
Sorry for the late reply.  After using py.test instead of nosetests the coverage is correctly reported now (more specifically hooking in coverage at the process level with sitecustomize.py and the COVERAGE_PROCESS_START env var when running py.test).  Thanks!  
Reply all
Reply to author
Forward
0 new messages