Skip to content

fix(tests): drop module-level test calls that break local_testing collection - #29519

Closed
mateo-berri wants to merge 1 commit into
litellm_internal_stagingfrom
claude/sharp-pascal-NRwvu
Closed

fix(tests): drop module-level test calls that break local_testing collection#29519
mateo-berri wants to merge 1 commit into
litellm_internal_stagingfrom
claude/sharp-pascal-NRwvu

Conversation

@mateo-berri

Copy link
Copy Markdown
Contributor

Relevant issues

CircleCI failures on pipeline 80150 in langfuse_logging_unit_tests (job 1757575) and litellm_assistants_api_testing (job 1757577)

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

CI (LiteLLM team)

  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

🐛 Bug Fix

Changes

Both failing jobs glob tests/local_testing/**/test_*.py and then narrow with -k "langfuse" or -k "assistants". The -k filter only applies after pytest has collected every file in that glob, so anything that breaks collection of any file breaks the whole job regardless of the keyword.

tests/local_testing/test_register_model.py ended with a bare test_update_model_cost_via_completion() at module scope. That call runs while the module is being imported during collection, and the function fires a real litellm.completion(model="gpt-3.5-turbo", ...). On this run OpenAI returned a 429 (quota exceeded), the function's except block called pytest.fail(...), and pytest reported it as ERROR collecting tests/local_testing/test_register_model.py. With -x the session stopped there, so both jobs failed at collection time even though neither one actually exercises register_model.

The CircleCI reruns you linked are the "rerun from failed" attempt; their logs only show the rerun plugin choking on the recorded failure (file or directory not found: pytest.py), which is why the real cause is not visible there. The original attempt (jobs 1756105 and 1756113) shows the 429 collection error at test_register_model.py:65.

Three sibling files had the same leftover pattern (test_wandb.py, test_lunary.py, test_multiple_deployments.py). Two of them swallow exceptions so they did not error collection, but test_multiple_deployments.py also calls pytest.fail(...) in its except block, so it was one bad network call away from taking down every local_testing job the same way. pytest discovers and runs all four test_* functions on its own, so the top-level calls were dead and only harmful; this removes all four.

To keep the class of bug from coming back, test_no_top_level_test_invocations.py parses every file under tests/local_testing/ and fails if any test function is invoked at module scope. It flags all four call sites on the pre-fix tree and passes once they are gone. It is pure AST parsing with no imports or network, so it is fast and deterministic.

Screenshots / Proof of Fix

This is a pytest collection-time bug in CI, so there is no proxy endpoint to curl; the faithful reproduction is the exact collection the jobs do.

Before (the original job logs, gh tests/local_testing collection):

==================================== ERRORS ====================================
_________ ERROR collecting tests/local_testing/test_register_model.py __________
...
tests/local_testing/test_register_model.py:65: in <module>
    test_update_model_cost_via_completion()
...
E   litellm.exceptions.RateLimitError: OpenAIException - You exceeded your current quota
ERROR tests/local_testing/test_register_model.py - Failed: An error occurred: litellm.RateLimitError
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 1 error in 19.09s ===============================

After (collection no longer imports-and-runs anything, no network call):

$ uv run --no-sync python -m pytest \
    tests/local_testing/test_register_model.py \
    tests/local_testing/test_wandb.py \
    tests/local_testing/test_lunary.py \
    tests/local_testing/test_multiple_deployments.py --collect-only -q
...
tests/local_testing/test_register_model.py::test_update_model_cost_via_completion
tests/local_testing/test_wandb.py::test_wandb_logging
tests/local_testing/test_wandb.py::test_wandb_logging_async
11 tests collected in 0.08s

Regression test green, and confirmed it flags the pre-fix tree:

$ uv run --no-sync python -m pytest tests/local_testing/test_no_top_level_test_invocations.py -q
1 passed

# scanning the HEAD (pre-fix) versions reports 4 violations:
tests/local_testing/test_register_model.py:65 calls test_update_model_cost_via_completion()
tests/local_testing/test_wandb.py:54 calls test_wandb_logging_async()
tests/local_testing/test_lunary.py:29 calls test_lunary_logging()
tests/local_testing/test_multiple_deployments.py:54 calls test_multiple_deployments()

Generated by Claude Code

…lection

Several files in tests/local_testing invoked their test functions at module
scope (e.g. test_register_model.py ran test_update_model_cost_via_completion()
at the bottom of the file). Those calls execute during pytest collection, so
they fire real network requests at import time. test_register_model.py's call
hit an OpenAI 429 and raised, turning into a collection error.

A collection error aborts the whole session for every job that globs
tests/local_testing/**/test_*.py, which is why unrelated jobs like
langfuse_logging_unit_tests (-k langfuse) and litellm_assistants_api_testing
(-k assistants) both failed even though neither touches register_model;
the -k filter only applies after collection.

pytest discovers and runs these test_* functions on its own, so the top-level
calls were dead and harmful. Removes them from test_register_model.py,
test_wandb.py, test_lunary.py, and test_multiple_deployments.py, and adds a
regression test that scans the directory for module-level test invocations.
@codecov

codecov Bot commented Jun 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor Author

Superseded by #29520; same fix, branch renamed to the litellm_ prefix to follow the internal-contributor convention


Generated by Claude Code

@mateo-berri mateo-berri closed this Jun 2, 2026
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.

1 participant