mesa icon indicating copy to clipboard operation
mesa copied to clipboard

test_suppressed_wake_is_logged_at_debug fails in full-suite order: importing the Boltzmann example app disables MESA logger propagation

Open lahari936 opened this issue 1 month ago • 1 comments

Describe the bug

tests/experimental/test_actions.py::TestOnIdle::test_suppressed_wake_is_logged_at_debug fails when the full suite runs, but passes in isolation. It is an ordering dependency, not a flake: importing one example app disables propagation on the MESA logger process-wide, and caplog then captures nothing.

This is on main (5451086f), unrelated to any open PR — I ran into it while checking that my own change had not broken anything.

To Reproduce

pytest tests/                       # 859 passed, 1 failed
pytest tests/experimental/test_actions.py   # 104 passed

Narrowed down to a two-test reproduction:

pytest tests/examples/test_examples.py::test_boltzmann_model \
       "tests/experimental/test_actions.py::TestOnIdle::test_suppressed_wake_is_logged_at_debug"
# 1 failed, 1 passed

Failure:

    assert "suppressed repeat on_idle wake" in caplog.text
E   AssertionError: assert 'suppressed repeat on_idle wake' in ''
tests\experimental\test_actions.py:1478: AssertionError

Cause

test_boltzmann_model imports the example's app module:

https://github.com/mesa/mesa/blob/main/tests/examples/test_examples.py#L30

and that module calls log_to_stderr(INFO) at import time:

https://github.com/mesa/mesa/blob/main/mesa/examples/basic/boltzmann_wealth_model/app.py#L15

log_to_stderr takes propagate: bool = False and applies it to the MESA root logger:

https://github.com/mesa/mesa/blob/main/mesa/mesa_logging.py#L191

So after that import, for the rest of the process:

>>> from mesa.mesa_logging import get_rootlogger, INFO, log_to_stderr
>>> get_rootlogger().propagate
True
>>> log_to_stderr(INFO)
>>> get_rootlogger().propagate
False

caplog works by handling records that propagate to the root logger, so once MESA.propagate is False, records from MESA.mesa.experimental.actions.actions never reach it and caplog.text is empty. Nothing resets the flag, so every later test in the same process that asserts on caplog for a MESA.* logger is affected — this test is just the one that currently does.

Expected behavior

Test results should not depend on whether an example app was imported earlier in the session.

Possible directions (happy to open a PR for whichever you prefer)

  1. Don't configure logging as an import side effect in example apps — move log_to_stderr(INFO) behind if __name__ == "__main__":, or drop it. This seems like the real fix: importing an example shouldn't reconfigure logging for the whole process.
  2. Add an autouse fixture that saves and restores MESA logger handlers/level/propagate around tests.
  3. Make the test independent of propagation by attaching its own handler to the MESA logger instead of relying on caplog.

Additional context

Mesa 4.0.0a0 at 5451086f, Python 3.14.2, Windows. The full-suite failure reproduces on a clean worktree of upstream/main with no local changes.

lahari936 avatar Oct 11 '26 04:10 lahari936

Duplicate of #3935, which I had already opened earlier with the same root cause. Closing this one; please follow #3935.

lahari936 avatar Oct 12 '26 00:10 lahari936