diff --git a/pytest_monitor/models.py b/pytest_monitor/models.py new file mode 100644 index 0000000..15fd3b3 --- /dev/null +++ b/pytest_monitor/models.py @@ -0,0 +1,6 @@ +from dataclasses import dataclass, field + + +@dataclass(frozen=True) +class PyTestMonitorConfig: + enabled: bool = field(default=True, init=True) diff --git a/pytest_monitor/pytest_monitor.py b/pytest_monitor/pytest_monitor.py index 1863857..e5d85c4 100644 --- a/pytest_monitor/pytest_monitor.py +++ b/pytest_monitor/pytest_monitor.py @@ -6,6 +6,7 @@ import memory_profiler import pytest +from pytest_monitor.models import PyTestMonitorConfig from pytest_monitor.session import PyTestMonitorSession # These dictionaries are used to compute members set on each items. @@ -23,7 +24,7 @@ } PYTEST_MONITOR_DEPRECATED_MARKERS = {} -PYTEST_MONITORING_ENABLED = True +config_stash = pytest.StashKey[PyTestMonitorConfig]() def pytest_addoption(parser): @@ -117,13 +118,13 @@ def pytest_configure(config): ) -def pytest_runtest_setup(item): +def pytest_runtest_setup(item: pytest.Item): """ Validate marker setup and print warnings if usage of deprecated marker is identified. Setting marker attribute to the discovered item is done after the above described verification. :param item: Test item """ - if not PYTEST_MONITORING_ENABLED: + if not item.session.stash[config_stash].enabled: return item_markers = {mark.name: mark for mark in item.iter_markers() if mark and mark.name.startswith("monitor_")} mark_to_del = [] @@ -158,7 +159,7 @@ def pytest_runtest_setup(item): @pytest.hookimpl(tryfirst=True, hookwrapper=True) -def pytest_runtest_makereport(item, call): +def pytest_runtest_makereport(item: pytest.Item, call: pytest.CallInfo): """ Used to identify the current call to add times. :param item: Test item @@ -172,8 +173,8 @@ def pytest_runtest_makereport(item, call): setattr(item, "test_effective_start_time", call.start) -def pytest_runtest_call(item): - if not PYTEST_MONITORING_ENABLED: +def pytest_runtest_call(item: pytest.Item): + if not item.session.stash[config_stash].enabled: return setattr(item, "monitor_results", False) if hasattr(item, "module"): @@ -187,7 +188,7 @@ def pytest_runtest_call(item): @pytest.hookimpl -def pytest_pyfunc_call(pyfuncitem): +def pytest_pyfunc_call(pyfuncitem: pytest.Function): """ Core sniffer logic. We encapsulate the test function in a sniffer function to collect memory results. @@ -211,7 +212,7 @@ def prof(): setattr(pyfuncitem, "mem_usage", memuse) setattr(pyfuncitem, "monitor_results", True) - if not PYTEST_MONITORING_ENABLED: + if not pyfuncitem.session.stash[config_stash].enabled: wrapped_function() else: if not pyfuncitem.session.config.option.mtr_disable_gc: @@ -227,7 +228,7 @@ def pytest_make_parametrize_id(config, val, argname): @pytest.hookimpl(hookwrapper=True) -def pytest_sessionstart(session): +def pytest_sessionstart(session: pytest.Session): """ Instantiate a monitor session to save collected metrics. We yield at the end to let pytest pursue the execution. @@ -251,15 +252,14 @@ def pytest_sessionstart(session): session.pytest_monitor = PyTestMonitorSession( db=db, remote=remote, component=component, scope=session.config.option.mtr_scope ) - global PYTEST_MONITORING_ENABLED - PYTEST_MONITORING_ENABLED = not session.config.option.mtr_none + session.stash[config_stash] = PyTestMonitorConfig(enabled=not session.config.option.mtr_none) session.pytest_monitor.compute_info(session.config.option.mtr_description, session.config.option.mtr_tags) yield @pytest.fixture(autouse=True, scope="module") -def _prf_module_tracer(request): - if not PYTEST_MONITORING_ENABLED: +def _prf_module_tracer(request: pytest.FixtureRequest): + if not request.session.stash[config_stash].enabled: yield else: t_a = time.time() @@ -288,14 +288,14 @@ def _prf_module_tracer(request): @pytest.fixture(autouse=True) def _prf_tracer(request: pytest.FixtureRequest): - if not PYTEST_MONITORING_ENABLED: + if not request.session.stash[config_stash].enabled: yield else: ptimes_a = request.session.pytest_monitor.process.cpu_times() yield ptimes_b = request.session.pytest_monitor.process.cpu_times() if not request.node.monitor_skip_test and getattr(request.node, "monitor_results", False): - item_name = request.node.originalname or request.node.name + item_name = request.node.originalname item_loc, *_ = request.node.location request.session.pytest_monitor.add_test_info( item_name,