PyCharm: Coverage in community edition?
Asked Answered
A

3

16

AFAIK the feature "test coverage" is only available in the professional version (code-coverage).

How to see code coverage of my tests with the PyCharm community version?

Archaeopteryx answered 3/9, 2019 at 7:57 Comment(0)
S
10

You can use PyCrunch plugin for this.

UI

As a bonus, tests will rerun when impacted files change.

disclosure: I'm an author of this plugin

Samhita answered 5/6, 2020 at 11:47 Comment(4)
I spent a very frustrating hour or two trying to get PyCrunch to work. I suspect it might work with the most simple of tests but I gave up when I finally managed to get it to discover a test but then the imports I needed stopped discovery again. All the tests run fine when running through PyCharm or the console. The documentation for the config.yaml is erroneous too - a hyphen prefix and a solidus suffix break things. Not a good experience. I wish I hadn't upvoted before I evaluated this properly.Verduzco
oh, and it would appear Gleb Sevruk is the author of PyCrunch. Had I known that when I read this answer I would've been a little more circumspect about it. He really should be disclosing that when he answers questions on here recommending his plugin. (github.com/gleb-sevruk/pycrunch-engine)Verduzco
Sorry for the trouble, it is hard to hear you are experiencing such frustration. Generally, it works on all my projects (except async-io tests), and code needs to be structured in more complex ways to satisfy the tool. The same thing goes with .Net alternative Ncrunch, it took a few days for me to set it up first time on a complex project. And I think this is not a proper way to give feedback about non-commercial product, without particular details. This is not helpful for me and other users. Instead, it is best to open a ticket on github or submit a PR. Disclaimer added.Samhita
Is there a way to be automatically warned, if a function is not covered by a corresponding unit test function but by the test of a parent function? #74210264Tambourin
P
21

As you have already found, test coverage feature is available only in the professional PyCharm version.

What is possible to do is using an external Python package that performs the coverage of your test suite. This package is named coverage.
You can easily install it using the following command:

pip install coverage

Then, you can use it directly via PyCharm terminal (be sure that the interpreter is the correct one).

Here a quick example:
suppose that you have a project structure like this one

- project_name
    - src
        - some_code.py
    - unittests
        - test_1.py
        - test_2.py  

In order to run all unittests folder you have to type in PyCharm terminal the following command:

coverage run --source=./unittests -m unittest discover -s unittests/ && coverage report

Note that in this example I'm starting the command from the project_name directory.

In this way, unittests will be run and also a coverage will be displayed.

Another interesting option is create a HTML report. If you are interested in doing so, use the following command:

coverage run --source=./unittests -m unittest discover -s unittests/ && coverage html

This way a new folder will be added which contains all source for the HTML report.

coverage package has a lot of options and it's possible customize it in different ways, so check documentation.

Plasmodium answered 16/9, 2019 at 15:52 Comment(1)
As a side note, unittest discover is a keyword phrase, so you shouldn't change it. The only part of the command you need to change to match your test directory is --source=./unittests and -s unittests/.Chicane
S
10

You can use PyCrunch plugin for this.

UI

As a bonus, tests will rerun when impacted files change.

disclosure: I'm an author of this plugin

Samhita answered 5/6, 2020 at 11:47 Comment(4)
I spent a very frustrating hour or two trying to get PyCrunch to work. I suspect it might work with the most simple of tests but I gave up when I finally managed to get it to discover a test but then the imports I needed stopped discovery again. All the tests run fine when running through PyCharm or the console. The documentation for the config.yaml is erroneous too - a hyphen prefix and a solidus suffix break things. Not a good experience. I wish I hadn't upvoted before I evaluated this properly.Verduzco
oh, and it would appear Gleb Sevruk is the author of PyCrunch. Had I known that when I read this answer I would've been a little more circumspect about it. He really should be disclosing that when he answers questions on here recommending his plugin. (github.com/gleb-sevruk/pycrunch-engine)Verduzco
Sorry for the trouble, it is hard to hear you are experiencing such frustration. Generally, it works on all my projects (except async-io tests), and code needs to be structured in more complex ways to satisfy the tool. The same thing goes with .Net alternative Ncrunch, it took a few days for me to set it up first time on a complex project. And I think this is not a proper way to give feedback about non-commercial product, without particular details. This is not helpful for me and other users. Instead, it is best to open a ticket on github or submit a PR. Disclaimer added.Samhita
Is there a way to be automatically warned, if a function is not covered by a corresponding unit test function but by the test of a parent function? #74210264Tambourin
M
0

first you need to install coverage

pip install coverage

then run given commands(django application)

coverage run --source='.' manage.py test --keepdb
coverage report --skip-covered -m
Mars answered 17/9, 2019 at 13:5 Comment(2)
This looks like the answer of Giordano. Why a second answer? What is different/better in your answer?Archaeopteryx
the commands are slight different like keepdb will not create the temp database every run and show the report on terminal and skip-covered will skip the files that has 100% of code coverageMars

© 2022 - 2024 — McMap. All rights reserved.