Skip to content

Modules imported with importlib don't record coverage #1002

@timofurrer

Description

@timofurrer

Describe the bug

When a module is dynamically imported with importlib it won't record any coverage data.

To Reproduce

  • Python: 3.7
  • Coverage: 5.1
  • No 3rd-party packages involved

I've created a minimal example illustrating the problem here: https://github.com/timofurrer/coverage-importlib-test
The repository consists of a single Python package with a single module src/app/core.py which uses
the importlib code from here to dynamically import src/app/plugins/plugin_mod.py.

When running the tests with tox the output shows the following:

src/app/__init__.py                 0      0      0      0   100%
src/app/core.py                    10      0      0      0   100%
src/app/plugins/__init__.py         0      0      0      0   100%
src/app/plugins/plugin_mod.py       1      1      0      0     0%   1
---------------------------------------------------------------------------
TOTAL                              11      1      0      0    91%

So no coverage for plugin_mod.py ...

Expected behavior

... However, I'd expect it to have 100% coverage as the others do.
Importing the module statically using import app.plugins.plugin_mod.py in src/app/core.py instead of with importlib works like a charm.

Additional information

The following .coveragerc configuration is used:

[run]
branch = True
source =
    app

[paths]
source =
    src
    .tox/*/site-packages

[report]
show_missing = True

Edit:

The problem seems to be related to the [run] source config - removing it, correctly reports coverage for the plugin module,
but it also includes modules not from the app packages into the coverage report - which is undesired.
Adding the app.plugins package to [run] source doesn't help.

The config also seems like a common one - e.g. attrs uses it, too

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions