python 3.x - py.test monkeypatch a package to disable code from __init__.py -


i have situation have object being instantiated inside package's __init__.py need monkeypatch py.test work correctly (it attempts load asyncio event loop, , want capture that).

here piece of code pampas/core/__init.py file:

from pampas.core import _yaml_config config = _yaml_config.mainconfig() 

i have test module attempts import module package (in tests/pampas/core/test_const.py):

test session starts (platform: linux, python 3.6.1, pytest 3.0.7, pytest-sugar 0.8.0) run-last-failure: rerun last 13 failures first rootdir: /lfs/opt/qqpamp0/venv/cliff/nrpe/pampas-py, inifile: setup.cfg plugins: sugar-0.8.0, mock-1.5.0, asyncio-0.5.0, aiohttp-0.1.3

―――――――――――――― error collecting tests/pampas/core/test_const.py ――――――――――――――― tests/pampas/core/test_const.py:3: in <module>     pampas.core import const pampas/core/__init__.py:22: in <module>     config = _yaml_config.mainconfig() pampas/core/_yaml_config.py:983: in __init__     super().__init__(extra_attrs=extra_attrs) pampas/core/_yaml_config.py:664: in __init__     self.load_entries(editable=false) pampas/core/_yaml_config.py:805: in load_entries     editable pampas/core/_yaml_config.py:827: in _set_path_attr     attr = configyamlattr(filepath, editable) pampas/core/_yaml_config.py:556: in __init__     self._context_manager = yamlasynccontextmanager(filepath, editable) pampas/core/_yaml_config.py:514: in __init__     self._lock = asyncio.lock() ../../../../pkg/lib/python3.6/asyncio/locks.py:149: in __init__     self._loop = events.get_event_loop() ../../../../pkg/lib/python3.6/asyncio/events.py:678: in get_event_loop     return get_event_loop_policy().get_event_loop() ../../../../pkg/lib/python3.6/asyncio/events.py:584: in get_event_loop     % threading.current_thread().name) e   runtimeerror: there no current event loop in thread 'mainthread'.  !!!!!!!!!!!!!!!!!!! interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!! 

my issue appears connected me attempting use asyncio.get_event_loop() call current event loop, py.test has problems with, , workaround explained here: http://aiohttp.readthedocs.io/en/stable/testing.html#writing-testable-services

however big issue actual loading of config variable in __init__.py module. need disabled testing, without making changes code purposes of testing if @ possible.

update:

i believe found solution, using pytest-asyncio extension.

import asyncio  import pytest   @pytest.fixture() def const(event_loop):     asyncio.set_event_loop(event_loop)     pampas.core import const     return const   @pytest.mark.parametrize('nagios,pampas', [     ('ok', 'green'),     ('warning', 'yellow'),     ('critical', 'red'),     ('unknown', 'notmonitored'), ]) def test_nagios_to_pampas(const, nagios, pampas):     assert const.nagiosstate[nagios].pampas == const.pampasstate[pampas] 


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -