Sub topic 3
Smoke-Testing the Whole Pipeline and Wiring It into CI
This chapter assumes you can write pytest tests, open pull requests in Git, and train a scikit-learn model. It also assumes a repository with a run_pipeline.py script that reads data, trains a model, and writes outputs.
Smoke-testing on a small sample
A smoke test is a quick end-to-end run of the pipeline on a small input. It does not judge accuracy. It checks that every stage connects and that the expected outputs appear. Unit tests cover single functions, while smoke tests catch broken connections between stages.
Commit a few hundred rows to tests/data/sample.csv, then add this test:
import subprocess
def test_pipeline_smoke(tmp_path):
out_dir = tmp_path / "out"
subprocess.run(
["python", "run_pipeline.py",
"--data", "tests/data/sample.csv",
"--out", str(out_dir)],
check=True,
)
assert (out_dir / "model.pkl").exists()
assert (out_dir / "metrics.json").exists()Putting it together in a CI workflow
Run the cheap checks on every pull request, then the smoke test. Run the expensive model tests only on a schedule or on demand.
name: ml-ci
on:
pull_request:
push:
branches: [main]
schedule:
- cron: "0 3 * * *"
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
- run: pip install -r requirements.txt
- name: Fast tests
run: pytest tests/unit tests/data -q
- name: Smoke test
run: pytest tests/smoke -q
- name: Heavy model tests
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
run: pytest tests/model -qKeeping CI fast and affordable
Cache pip downloads, cancel superseded runs on the same branch, and keep the pull-request job short so developers keep using it. Move long training runs to the nightly schedule.
Versioning data, models, and metrics
Name each model artifact after the Git commit and a hash of the training data. Write metrics.json next to it so every result traces back to its inputs. Store the artifact in a registry or object store, not in Git.
From CI to continuous delivery and retraining
Only artifacts that pass the quality gate are promoted. The delivery job reads metrics.json and deploys the artifact it names. A scheduled job can also use the same metrics file to decide when retraining is needed.
:::
Exercise
Why does the smoke test use a small sample instead of the full dataset?
Show answer
It runs on every pull request, so it must finish in minutes. It only needs to prove that the stages connect, which a small input shows as well as a large one.