coverage-conditional-plugin icon indicating copy to clipboard operation
coverage-conditional-plugin copied to clipboard

Pytest variable not detected?

Open dosaki opened this issue 3 years ago • 4 comments

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/[email protected]/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

dosaki avatar Sep 26 '22 09:09 dosaki

I think that the problem is that tests/integration/' in os_environ['PYTEST_CURRENT_TEST'].lower() this part cannot access PYTEST_CURRENT_TEST for some reason.

Maybe it is somehow reset?

sobolevn avatar Sep 26 '22 11:09 sobolevn

My theory is that pytest injects this variable after the plug in evaluates it.

I don't know how I could go about testing if that's the case.

dosaki avatar Sep 26 '22 11:09 dosaki

Maybe you can do something like this?

# Makefile
test_unit:
    TEST_TYPE=unit pytest tests/unit

test_integration:
    TEST_TYPE=integration pytest tests/integration

And then use TEST_TYPE the same way you use PYTEST_CURRENT_TEST?

sobolevn avatar Sep 26 '22 11:09 sobolevn

At the moment, I execute the scripts via a bash script and I export a variable (like I mentioned in #179)

dosaki avatar Sep 26 '22 12:09 dosaki