How to use Coverage.py with PyCharm Community Edition

已回答

I'm using PyCharm Community Edition 2017.3 on Windows. I have coverage.py module installed through pip. I can run my unit tests and gather coverage by running these commands in a command window.

python -m unittest discover -s tests/
coverage run -m unittest discover -s tests/
coverage report -m

coverage html

This works fine. But since I need to run the unit tests frequently this isn't very convenient. I was wondering if there is a Run configuration I can create in PyCharm which would do this for me with a single click.

 

 

0

Hi Jithin10a! Coverage support is included in Professional Edition only: https://www.jetbrains.com/pycharm/features/editions_comparison_matrix.html

0
Avatar
Permanently deleted user

There's also a way to integrate it a bit better:

 

1. Create a batch file for whichever OS you're using. When I set this up, I happened to be on a PC so my script looked like this:

C:\program_files\anaconda3\envs\project\Scripts\coverage-3.6.exe run --rcfile=c:\_src\project\tests\.coveragerc -m unittest discover -s tests/
C:\program_files\anaconda3\envs\project\Scripts\coverage-3.6.exe report -m
C:\program_files\anaconda3\envs\project\Scripts\coverage-3.6.exe html
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" C:\_src\project\htmlcov\index.html

2. Create an External Tool

  • Ctrl-Alt-S / ⌘-, / File -> Settings
  • Tools -> External Tools
  • Click the + icon in the right pane
  • Name it whatever
  • Select the program as your newly-minted batch file. Be sure that the working directory is correct.

3. Open up/keep settings open and go to Keymap

4. Search for your external tool name in the right pane

5. Create a shortcut for it, whichever you please.

Congrats! Now you just have to hit your keyboard shortcut and it will run coverage and also open chrome to the coverage html.

1

请先登录再写评论。