[TIP] Running PyTest with coverage from PyCharm

Tony Cappellini cappy2112 at gmail.com
Thu Aug 29 14:40:24 PDT 2019


I've managed to get Pytest to run my test file.

I then thought I would run Pytest with Coverage from within Pycharm
(because I couldn't find the information on how to run pytest & coverage at
the same time, from the command line. The docs on this are somewhat
vague.).

Within Pycharm, you can tell Pytest exactly where to run, if you want to
focus on a specific test or test directory. However, there doesn't appear
to be an option to tell Pycharm to run coverage to run from the same
directory as the test file.

The file being tested is fairly short (about 20 lines). The test file I've
written is also fairly short, about 20-25 lines across 3 test functions.
I'm expecting that my tests are covering a fairly high percentage of the
file being tested. Running coverage from Pycharm shows this: 6% files, 84%
lines covered.

The 6% comes to me as a surprise since I'm only testing 1 file with 1 test
file.
Looking into the coverage docs, it looks like that I can tell coverage
where to un, include, and exclude using .coveragerc. So I've added a
specific path to the directory where my test file is. Nothing changes in
the coverage results.

I then tried running coverage from the command line, and am seeing an
entirely different set of coverage statistics.

----------- coverage: platform win32, python 3.6.6-final-0 -----------
Name                                Stmts   Miss  Cover
-------------------------------------------------------
unit_tests\logfile.py                  37      7    81%
unit_tests\never_stops_running.py       7      7     0%
unit_tests\test_popen.py               29      0   100%
-------------------------------------------------------
TOTAL                                  73     14    81%


*My questions:*
1. What needs to change so that I get the same results when running
Coverage from the command line and within Pycharm?
2. When running Coverage from Pycharm is there a way to tell coverage to
show me which lines are not being tested?
3. Does anyone else feel that PyCharm should have a dialog that allows the
user to point it to which code to measure coverage on? The Run/Debug dialog
gives the user control over where to run Pytest, so why not coverage too?

thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20190829/67bc69a3/attachment.htm>


More information about the testing-in-python mailing list