1. memedough
  2. pytest-cov
  3. Issues
Issue #15 new

KeyError: 'COV_CORE_SOURCE' when clearing environment

space one
created an issue

For security purposes i am clearing the environment in my application. This raises the following error in combination with coverage.

python2 -m pytest --cov-report html --cov test_broken.py test_broken.py

import os

def test_broken():
        os.environ.clear()

of course the .clear() is not done in the test definition but in the code which is tested.

test_broken.py .Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/lib/python2.7/site-packages/pytest.py", line 10, in <module>
    raise SystemExit(pytest.main())
  File "/usr/lib/python2.7/site-packages/_pytest/config.py", line 19, in main
    exitstatus = config.hook.pytest_cmdline_main(config=config)
  File "/usr/lib/python2.7/site-packages/_pytest/core.py", line 368, in __call__
    return self._docall(methods, kwargs)
  File "/usr/lib/python2.7/site-packages/_pytest/core.py", line 379, in _docall
    res = mc.execute()
  File "/usr/lib/python2.7/site-packages/_pytest/core.py", line 297, in execute
    res = method(**kwargs)
  File "/usr/lib/python2.7/site-packages/_pytest/main.py", line 111, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/usr/lib/python2.7/site-packages/_pytest/main.py", line 104, in wrap_session
    exitstatus=session.exitstatus)
  File "/usr/lib/python2.7/site-packages/_pytest/core.py", line 368, in __call__
    return self._docall(methods, kwargs)
  File "/usr/lib/python2.7/site-packages/_pytest/core.py", line 379, in _docall
    res = mc.execute()
  File "/usr/lib/python2.7/site-packages/_pytest/core.py", line 297, in execute
    res = method(**kwargs)
  File "/usr/lib/python2.7/site-packages/_pytest/terminal.py", line 325, in pytest_sessionfinish
    __multicall__.execute()
  File "/usr/lib/python2.7/site-packages/_pytest/core.py", line 297, in execute
    res = method(**kwargs)
  File "/usr/lib/python2.7/site-packages/pytest_cov.py", line 98, in pytest_sessionfinish
    self.cov_controller.finish()
  File "/usr/lib/python2.7/site-packages/cov_core.py", line 142, in finish
    self.unset_env()
  File "/usr/lib/python2.7/site-packages/cov_core.py", line 65, in unset_env
    del os.environ['COV_CORE_SOURCE']
  File "/usr/lib/python2.7/os.py", line 496, in __delitem__
    del self.data[key]
KeyError: 'COV_CORE_SOURCE'

Comments (3)

  1. Fotis Gimian

    Hey there guys, hope you're doing well :)

    I came across this issue today myself as well and just wanted to offer a little workaround which you can use in your test cases.

    Rather than clearing the entire environment when you start your test run:

    os.environ = {}
    

    Clear out everything except for the keys that start with COV_CORE_:

    os.environ = dict([(k, v) for k, v in os.environ.items()
                      if k.startswith('COV_CORE_')])
    

    Hope this helps as a workaround until a permanent solution is found

    Cheers

    Fotis

  2. Log in to comment