Conditional coverage based on any rules you define!
Project description
coverage-conditional-plugin
Conditional coverage based on any rules you define!
Some projects have different parts that relies on different environments:
- Python version, some code is only executed on specific versions and ignored on others
- OS version, some code might be Windows, Mac, or Linux only
- External packages, some code is only executed when some 3rd party package is installed
Current best practice is to use # pragma: no cover
for this places in our project.
This project allows to use configurable pragmas
that include code to the coverage if some condition evaluates to true,
and fallback to ignoring this code when condition is false.
Read the announcing post.
Installation
pip install coverage-conditional-plugin
Then you will need to add to your setup.cfg
or .coveragerc
file
some extra configuration:
[coverage:run]
# Here we specify plugins for coverage to be used:
plugins =
coverage_conditional_plugin
[coverage:coverage_conditional_plugin]
# Here we specify our pragma rules:
rules =
"sys_version_info >= (3, 8)": py-gte-38
"is_installed('mypy')": has-mypy
Or to your pyproject.toml
:
[tool.coverage.run]
# Here we specify plugins for coverage to be used:
plugins = ["coverage_conditional_plugin"]
[tool.coverage.coverage_conditional_plugin.rules]
# Here we specify our pragma rules:
py-gte-38 = "sys_version_info >= (3, 8)"
has-mypy = "is_installed('mypy')"
Adapt rules to suit your needs!
Example
Imagine that we have this code:
try: # pragma: has-django
import django
except ImportError: # pragma: has-no-django
django = None
def run_if_django_is_installed():
if django is not None: # pragma: has-django
...
And here's the configuration you might use:
[coverage:coverage_conditional_plugin]
rules =
"is_installed('django')": has-django
"not is_installed('django')": has-no-django
When running tests with and without django
installed
you will have 100%
coverage in both cases.
But, different lines will be included.
With django
installed it will include
both try:
and if django is not None:
conditions.
When running without django
installed,
it will include except ImportError:
line.
Writing pragma rules
Format for pragma rules is:
"pragma-condition": pragma-name
Code inside "pragma-condition"
is evaluted with eval
.
Make sure that the input you pass there is trusted!
"pragma-condition"
must return bool
value after evaluation.
We support all environment markers specified in PEP-496. See Strings and Version Numbers sections for available values. Also, we provide a bunch of additional markers:
sys_version_info
is the same assys.version_info
os_environ
is the same asos.environ
is_installed
is our custom function that tries to import the passed string, returnsbool
valuepackage_version
is our custom function that tries to get package version frompkg_resources
and returns its parsed version
Use get_env_info
to get values for the current environment:
from coverage_conditional_plugin import get_env_info
get_env_info()
License
Credits
This project was generated with wemake-python-package
. Current template version is: a61725009d8399ae77376b5ad9de354214bc1159. See what is updated since then.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file coverage-conditional-plugin-0.4.0.tar.gz
.
File metadata
- Download URL: coverage-conditional-plugin-0.4.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/2.7.10 Darwin/18.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ca50bb03b32f8ba8cf63b18d830f334cb75f401579040dd5c61df1c4687fef0 |
|
MD5 | 9990aa47453790ad546bfabdd1d98bb0 |
|
BLAKE2b-256 | 036dd62e0c0200a4e51eb736c3c9bdd9710bfe9e83c7c99e7017f4c6c4cf2c67 |
File details
Details for the file coverage_conditional_plugin-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: coverage_conditional_plugin-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/2.7.10 Darwin/18.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bf586f7b793a9e4e2950f7af32711012929b39d1a4b53a4829489e763fda2dc |
|
MD5 | 454e9d39d9cac2bfd7580e5648a9a657 |
|
BLAKE2b-256 | 684911edcc4ac2499f0ee41741b8174ebd800cb9931843bdc78860d5d634aa29 |