Skip to content

Report an unimportable example as one failing test, not a collection error - #502

Merged
tpike3 merged 2 commits into
mesa:mainfrom
lahari936:isolate-unimportable-examples
Nov 1, 2026
Merged

tpike3 merged 2 commits into
mesa:mainfrom
lahari936:isolate-unimportable-examples

Conversation

@lahari936

Copy link
Copy Markdown
Contributor

The gap

test_examples.py builds its parameter list inside the decorator:

@pytest.mark.parametrize("model_class", get_models("examples"))

get_models() calls importlib.import_module() on every model.py under examples/, unguarded, while pytest is still collecting. One ImportError therefore propagates out of collection and pytest stops:

ERROR test_examples.py
Interrupted: 1 error during collection
1 error in 11.87s

That is not one failing example — it is zero tests run, for all 26, and the summary says nothing about the 25 that are fine.

This is the amplifier behind the recent run of breakages. The warehouse example's stale mesa.meta_agents import (fixed in #499) aborted collection, so mesa's own examples job was red for 24 consecutive runs on main (2026-10-22 → 2026-10-28) while reporting nothing about any other example. A genuinely new breakage introduced in that window would have been indistinguishable from the standing one, and PRs merged throughout.

Reproducing it

On 9112701, add one throwaway example whose import fails:

mkdir -p examples/zz_broken_demo
printf 'import mesa\nimport totally_absent_package_xyz\n\n\nclass BrokenModel(mesa.Model):\n    pass\n' \
  > examples/zz_broken_demo/model.py
pytest test_examples.py -q
result
before 1 error in 11.87s — Interrupted: 1 error during collection, 0 tests ran
after 1 failed, 25 passed — FAILED test_examples.py::test_model_steps[examples.zz_broken_demo.model]

The change

Guard the import and append a parametrized case carrying the exception, keyed by module name. The test fails with the real cause:

Failed: example could not be imported: ModuleNotFoundError("No module named 'totally_absent_package_xyz'")

Twelve added lines, no new dependency, no change to how healthy examples are collected or named — the ids stay test_model_steps[AcoTspModel] and a healthy tree is still 25 passed.

Why this shape

The alternative is to parametrize over module paths and import inside the test, which also works but collapses every model class in a module into a single test and renames all 25 existing ids. Guarding the import keeps the current granularity and touches one function.

What I deliberately left out

No fixture-based regression test for this behaviour. A permanently-broken example committed under examples/ would be collected as a real example and fail forever, so the fixture would have to live outside that tree with its own os.walk root — more machinery than the twelve lines it guards. The manual reproduction above is in the commit message so the next person can re-run it in thirty seconds. Happy to add it properly if you would rather have it enforced.

…error

`get_models()` runs inside the `@pytest.mark.parametrize` decorator, so every
`model.py` under `examples/` is imported while pytest is still collecting. The
import is unguarded, so one `ImportError` propagates out of collection and
pytest stops with

    ERROR test_examples.py
    Interrupted: 1 error during collection

That is not one failing example. It is zero tests run, for every example in
the repository, and the summary line says nothing about the other 25.

This is what made the recent breakages invisible rather than merely broken.
The warehouse example's stale `mesa.meta_agents` import (mesa#499) aborted
collection, so mesa's own `examples` job went red and stayed red for 24
consecutive runs on `main` without ever reporting on the examples that were
fine. A new breakage introduced during that window would have looked
identical to the standing one.

Guard the import and turn a failure into a single parametrized case that
fails with the underlying exception, keyed by module name. One broken example
now costs exactly one red test.

Demonstrated on 9112701 by adding a throwaway example whose `model.py`
imports a missing module:

    before   1 error in 11.87s        (Interrupted during collection, 0 ran)
    after    1 failed, 25 passed      (FAILED ...[examples.zz_broken_demo.model])

With a healthy tree the behaviour is unchanged: 25 passed, and the test ids
for working examples stay as they were (`test_model_steps[AcoTspModel]`).

Co-Authored-By: Claude Opus 5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 09:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@mesa mesa deleted a comment from coderabbitai Bot Nov 1, 2026

@tpike3 tpike3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks @lahari936

@tpike3
tpike3 merged commit 8163b32 into mesa:main Nov 1, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants