Repository navigation
test: import litellm once at session start and repeat changed test files in CI - #1060
Conversation
…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>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 6 selected item(s). CI at Test notes (3)
Review details
|
…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>
|
Review points answered at |
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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 🚀 New features to boost your workflow:
|
napetrov
left a comment
There was a problem hiding this comment.
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.
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>
@napetrov (#1060 (review)): Review body recapping the checks; its one inline comment is the thread above, answered there. |
Summary
Parts 2 and 3 of #1035 (flaky-test prevention); parts 1 and 4 and the optional scheduled run stay open as follow-ups.
mainfilterwarnings = ["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 pydanticReadOnlywarning from litellm's lazy import). With a litellm stand-in that warns on import, a child pytest onmaincollects the suite with exit 0: nothing fails at session start, the warning waits for the first test that imports litellm.Part 2:
tests/conftest.pyimports litellm at the top, after theLITELLM_LOCAL_MODEL_COST_MAPsetting. The warning filter frompyproject.tomlis 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 renamedtests/test_*.pyagainst the PR base, deleted files excluded, none means skip, cap of 10) has its own tests. The newRepeat changed testsstep in thetestjob runs it on pull requests, on the Python 3.14 leg that already has the full history. Each file set runs 20 times underpytest -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:
tests/waiting.py: Tests: move the remaining fixed sleeps to tests/waiting.py #1058.mainunder several seeds: CI: scheduled whole-suite run on main under several randomly seeds #1059.Closes #1035
Changes
tests/conftest.py:import litellmafter the environment settings; theprovider_callsfixture 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-inlitellmfirst onPYTHONPATH; the stand-in callswarnings.warnwhen 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) andtests/test_repeat_changed_tests.py(new):changed_test_filesreadsgit diff --diff-filter=AMR base...headand decodes the names withos.fsdecode, so a test file whose name is not UTF-8 (test_café.pyin 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;repeatruns the seeds and returns 0, 1 (first failing seed) or, frommain, 2 when git cannot diff;--repeatsand--max-filestake 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: theRepeat changed testsstep,if: github.event_name == 'pull_request' && matrix.coverage, base passed throughenvlike the neighbouring steps.make ci-localreads it unchanged and runs it.CONTRIBUTING.md: the import and the step in the list of what CI holds a PR to.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-onlyoverteststook 15.98 s onmainand 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 intopyproject.toml; that is the intent. The offline guard and thetests.outcomes.pluginloading 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.ymlandconftest.pyare also touched by other open PRs (#958 and #1036 editci.yml); the step sits betweenRun testsandCoverage of changed lines, so a merge conflict would be textual only.Testing
On a Linux review host, at
102496ab(onmain96fb1cf3):make ci-local: 37 passed, 0 failed, 14 skipped (the pinned Claude Code install,test-latestand deploy jobs that do not run on a PR,intel-cimatrix jobs, conda-forge, fuzz, CLI snapshot packaging, semgrep). 24201 tests passed, 19 skipped in the full run;Repeat changed testsran the two new test files over 20 seeds, all green; changed-line coverage reports 0 measurable lines (scripts/andtests/are outside--cov=dokimi).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.test_a_quiet_litellm_import_leaves_collection_green; for the script,--diff-filter=AMand a wrong path prefix fail the added/modified/renamed listing test,AMRDfails it on the deleted file, a regex that allows subdirectories failstest_only_test_files_directly_under_tests_count, an unconditional run failstest_no_changed_file_runs_nothing, an off-by-one in the seeds fails the all-pass test,continueforreturn 1and a wrong replay seed failtest_the_first_failing_seed_stops_the_run_and_is_named, no cap failstest_files_past_the_cap_are_logged_and_not_repeated, and exit 3 for exit 2 failstest_a_bad_base_raises_so_main_can_exit_2. Replacing the positive-integer type with plainintfailstest_a_limit_that_is_not_a_positive_integer_is_a_usage_errorfor0and-1on both options (4 of its 6 cases fail; the two text cases are refused byinttoo). 12 of 12 fail.test_caf<0xe9>.py: witherrors="replace"(the code before this round)changed_test_filesreturned a path with U+FFFD andtest_a_file_name_that_is_not_utf8_round_trips_to_the_path_on_diskfails;repeatwith such a name and a plain' '.joinraisedUnicodeEncodeErroron the first log line, andtest_a_file_name_that_is_not_utf8_is_logged_escapedfails on both that and asurrogateescapemutant of the log helper. Both pass now.make ci-localonly), 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
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)