Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: "Already imported a file that will be measured", loss of coverage #909

Closed
mgedmin opened this issue Jan 2, 2020 · 3 comments
Closed
Labels
Milestone

Comments

@mgedmin
Copy link

@mgedmin mgedmin commented Jan 2, 2020

Describe the bug

I'm measuring gtimelog's test coverage with coverage run -m gtimelog.tests. Since coverage 5.0a4 this is missing some coverage (shows 79% instead of 100%) and also produces warnings:

Coverage.py warning: Already imported a file that will be measured: /home/mg/src/gtimelog/src/gtimelog/__init__.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /home/mg/src/gtimelog/src/gtimelog/timelog.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /home/mg/src/gtimelog/src/gtimelog/settings.py (already-imported)

To Reproduce
How can we reproduce the problem? Please be specific.

  1. What version of Python are you using? 2.7.17 and 3.7.5

  2. What version of coverage.py are you using? 5.0.1 (but every version starting with 5.0a4 is affected; 5.0a3 is fine)

  3. What versions of what packages do you have installed?

    coverage==5.0.1
    coverage-python-version==0.2.0
    freezegun==0.3.12
    -e git+https://github.com/gtimelog/gtimelog@5f7858cc58925e42b2d7cbc1d872b11745c38872#egg=gtimelog
    mock==3.0.5
    python-dateutil==2.8.1
    six==1.13.0
    </details>
    
    
  4. What code are you running? Give us a specific commit of a specific repo that we can check out.
    https://github.com/gtimelog/gtimelog/tree/5f7858cc58925e42b2d7cbc1d872b11745c38872

  5. What commands did you run? make coverage for a quick version; .tox/coverage/bin/pip install coverage=$version && .tox/coverage/bin/coverage -m gtimelog.test && .tox/coverage/bin/coverage report to test a specific version of coverage.py

Expected behavior
100% coverage reported, no warnings.

@mgedmin mgedmin added the bug label Jan 2, 2020
@mgedmin

This comment has been minimized.

Copy link
Author

@mgedmin mgedmin commented Jan 2, 2020

git bisect blames commit b7e0eec

@mgedmin

This comment has been minimized.

Copy link
Author

@mgedmin mgedmin commented Jan 2, 2020

So it looks like coverage run -m gtimelog.tests imports gtimelog/__init__.py and gtimelog/tests/__init__.py before starting coverage measurement. As a workaround I can apply this patch to my (hacky) test code:

diff --git a/src/gtimelog/tests/__init__.py b/src/gtimelog/tests/__init__.py
index 02f7cc5..31f8aca 100644
--- a/src/gtimelog/tests/__init__.py
+++ b/src/gtimelog/tests/__init__.py
@@ -1,10 +1,9 @@
 """Tests for gtimelog"""
 import unittest
 
-from gtimelog.tests import test_timelog, test_settings, test_main
-
 
 def test_suite():
+    from gtimelog.tests import test_timelog, test_settings, test_main
     return unittest.TestSuite([
         test_timelog.test_suite(),
         test_settings.test_suite(),

and then I have only one wrongly reported missing code line: the __version__ assignment in gtimelog/__init__.py.

I would prefer not having to do that: after all, things worked fine with coverage 4.x (or 5.0a3).

(And anyway the real workaround would be to stop being silly with custom test running modules and start using pytest, which is on my todo list anyway.)

mgedmin added a commit to gtimelog/gtimelog that referenced this issue Jan 2, 2020
mgedmin referenced this issue Jan 2, 2020
Adjust sys.path to better emulate Python, but only if we should.
@nedbat nedbat added this to the 5.0.2 milestone Jan 2, 2020
nedbat added a commit that referenced this issue Jan 3, 2020
nedbat added a commit that referenced this issue Jan 3, 2020
@nedbat nedbat closed this in fdfc91c Jan 3, 2020
@nedbat nedbat added the fixed label Jan 3, 2020
@nedbat

This comment has been minimized.

Copy link
Owner

@nedbat nedbat commented Jan 5, 2020

This is now available in coverage 5.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.