# coverage-conditional-plugin
[![wemake.services](https://img.shields.io/badge/%20-wemake.services-green.svg?label=%20&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC%2FxhBQAAAAFzUkdCAK7OHOkAAAAbUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP%2F%2F%2F5TvxDIAAAAIdFJOUwAjRA8xXANAL%2Bv0SAAAADNJREFUGNNjYCAIOJjRBdBFWMkVQeGzcHAwksJnAPPZGOGAASzPzAEHEGVsLExQwE7YswCb7AFZSF3bbAAAAABJRU5ErkJggg%3D%3D)](https://wemake.services)
[![Build Status](https://github.com/wemake-services/coverage-conditional-plugin/workflows/test/badge.svg?branch=master&event=push)](https://github.com/wemake-services/coverage-conditional-plugin/actions?query=workflow%3Atest)
[![codecov](https://codecov.io/gh/wemake-services/coverage-conditional-plugin/branch/master/graph/badge.svg)](https://codecov.io/gh/wemake-services/coverage-conditional-plugin)
[![Python Version](https://img.shields.io/pypi/pyversions/coverage-conditional-plugin.svg)](https://pypi.org/project/coverage-conditional-plugin/)
[![wemake-python-styleguide](https://img.shields.io/badge/style-wemake-000000.svg)](https://github.com/wemake-services/wemake-python-styleguide)
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](https://sobolevn.me/2020/02/conditional-coverage).
## Installation
```bash
pip install coverage-conditional-plugin
```
Then you will need to add to your `setup.cfg` or `.coveragerc` file
some extra configuration:
```ini
[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`:
```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:
```python
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:
```ini
[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](https://www.python.org/dev/peps/pep-0496/).
See [Strings](https://www.python.org/dev/peps/pep-0496/#strings)
and [Version Numbers](https://www.python.org/dev/peps/pep-0496/#version-numbers)
sections for available values. Also, we provide a bunch of additional markers:
- `sys_version_info` is the same as [`sys.version_info`](https://docs.python.org/3/library/sys.html#sys.version_info)
- `os_environ` is the same as [`os.environ`](https://docs.python.org/3/library/os.html#os.environ)
- `is_installed` is our custom function that tries to import the passed string, returns `bool` value
- `package_version` is our custom function that tries to get package version from `pkg_resources` and returns its [parsed version](https://packaging.pypa.io/en/latest/version/#packaging.version.parse)
Use `get_env_info` to get values for the current environment:
```python
from coverage_conditional_plugin import get_env_info
get_env_info()
```
## License
[MIT](https://github.com/wemake.services/coverage-conditional-plugin/blob/master/LICENSE)
## Credits
This project was generated with [`wemake-python-package`](https://github.com/wemake-services/wemake-python-package). Current template version is: [a61725009d8399ae77376b5ad9de354214bc1159](https://github.com/wemake-services/wemake-python-package/tree/a61725009d8399ae77376b5ad9de354214bc1159). See what is [updated](https://github.com/wemake-services/wemake-python-package/compare/a61725009d8399ae77376b5ad9de354214bc1159...master) since then.
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
最新资源
- zigbee CC2530无线自组网协议栈系统代码实现协调器与终端基于GenericApp的无线收发例程.zip
- zigbee CC2530无线自组网协议栈系统代码实现协调器与终端无线收发控制LED灯.zip
- zigbee CC2530无线自组网协议栈系统代码实现协调器与终端之间的数据相互收发.zip
- zigbee CC2530无线自组网协议栈系统代码实现协议捕捉与数据分析.zip
- zigbee CC2530无线自组网协议栈系统代码实现终端采集光敏传感器并发给协调器串口输出.zip
- zigbee CC2530无线自组网协议栈系统代码实现终端采集气体传感器值发给协调器并串口打印.zip
- zigbee CC2530无线自组网协议栈系统代码实现终端采集人体红外热释电传感器发送给协调器并串口输出.zip
- 功能完善的小说CMS系统项目全套技术资料.zip
- zigbee CC2530无线自组网协议栈系统代码实现终端采集温度传感器DS18B20发送给协调器并串口打印输出.zip
- 食物营养分析-java-基于 SpringBoot 的食物营养分析与推荐网站的设计与实现(毕业论文)
- zigbee CC2530无线自组网协议栈系统代码实现终端采集温湿度传感器DHT11并发给协调器并串口打印输出.zip
- zigbee CC2530无线自组网协议栈系统的RemoTI源码.zip
- zigbee CC2530无线自组网协议栈系统之新建自己的Zstack工程模板(协调器、终端、路由器).zip
- 宠物救助及领养-java-基于springBoot的宠物救助及领养平台的设计与实现(毕业论文)
- 从基础到高级:Rust语言教程与编程实战
- 城市公交查询-java-基于springBoot的城市公交查询系统设计与实现(毕业论文)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0