Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python coverage - exclude packages

I'm using the python coverage tool to run my unit test. As you can see from the result, it includes all the "site-packages". How can I exclude them from the report? I only want to show the report for project source code.

(ctrp) ubuntu@ubuntu-xenial:~/transfer$ coverage run -m unittest discover
...
...
----------------------------------------------------------------------
Ran 1 test in 0.021s

FAILED (errors=1)
(ctrp) ubuntu@ubuntu-xenial:~/transfer$
(ctrp) ubuntu@ubuntu-xenial:~/transfer$
(ctrp) ubuntu@ubuntu-xenial:~/transfer$
(ctrp) ubuntu@ubuntu-xenial:~/transfer$ coverage report
Name                                                                                                   Stmts   Miss  Cover
--------------------------------------------------------------------------------------------------------------------------
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/__init__.py                              13      0   100%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/_compat.py                              403    321    20%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/_unicodefun.py                           66     55    17%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/core.py                                 819    577    30%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/decorators.py                           144     85    41%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/exceptions.py                            98     70    29%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/formatting.py                           134    112    16%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/globals.py                               18     12    33%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/parser.py                               236    203    14%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/termui.py                               176    149    15%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/types.py                                270    175    35%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/click/utils.py                                167    115    31%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/ddt.py                                        107     77    28%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/__init__.py                              17      0   100%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/_compat.py                               52     31    40%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/app.py                                  563    283    50%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/blueprints.py                           157    118    25%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/cli.py                                  250    190    24%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/config.py                                90     57    37%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/ctx.py                                  151     48    68%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/debughelpers.py                          86     65    24%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/globals.py                               26      6    77%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/helpers.py                              317    213    33%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/json.py                                  87     43    51%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/sessions.py                             143     81    43%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/signals.py                               29      2    93%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/templating.py                            82     65    21%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/testing.py                               65     33    49%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/views.py                                 42     10    76%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask/wrappers.py                              74     31    58%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask_env.py                                   21      2    90%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask_restful/__init__.py                     287    177    38%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask_restful/representations/__init__.py       0      0   100%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask_restful/representations/json.py          12      8    33%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask_restful/reqparse.py                     173     71    59%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/flask_restful/utils/__init__.py                21     16    24%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/itsdangerous.py                               413    294    29%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/__init__.py                             18      2    89%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/_compat.py                              63     31    51%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/_stringdefs.py                          34     30    12%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/bccache.py                             161    118    27%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/compiler.py                           1094    950    13%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/defaults.py                             20      0   100%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/environment.py                         515    410    20%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/exceptions.py                           70     48    31%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/filters.py                             356    274    23%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/idtracking.py                          195    149    24%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/lexer.py                               351    227    35%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/loaders.py                             223    170    24%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/nodes.py                               433    228    47%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/optimizer.py                            17      8    53%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/parser.py                              639    580     9%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/runtime.py                             397    280    29%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/tests.py                                55     27    51%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/utils.py                               295    205    31%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/jinja2/visitor.py                              40     31    22%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/markupsafe/__init__.py                        156     86    45%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/markupsafe/_compat.py                          14      5    64%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/markupsafe/_native.py                          15      9    40%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/six.py                                        444    226    49%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/__init__.py                             4      0   100%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/_locale_data.py                         3      0   100%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/concurrent.py                         193    119    38%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/escape.py                             144    100    31%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/gen.py                                523    407    22%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/http1connection.py                    410    357    13%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/httpserver.py                         109     76    30%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/httputil.py                           394    298    24%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/ioloop.py                             398    294    26%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/iostream.py                           742    639    14%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/locale.py                             231    191    17%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/log.py                                102     80    22%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/netutil.py                            217    157    28%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/platform/__init__.py                    0      0   100%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/platform/asyncio.py                   114     85    25%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/platform/auto.py                       24      8    67%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/platform/interface.py                  17      7    59%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/platform/posix.py                      40     26    35%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/process.py                            189    148    22%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/stack_context.py                      155    124    20%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/tcpserver.py                           83     60    28%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/template.py                           485    385    21%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/util.py                               171    103    40%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/web.py                               1453   1190    18%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/tornado/wsgi.py                               175    136    22%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/__init__.py                           29      8    72%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/_compat.py                           148     80    46%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/_internal.py                         238    166    30%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/datastructures.py                   1316    826    37%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/exceptions.py                        192     37    81%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/filesystem.py                         22     13    41%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/formparser.py                        242    169    30%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/http.py                              407    334    18%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/local.py                             215     67    69%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/routing.py                           629    308    51%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/test.py                              467    212    55%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/urls.py                              415    185    55%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/utils.py                             243    173    29%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/wrappers.py                          605    284    53%
/home/ubuntu/.virtualenvs/ctrp/lib/python3.5/site-packages/werkzeug/wsgi.py                              542    371    32%
app.py                                                                                                    53     15    72%
tests/__init__.py                                                                                          0      0   100%
tests/test_api.py                                                                                         12      0   100%
--------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                                  23565  16127    32%
(ctrp) ubuntu@ubuntu-xenial:~/transfer$
like image 820
user1187968 Avatar asked May 08 '17 19:05

user1187968


2 Answers

create .coveragerc with the following

# .coveragerc to control coverage.py
[run]
omit =
    */site-packages/*
    */distutils/*
    tests/*
like image 83
Omar S. Avatar answered Oct 29 '22 15:10

Omar S.


What we did is the following:

Given the project was organized as pip package:

/ project-name-root
  + meta-information
  + requirements.txt
  + project-name
    + some-modules
    + __init__.py
  + test
    + __init__.py
    + some-test-suites

we run the coverage command like this:

cd project-name-root
PYTHONPATH=. coverage run --source=project-name ./test/__init__.py
coverage report -m

The --source parameter states for which files/directory to keep track of coverage and the last argument is what is executed. You can find the description here: coverage --source parameter.

If you are interested in a working package template we use cookiecutter and this template created by the author of cookiecutter.

like image 32
ovanes Avatar answered Oct 29 '22 14:10

ovanes