How To Get Code Coverage Report In Pycharm For Python Project
Solution 1:
You could just use Coverage.py.
Just pip install coverage
and add a main method on your test.py or main.py and run it.
For example, add to test.py,
if __name__ == '__main__':
unittest.main()
And run on terminal,
coverage test.py
Solution 2:
You can set up a shell script and run it with a configuration.
For example, assuming a project structure like this...
/project
/src
main.py
/test
test_something.py
...this shell script will run all the tests in the /test
directory against the source files in the /src
directory and open a browser with the HTML results:
# contents of test_coverage.sh
coverage erase
coverage run --source=./src -m unittest discover ./test
coverage report -m
coverage html
open ./htmlcov/index.html
This script is written for Mac using unittest, but you can modify it for whatever OS and test runner you're using. Of course, you can also modify it to run whatever commands you want. For example, if you don't want to generate or open the HTML results, just remove the last two lines. If your project structure looks different, modify the --source=./src
and ./test
arguments for the coverage run
command.
This shell script should be run from the root /project
folder. Make sure import statements in your test_*.py files are relative to that folder. This can easily be overlooked in PyCharm since source directories may be added to PYTHONPATH for you.
Save the shell script as test_coverage.sh
(or whatever you want to name it) and put it in the root /project
folder. Then add a Shell Script run configuration in PyCharm with the script path to that file.
When you run the configuration in PyCharm, you should get a report in the terminal and your default browser should open the HTML report (assuming you use the commands above).
Post a Comment for "How To Get Code Coverage Report In Pycharm For Python Project"