Option C oc_diagnostics
Loading...
Searching...
No Matches
conftest Namespace Reference

Functions

 suppress_preinit_warning ()

Detailed Description

pytest Session Configuration — oc_diagnostics
==============================================

Purpose
-------
Provide a single session-scoped pytest fixture that suppresses the one-shot
pre-init ``RuntimeWarning`` for the full pytest test session.

Background
----------
``unified_debug._dbg()`` emits a one-shot ``RuntimeWarning`` the first time it
is called before ``oc_diagnostics.init()`` has been called.  This is correct
and useful behaviour in production — but in the test suite, ``init()`` is
intentionally omitted from every test to avoid installing stream interceptors or
writing log files.  As a result, the first test that instantiates
``DiagnosticsMiddleware`` (or any other code that calls ``_dbg()`` at class
construction time) triggers the warning as a false positive.

``run_tests.py`` already handles this by setting ``_init_warned = True`` at the
top of ``main()``.  This ``conftest.py`` provides the equivalent suppression for
direct pytest invocations (e.g. ``pytest tests/``, ``pytest tests/test_fastapi_middleware.py``).

Effect on ``test_dbg_before_init_warning``
------------------------------------------
That test explicitly exercises the one-shot warning mechanism.  It saves
``_init_warned``, resets it to ``False`` in its own setup, and restores it in
``finally``.  The session fixture does not interfere because the test owns its
own state transitions.

Contracts
---------
- Must not start servers or modify persistent state.
- Must not suppress legitimate ``RuntimeWarning`` instances from production code.
- ``test_dbg_before_init_warning`` must continue to pass unchanged.

Function Documentation

◆ suppress_preinit_warning()

conftest.suppress_preinit_warning ( )
Suppress the one-shot pre-init RuntimeWarning for the full test session.

Mirrors the ``_ud_runner._init_warned = True`` line at the top of
``run_tests.py main()``, ensuring consistent warning-free output whether
tests are driven by ``python tests/run_tests.py`` or ``pytest tests/``.

Definition at line 47 of file conftest.py.