Skip to content

test: import litellm once at session start and repeat changed test files in CI - #1060

Merged
antonwolfy merged 3 commits into
intel:mainfrom
antonwolfy:test/flaky-prevention
Nov 22, 2026
Merged

antonwolfy merged 3 commits into
intel:mainfrom
antonwolfy:test/flaky-prevention

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Parts 2 and 3 of #1035 (flaky-test prevention); parts 1 and 4 and the optional scheduled run stay open as follow-ups.

Part What goes wrong on main
2, import-time warnings filterwarnings = ["error"] turns a third-party import-time warning into a failure of whichever test imports the module first on an xdist worker (one run of #635 failed about 20 tests in five files on a pydantic ReadOnly warning from litellm's lazy import). With a litellm stand-in that warns on import, a child pytest on main collects the suite with exit 0: nothing fails at session start, the warning waits for the first test that imports litellm.
3, catch a flaky test when it is added CI runs a changed or added test file once per PR, so an order- or load-dependent test passes review and flakes later (#882, #947 and #1007 are the growth test that flaked three times).

Part 2: tests/conftest.py imports litellm at the top, after the LITELLM_LOCAL_MODEL_COST_MAP setting. The warning filter from pyproject.toml is already active while conftest loads (checked with the stand-in below), so no fixture or hook is needed and the filter is untouched. litellm is the only module added: it is the one the tests import lazily, and importing it loads openai, pydantic, tiktoken and httpx with it; numpy and scipy are not loaded by it and pytest-randomly already loads numpy.

Part 3: the option taken is a small script, scripts/repeat_changed_tests.py, not pytest-repeat, so there is no new dependency and the file selection (added, modified or renamed tests/test_*.py against the PR base, deleted files excluded, none means skip, cap of 10) has its own tests. The new Repeat changed tests step in the test job runs it on pull requests, on the Python 3.14 leg that already has the full history. Each file set runs 20 times under pytest -n auto --randomly-seed=1..20, stops at the first failing seed and prints the command that replays it. Past 10 files the log lists the rest as run once, in the full suite.

Left to follow-ups, so closing #1035 loses nothing:

Closes #1035

Changes

  • tests/conftest.py: import litellm after the environment settings; the provider_calls fixture uses that module name instead of importing it again.
  • tests/test_conftest_preimport.py (new): a child pytest with the repo's config and conftest and a stand-in litellm first on PYTHONPATH; the stand-in calls warnings.warn when asked. The warning run exits non-zero and prints the stand-in's message; the quiet run exits 0.
  • scripts/repeat_changed_tests.py (new) and tests/test_repeat_changed_tests.py (new): changed_test_files reads git diff --diff-filter=AMR base...head and decodes the names with os.fsdecode, so a test file whose name is not UTF-8 (test_café.py in Latin-1 bytes) keeps its real path, and the log lines print such a name with the bytes escaped (test_caf\xe9.py) instead of raising; repeat runs the seeds and returns 0, 1 (first failing seed) or, from main, 2 when git cannot diff; --repeats and --max-files take a positive integer, and 0, a negative number or text is a usage error with exit 2 before git or pytest runs.
  • .github/workflows/ci.yml: the Repeat changed tests step, if: github.event_name == 'pull_request' && matrix.coverage, base passed through env like the neighbouring steps. make ci-local reads it unchanged and runs it.
  • CONTRIBUTING.md: the import and the step in the list of what CI holds a PR to.
  • No changelog fragment: only tests/, scripts/, .github/ and a doc change.

Risk

Every test session now imports litellm at start (about 3 s on the host), which tests were already doing on first use; pytest --collect-only over tests took 15.98 s on main and 13.47 s here (24203 and 24212 tests), so collection time is unchanged within noise. A future litellm release whose import warns now fails every session at conftest, by name, until a narrow ignore goes into pyproject.toml; that is the intent. The offline guard and the tests.outcomes.plugin loading are untouched.

The new step adds up to 20 runs of at most 10 test files on the 3.14 leg of a PR that changes tests (about 130 s for two small files on the host, 20 × 4 s plus start-up); a PR that changes no test file pays a second. A test that was already flaky fails the PR that touches it, not the one that introduced it. ci.yml and conftest.py are also touched by other open PRs (#958 and #1036 edit ci.yml); the step sits between Run tests and Coverage of changed lines, so a merge conflict would be textual only.

Testing

On a Linux review host, at 102496ab (on main 96fb1cf3):

  • make ci-local: 37 passed, 0 failed, 14 skipped (the pinned Claude Code install, test-latest and deploy jobs that do not run on a PR, intel-ci matrix jobs, conda-forge, fuzz, CLI snapshot packaging, semgrep). 24201 tests passed, 19 skipped in the full run; Repeat changed tests ran the two new test files over 20 seeds, all green; changed-line coverage reports 0 measurable lines (scripts/ and tests/ are outside --cov=dokimi).
  • On main's code, tests/test_conftest_preimport.py: 1 failed, 1 passed; the warning run exits 0 where it must not (assert 0 != 0). With the change both pass.
  • Mutants, each failing the test named: a conftest that breaks when litellm is quiet fails test_a_quiet_litellm_import_leaves_collection_green; for the script, --diff-filter=AM and a wrong path prefix fail the added/modified/renamed listing test, AMRD fails it on the deleted file, a regex that allows subdirectories fails test_only_test_files_directly_under_tests_count, an unconditional run fails test_no_changed_file_runs_nothing, an off-by-one in the seeds fails the all-pass test, continue for return 1 and a wrong replay seed fail test_the_first_failing_seed_stops_the_run_and_is_named, no cap fails test_files_past_the_cap_are_logged_and_not_repeated, and exit 3 for exit 2 fails test_a_bad_base_raises_so_main_can_exit_2. Replacing the positive-integer type with plain int fails test_a_limit_that_is_not_a_positive_integer_is_a_usage_error for 0 and -1 on both options (4 of its 6 cases fail; the two text cases are refused by int too). 12 of 12 fail.
  • A tracked test_caf<0xe9>.py: with errors="replace" (the code before this round) changed_test_files returned a path with U+FFFD and test_a_file_name_that_is_not_utf8_round_trips_to_the_path_on_disk fails; repeat with such a name and a plain ' '.join raised UnicodeEncodeError on the first log line, and test_a_file_name_that_is_not_utf8_is_logged_escaped fails on both that and a surrogateescape mutant of the log helper. Both pass now.
  • Not run: the other Python versions, a real CI runner (the new step runs here through make ci-local only), and the semgrep check (skipped here).

Open questions

None.

Revert

A plain revert restores the lazy litellm import in tests and removes the step and the script. Nothing is stored.

Checklist

  • Tests added/updated and pytest passes
  • ruff, mypy, and schema checks pass
  • Docs updated (or N/A) — see the docs-drift checklist in CONTRIBUTING.md
  • Self-reviewed per "Before opening a PR" in AGENTS.md
  • Each new raise/exit and test names the doc, contract or issue it traces to
  • No ad-hoc run artifacts committed
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml)

…les in CI (intel#1035)

Part 2 of intel#1035: an import-time warning of a third-party module (the pydantic ReadOnly one from litellm's lazy import) failed whichever test imported litellm first on an xdist worker, so one run failed about 20 tests in five files. tests/conftest.py now imports litellm after the LITELLM_LOCAL_MODEL_COST_MAP setting; the filter from pyproject.toml is active while conftest loads, so such a warning fails the session once, in conftest.py, under its own name. tests/test_conftest_preimport.py runs a child pytest with a stub litellm that warns and checks it.

Part 3: a pull request step, Repeat changed tests, runs the test files the PR adds, modifies or renames 20 times under -n auto with --randomly-seed 1 to 20, at most 10 files (scripts/repeat_changed_tests.py, tested in tests/test_repeat_changed_tests.py). It runs on the Python 3.14 leg only, stops at the first failing seed and prints it. No new dependency.

Parts 1 (growth margin) and 4 (sleeps) and the scheduled run on main are left to follow-ups.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@exai-bot

exai-bot commented Nov 20, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Review complete: 0 finding(s) across 6 selected item(s).

CI at 05ad004: 21 passing, 3 skipped.

Test notes (3)
  • dokimi/tests/test_conftest_preimport.py: dokimi/tests/test_conftest_preimport.py passes at the head and also at the merge base, where the code this pull request changes is still as it was. No test in it fails without the change, so none of them would notice if the change were undone.
  • dokimi/scripts/repeat_changed_tests.py:78: With if value < 1: changed to if value <= 1:, the tests still pass: dokimi/tests/test_conftest_preimport.py, dokimi/tests/test_repeat_changed_tests.py. A value exactly at the boundary gives the same result either way here, so no test says which one is meant.
  • dokimi/scripts/repeat_changed_tests.py:80: With return value changed to return None, the tests still pass: dokimi/tests/test_conftest_preimport.py, dokimi/tests/test_repeat_changed_tests.py. If this condition matters, a test should pin it.
Review details
  • Commit reviewed: 05ad004 against merge base 96fb1cf
  • Coverage: 6 files in 2 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 255k tokens (161k from cache), 52s
  • Tool calls: 28 (16 file_read, 7 code_search, 2 local_check_results, 1 ci_status, 1 file_find, 1 file_read_diff)
  • Changed-test repetition tooling, CI integration, and contributor guidance: 4 files
  • Test pre-import configuration and regression coverage: 2 files

Comment thread dokimi/scripts/repeat_changed_tests.py Resolved
…ax-files

Settles the exai-bot review thread on scripts/repeat_changed_tests.py: --repeats 0 returned 0 without running pytest, and --max-files 0 ran pytest with no file paths, which collects the whole suite for every seed.

Both options now take a positive integer through argparse, so 0, a negative number or text is a usage error with exit 2 before git or pytest runs. Tests cover both options.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@antonwolfy

Copy link
Copy Markdown
Contributor Author

Review points answered at 05ad004; 11 checks still pending (CodeQL (actions), Dependency review, External catalog smoke test (uxlfoundation/skills), Fidelity golden (claude-code 2.1.318), Linux Python 3.14, Linux Wheels Python 3.14, Mutation of changed lines, Package install smoke test (py3.11), Tests (py3.14), benchmark, mypy); every other check passed.

@codecov-commenter

codecov-commenter commented Nov 20, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.91%. Comparing base (96fb1cf) to head (102496a).
⚠️ Report is 47 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #1060    +/-   ##
========================================
  Coverage   95.91%   95.91%            
========================================
  Files         448      450     +2     
  Lines       59388    59527   +139     
  Branches    10238    10265    +27     
========================================
+ Hits        56962    57098   +136     
- Misses       1506     1507     +1     
- Partials      920      922     +2     

see 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@napetrov napetrov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change class: CONTRACT (head 05ad004)

  • Contract: changed. cli dokimi/scripts/repeat_changed_tests.py
  • Responsibility: none
  • Dependency: none
  • Review depth: check the changed surface: callers, docs, tests pin new behaviour
  • Coverage gaps: none

Checked pytest startup, changed-file selection and seed replay. I left 1 inline comment.

Focused pytest run on 05ad0042a: 15 passed in 4.62s. CI on head 05ad004: 0 failing Actions jobs.

Comment thread dokimi/scripts/repeat_changed_tests.py Resolved
Settles the review comment on changed_test_files: a tracked test_*.py whose name held the byte 0xe9 came back with U+FFFD, a path that does not exist, so the repeat step ran the wrong file.

The names git prints are now decoded with os.fsdecode, so they round-trip to the path on disk. Such a name would then have raised UnicodeEncodeError in the log lines, so they print with the non-UTF-8 bytes escaped.

Tests: a POSIX file named test_caf<0xe9>.py is listed by its real name, and the log lines escape it.
Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) ### Change class: CONTRACT (head 05ad004)

@napetrov (#1060 (review)): Review body recapping the checks; its one inline comment is the thread above, answered there.

@antonwolfy
antonwolfy merged commit ec016f4 into intel:main Nov 22, 2026
31 of 32 checks passed
@antonwolfy
antonwolfy deleted the test/flaky-prevention branch November 22, 2026 12:44
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.

Flaky tests: growth-test margin, import-time warnings, repeat changed tests in CI

4 participants