简体   繁体   English

PyCharm,Django:零代码覆盖率

[英]PyCharm, Django: zero code coverage

PyCharm has a "Run with Coverage" action for Django test targets. PyCharm为Django测试目标提供了“Run with Coverage”操作。 This runs the tests, but shows zero test coverage (0% files, not covered in the project pane, and all red in the editor). 这将运行测试,但显示零测试覆盖率(0%文件,项目窗格中未涵盖,编辑器中全部为红色)。 Checking or unchecking "Use bundled coverage.py" makes no difference. 选中或取消选中“使用捆绑的coverage.py”没有任何区别。

Running the same tests from the CLI gives the expected results: 从CLI运行相同的测试会得到预期的结果:

$ 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? 什么可能导致这个?

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 https://youtrack.jetbrains.com/issue/PY-17616

PS: Refreshing old question, as bug still has not been fixed. PS:刷新旧问题,因为bug还没有修复。

I had a similar issue using the PyCharm bundled coverage.py 我有一个类似的问题使用PyCharm捆绑的coverage.py

The tests were running fine, but the coverage results were not loaded, "0%" or "not covered" everywhere. 测试运行正常,但覆盖结果没有加载,“0%”或“未覆盖”到处都是。

There was an error logged in the PyCharm console though, following the output of the tests, that was coverage.py related: 在PyCharm控制台中记录了一个错误,但是在测试结果之后,就是coverage.py相关:

/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". 我的解决方案是运行捆绑的coverage.py,并选择忽略错误:“ - 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: 我编辑了捆绑的“run_coverage.py”文件,您可以在控制台输出中看到它的位置,并在最后一行添加“-i”选项:

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. 这对我有用,错误被忽略,所有覆盖数据现在都加载到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. 如果使用“-i”解决了您的问题,更好的解决方案是修复错误,但在此之前,您将看到覆盖结果。

I've also been trying to solve this issue in Ubuntu. 我也一直试图在Ubuntu中解决这个问题。

At the moment I tried with both the apt-get Python and the Enthought Canopy stack, with no success. 目前我尝试使用apt-get Python和Enthought Canopy堆栈,但没有成功。 In Windows however it does work (using Canopy). 在Windows中,它确实有效(使用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()

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 我最终使用nosetests --cover-xml生成xml数据,但你也可以使用coverage xml从之前的coverage.py运行生成coverage xml

Then that report can be conveniently loaded in PyCharm/IDEA from the Analyze -> Show Coverage Data… -> + button and selecting the xml file. 然后可以从Analyze - > Show Coverage Data ... - > +按钮并选择xml文件,方便地在PyCharm / IDEA中加载该报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM