Skip to content

Pytest variable not detected? #178

@dosaki

Description

@dosaki

Hello,

I'm trying to get this plugin to work so that I can exclude integration tests from coverage when running unit tests, and vice-versa.

My integration tests sit in tests/integration while unit tests are in tests/unit.

My plan was to use the PYTEST_CURRENT_TEST environment variable that pytest automatically creates to make this distinction.
However, the rules seem to evaluated before the variable is introduced. I get the error below:

Exception during conditional coverage evaluation: Traceback (most recent call last):
  File "/Users/tiagocorreia/dev/pita/venv/lib/python3.9/site-packages/coverage_conditional_plugin/__init__.py", line 99, in _should_be_applied
    return eval(code, env_info)  # noqa: WPS421, S307
  File "<string>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.9/3.9.14/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'PYTEST_CURRENT_TEST'

Confusingly, this snippet prints the variable value though:

from coverage_conditional_plugin import get_env_info
print(get_env_info())

FYI, the output of the above snippet:

{
    'implementation_name': 'cpython', 'implementation_version': '3.9.14', 'os_name': 'posix',
    'platform_machine': 'arm64',
    'platform_release': '21.6.0', 'platform_system': 'Darwin',
    'platform_version': 'Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000',
    'python_full_version': '3.9.14', 'platform_python_implementation': 'CPython', 'python_version': '3.9',
    'sys_platform': 'darwin',
    'sys_version_info': sys.version_info(major=3, minor=9, micro=14, releaselevel='final', serial=0),
    'os_environ': environ({
                           # some other stuff
                           'PYTEST_CURRENT_TEST': 'tests/unit/domain/test_label.py::test_Label_from_csv (call)'
                           })
    # other bits
}

and my .coveragerc:

[run]
omit = *entrypoint*,*/tests/*

[coverage:run]
plugins =
  coverage_conditional_plugin

[coverage:report]
skip_empty = true
omit = *entrypoint*,*/tests/*

[coverage:coverage_conditional_plugin]
rules =
  "'tests/integration/' in os_environ['PYTEST_CURRENT_TEST'].lower()": integration
  "'tests/unit/' in os_environ['PYTEST_CURRENT_TEST'].lower()": unit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions