Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to omit (remove) virtual environment (venv) from python coverage unit testing?

https://coverage.readthedocs.io/en/coverage-4.5.1a/source.html#source

My coverage is also including “venv” folder and I would like to exclude it no matter what I do even with --include or omit nothing works

coverage run --omit /venv/* tests.py

This runs the test but still adds "venv" folder and dependencies and their % coverage

When I do

coverage run --include tests.py

To run only tests - it says

Nothing to do.

It is pretty annoying... can someone please help?

Python Coverage Report

like image 818
Radek Avatar asked Sep 02 '18 02:09

Radek


People also ask

How do you delete a VENV in Python?

There is no command for deleting your virtual environment. Simply deactivate it and rid your application of its artifacts by recursively removing it. Note that this is the same regardless of what kind of virtual environment you are using.

Should I use VENV or Virtualenv?

These are almost completely interchangeable, the difference being that virtualenv supports older python versions and has a few more minor unique features, while venv is in the standard library.

How do I check my virtual environment in Python?

If you want to verify that you're using the right pip and the right virtual environment, type pip -V and check that the path it displays points to a subdirectory of your virtual environment. Note that when you want to upgrade pip in a virtual environment, it's best to use the command python -m pip install -U pip .


1 Answers

The help text for the --omit option says (documentation)

--omit=PAT1,PAT2,...  Omit files whose paths match one of these patterns.
                      Accepts shell-style wildcards, which must be quoted.

It will not work without quoting the wildcard, as bash will expand the wildcards before handing the argument list to the coverage binary. Use single-quotes to avoid bash wildcard expansion.

To run my tests without getting coverage from any files within venv/*:

$ coverage run --omit 'venv/*' -m unittest tests/*.py && coverage report -m
........
----------------------------------------------------------------------
Ran 8 tests in 0.023s

OK
Name                      Stmts   Miss  Cover   Missing
-------------------------------------------------------
ruterstop.py                 84      8    90%   177, 188, 191-197, 207
tests/test_ruterstop.py     108      0   100%
-------------------------------------------------------
TOTAL                       192      8    96%

If you usually use plain python -m unittest to run your tests you can of course omit the test target argument as well.

$ coverage run --omit 'venv/*' -m unittest
$ coverage report -m
like image 77
sshow Avatar answered Sep 21 '22 14:09

sshow