Option C oc_diagnostics
Loading...
Searching...
No Matches
conftest.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/conftest.py
4
5"""
6pytest Session Configuration — oc_diagnostics
7==============================================
8
9Purpose
10-------
11Provide a single session-scoped pytest fixture that suppresses the one-shot
12pre-init ``RuntimeWarning`` for the full pytest test session.
13
14Background
15----------
16``unified_debug._dbg()`` emits a one-shot ``RuntimeWarning`` the first time it
17is called before ``oc_diagnostics.init()`` has been called. This is correct
18and useful behaviour in production — but in the test suite, ``init()`` is
19intentionally omitted from every test to avoid installing stream interceptors or
20writing log files. As a result, the first test that instantiates
21``DiagnosticsMiddleware`` (or any other code that calls ``_dbg()`` at class
22construction time) triggers the warning as a false positive.
23
24``run_tests.py`` already handles this by setting ``_init_warned = True`` at the
25top of ``main()``. This ``conftest.py`` provides the equivalent suppression for
26direct pytest invocations (e.g. ``pytest tests/``, ``pytest tests/test_fastapi_middleware.py``).
27
28Effect on ``test_dbg_before_init_warning``
29------------------------------------------
30That test explicitly exercises the one-shot warning mechanism. It saves
31``_init_warned``, resets it to ``False`` in its own setup, and restores it in
32``finally``. The session fixture does not interfere because the test owns its
33own state transitions.
34
35Contracts
36---------
37- Must not start servers or modify persistent state.
38- Must not suppress legitimate ``RuntimeWarning`` instances from production code.
39- ``test_dbg_before_init_warning`` must continue to pass unchanged.
40"""
41
42import pytest
44
45
46@pytest.fixture(autouse=True, scope="session")
48 """Suppress the one-shot pre-init RuntimeWarning for the full test session.
49
50 Mirrors the ``_ud_runner._init_warned = True`` line at the top of
51 ``run_tests.py main()``, ensuring consistent warning-free output whether
52 tests are driven by ``python tests/run_tests.py`` or ``pytest tests/``.
53 """
54 _ud._init_warned = True
suppress_preinit_warning()
Definition conftest.py:47