running nose --with-coverage to get all the package files, but not other dependencies and libs

alonisser picture alonisser · Sep 6, 2013 · Viewed 20.7k times · Source

My project folder(yeah - I know it's best practice) is something like:

.
├── app.py
├── otherscript.py
├── tests/
└── tools/
    ├── __init__.py
    └── toolfile.py

I need nose --with-coverage to test the .py scripts in the main folder, tools folder and exclude the tests folder (although I don't really care about excluding that)

When I run basic

nose --with-coverage

I get coverage on all installed dependencies and libs (flask, requests, etc)

when I run

nose --with-coverage --cover-package=folder name (or . or ./)

I get coverage for the tests folder. the tools/__init__.py file and app.py but not for the rest of the scripts:

> (opentaba-server) D:\username\pathto\opentaba-server>nosetests --with-coverage -- cover-package=./ ... Name                                      

> Stmts   Miss  Cover   Missing
> ----------------------------------------------------------------------- Tests\functional_tests\test_return_json      26      0   100%
> Tests\unit_test\test_createdb                 0      0   100%
> Tests\unit_test\test_scrape                   0      0   100% app     
> 63     15    76%   22, 24, 72-81, 8 8-106, 133 tools\__init__         
> 0      0   100%
> ----------------------------------------------------------------------- TOTAL                                        89     15    83%
> ---------------------------------------------------------------------- Ran 3 tests in 5.182s OK

When I run with the --cover-inclusive flag . It just fails with :

nosetests-scripts.py: error: no such option: --with-coverage

I'll be glad for any help with this

Answer

Georg picture Georg · Dec 8, 2014

I had a very similar problem with generated code. The solution was to exclude the generated code or tools code in your case only from the reports.

So we now use nosetests to run our tests like

nosetests --with-coverage --cover-inclusive --cover-package=$(PACKAGE)

and afterwards, we create the reports manually, so

coverage combine
coverage report --omit 'tools/*'

Thus, coverage.py will cover your tools package, but they won't show up in the reports.