Why is unittest coverage disabled for Python 3.8.3?

Viewed 2197

I have this version of Python:

3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)]

running on Windows 10 in PyCharm CE 2020.1.1. Using a very simple unittest:

# test_simple.py

from unittest import TestCase

class Simple(TestCase):
    def test_one(self):
        self.assertTrue(True)

I can run python -m coverage run -m unittest, which completes and generates a .coverage SQLite file; and I can run the test from the IDE which produces a Test Results tree; but Run / Run with Coverage is grayed out with no explanation.

I have followed the (fairly scattered) PyCharm documentation to do Settings / Build, Execution, Deployment, Activate Coverage View enabled, Show options before applying coverage to the editor and this has not helped.

Why is coverage grayed out and how can I fix it?

1 Answers

Unfortunately, IDE coverage support is not available in the Community Edition of PyCharm, as can be seen in the edition features.

You have to run coverage on the command line as usual, as shown for example in this answer.

You could also use some wrapper that creates and opens a HTML report. An example for such a wrapper that uses pytest with coverage can be found in this blog post. Here is the script used in that blog (note that I changed the coverage path to work with current versions):

import os
import subprocess
import webbrowser


def main():
    subprocess.call(['coverage', 'erase'])    
    subprocess.call(['coverage', 'run', '--module', 'pytest'])
    subprocess.call(['coverage', 'html'])
    webbrowser.open("file://" + os.getcwd() + "/htmlcov/index.html", new=2)

if __name__ == '__main__':
    main()
Related