Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest variable not detected? #178

Open
dosaki opened this issue Sep 26, 2022 · 4 comments
Open

Pytest variable not detected? #178

dosaki opened this issue Sep 26, 2022 · 4 comments

Comments

@dosaki
Copy link

dosaki commented Sep 26, 2022

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
@sobolevn
Copy link
Member

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?

@dosaki
Copy link
Author

dosaki commented Sep 26, 2022

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.

@sobolevn
Copy link
Member

sobolevn commented Sep 26, 2022

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?

@dosaki
Copy link
Author

dosaki commented Sep 26, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants