how to make py.test --cov skip virtualenv directory

@alexamici answer is the correct one for virtualenvs, but for completeness, adding pyproject.toml (suggested config file for python projects) set up:

[tool.coverage.run]
omit = [
    "file/to/exclude.py",
    "other/file.py",
    "some/path/*"
    ]

In the root of your project, create file .coveragerc containing:

[run]
omit = path_to_libs_to_omit/*

Depending on your setup, you might need to add --cov-config=path/to/.coveragerc as option to the py.test command.

There are more options you can use to configure coverage.


This is how I managed to do it: pytest.py

[pytest]
python_files = tests.py test_*.py *_tests.py
addopts = -v -p no:warnings --nomigrations --cov=. --no-cov-on-fail

coveragerc

[run]
omit = venv/*

You should add your module's name to the --cov command line option, for example form pytest-cov documentation:

py.test --cov=myproj tests/

This restrict the coverage to the module myproj and all its sub-modules.