6pytest Session Configuration — oc_diagnostics
7==============================================
11Provide a single session-scoped pytest fixture that suppresses the one-shot
12pre-init ``RuntimeWarning`` for the full pytest test session.
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.
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``).
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
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.
46@pytest.fixture(autouse=True, scope="session")
48 """Suppress the one-shot pre-init RuntimeWarning for the full test session.
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/``.
54 _ud._init_warned =
True
suppress_preinit_warning()