Package: coverage-conditional-plugin
coverage-conditional-pluginConditional 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 othersOS version, some code might be Windows, Mac, or Linux onlyExternal packages, some code is only executed when some 3rd party package is installedCurrent best practice is to use# pragma: no coverfor 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.Readthe announcing post.Installationpipinstallcoverage-conditional-pluginThen you will need to add to yoursetup.cfgor.coveragercfile
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 files to conditionally omit:omit="sys_platform== 'win32'":"my_project/omit*.py""my_project/win.py"# Here we specify our pragma rules:rules="sys_version_info >=(3, 8)": py-gte-38"is_installed('mypy')":has-mypyOr to yourpyproject.toml:[tool.coverage.run]# Here we specify plugins for coverage to be used:plugins=["coverage_conditional_plugin"][tool.coverage.coverage_conditional_plugin.omit]# Here we specify files to conditionally omit:"my_project/omit*.py"="sys_platform == 'win32'"[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!ExampleImagine that we have this code:try:# pragma: has-djangoimportdjangoexceptImportError:# pragma: has-no-djangodjango=Nonedefrun_if_django_is_installed():ifdjangoisnotNone:# 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-djangoWhen running tests with and withoutdjangoinstalled
you will have100%coverage in both cases.But, different lines will be included.
Withdjangoinstalled it will include
bothtry:andif django is not None:conditions.When running withoutdjangoinstalled,
it will includeexcept ImportError:line.Writing pragma rulesFormat for pragma rules is:"pragma-condition": pragma-nameCode inside"pragma-condition"is evaluted witheval.
Make sure that the input you pass there is trusted!"pragma-condition"must returnboolvalue after evaluation.We support all environment markers specified inPEP-496.
SeeStringsandVersion Numberssections for available values. Also, we provide a bunch of additional markers:sys_version_infois the same assys.version_infoos_environis the same asos.environis_installedis our custom function that tries to import the passed string, returnsboolvaluepackage_versionis our custom function that tries to get package version frompkg_resourcesand returns itsparsed versionUseget_env_infoto get values for the current environment:fromcoverage_conditional_pluginimportget_env_infoget_env_info()Writing omitsOmits allow entire files to be conditionally omitted from coverage measurement.The TOML format for omits is:[tool.coverage.coverage_conditional_plugin.omit]"pragma-condition"=["project/prefix*.py","project/filename.py"]# or"pragma-condition"="project/filename.py"Note:iniformat is not supported foromitconfiguration option,
because there's no easy way to parseinicomplex configuration.
PRs with the fix are welcome!File name patterns should follow coverage.py's[run] omitsyntax.
Seecoverage.py.LicenseMIT