PyCharm, Django: zero code coverage

天大地大妈咪最大 提交于 2020-01-12 13:47:42

问题


PyCharm has a "Run with Coverage" action for Django test targets. This runs the tests, but shows zero test coverage (0% files, not covered in the project pane, and all red in the editor). Checking or unchecking "Use bundled coverage.py" makes no difference.

Running the same tests from the CLI gives the expected results:

$ coverage --version
Coverage.py, version 3.5.1.  http://nedbatchelder.com/code/coverage


$ coverage run ./manage.py test blackbox
Creating test database for alias 'default'...
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s

OK
Destroying test database for alias 'default'...


$ coverage report
Name                      Stmts   Miss  Cover
---------------------------------------------
__init__                      0      0   100%
blackbox/__init__             0      0   100%
blackbox/models               5      0   100%
blackbox/rules/__init__       1      0   100%
blackbox/rules/board         62     19    69%
blackbox/tests               49      6    88%
manage                       11      4    64%
settings                     24      0   100%
---------------------------------------------
TOTAL                       152     29    81%

What could cause this?


回答1:


If you access your project via any symlink in the path, coverage display will fail.

Try to open same project through real path, and you will get correct behavior.

https://youtrack.jetbrains.com/issue/PY-17616

PS: Refreshing old question, as bug still has not been fixed.




回答2:


I had a similar issue using the PyCharm bundled coverage.py

The tests were running fine, but the coverage results were not loaded, "0%" or "not covered" everywhere.

There was an error logged in the PyCharm console though, following the output of the tests, that was coverage.py related:

/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python 
"/Applications/PyCharm 2.5 EAP.app/helpers/run_coverage.py" 
run "--omit=/Applications/PyCharm 2.5 EAP.app/helpers" bin/test

Creating test database for alias 'default'...
................................
----------------------------------------------------------------------
Ran xx tests in xxs

OK
No source for code: '/path/file.py' (<- error)

Process finished with exit code 0

My solution was to run the bundled coverage.py with the option to ignore errors: "-i".

I've edit the bundled "run_coverage.py" file, you can see its location in the console output, and add the "-i" option in the last line:

main(["xml", "-o", coverage_file + ".xml"])

to:

main(["xml", "-i", "-o", coverage_file + ".xml"])

This worked for me, the error is ignored and all the coverage data are now loaded in the UI.

If using "-i" solve the issue on your side, a better solution would be to fix the errors, but until then, you'll see coverage results.




回答3:


I've also been trying to solve this issue in Ubuntu.

At the moment I tried with both the apt-get Python and the Enthought Canopy stack, with no success. In Windows however it does work (using Canopy).

I've used the following code:

# in a.py
class A(object):

    def p(self, a):
        return a

# in test_a.py
from unittest import TestCase, main
from a import A

class TestA(TestCase):
    def test_p(self):
        inst = A()
        val = inst.p("a")
        self.assertEqual("a", val)


if _name_ == "__main__":
    main()



回答4:


I had a similar issue. I ended up generating xml data using nosetests --cover-xml, but you can also generate an xml from a previous coverage.py run with coverage xml

Then that report can be conveniently loaded in PyCharm/IDEA from the Analyze -> Show Coverage Data… -> + button and selecting the xml file.



来源:https://stackoverflow.com/questions/9841148/pycharm-django-zero-code-coverage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!