# Testing DAGs For a DAG author on a laptop or in CI, with `pip install schedr` (the wheel includes the binary) and no cluster. Four levels, from cheapest to fullest. - **Static check: `schedr dags check `** (see Upgrades and compatibility): parses DAG files and `schedr.toml` files, reports import errors and parse warnings; non-zero exit code on errors. - **How it works (1.15a):** `` is a directory (a DAG root named after it, scanned with its `.schedrignore` like a configured root) or one file (checked alone, its directory on `sys.path`). The files are parsed by the **real runner** (`python -m schedr.runner parse`, the `[dags] interpreter`) and then go through **the master's own checks** (the same `validate_files`: cron fields, schedules, task references, ...), so a DAG that passes here isn't rejected by the master. One line per file (`ok sync.py: sync (2 tasks)` or `error broken.py: ModuleNotFoundError: ...` with its traceback), then a summary; `--json` gives `{"files": [{path, dags, error}]}`. Exit code 1 if any file has an error. - **Tracebacks start at the user's code:** the runner drops its own and `importlib`'s leading frames from import errors (in `dags check`, the UI and the DB alike), so the first frame shown is the DAG file's. - **Local version vs the workers (2.12b):** a DAG written with a newer local `schedr` than a worker may use a feature that worker doesn't have (an import error in the UI, never silent). Pinning the local `schedr` to the worker image (`schedr==1.4.*` in the DAG repository's dev dependencies) remains how you write against that version. `schedr dags check --against ` reads `GET /api/v1/workers` after the file report and **fails** (exit 1) when this schedr is newer, as `major.minor`, than any **active** worker. A worker drained for maintenance stays `active` and counts; `draining`, `disconnected`, `offline` and `lost` do not take new tasks. The patch is ignored, as everywhere else. The error names the older ones, grouped by version, oldest first: `this schedr is 0.3; older active workers at http://sched.example: 0.1: w3; 0.2: w1, w2` (the CLI prints it with the `error:` prefix). A pass prints one line after the file report: `ok: this schedr is 0.3; 2 active workers at https://prod run 0.3 (w1, w2)` (one worker says `runs`; several versions are oldest first). A version that does not parse is unknown. Dispatch stores `parse().ok()` (`None`) and `Worker::runs` still offers that worker tasks. The check follows that rule: the worker is listed and does not fail the check, and the passing line names the text as `unknown version`. No active worker: `no active worker at to compare with this schedr (0.3): workers are recorded disconnected right after a master restart until they register again, so retry in a minute`. The server unreachable, or refusing the call, is that error after the file report; a different major is the API's existing refusal (`Schedr-Client-Version`). The call is `connect_to`: the same credentials as the other commands, a 5 s connect timeout and a 30 s request timeout (a handshake that never completes stops). `--token` / `--user` (and `SCHEDR_TOKEN`, `SCHEDR_USER`, `SCHEDR_PASSWORD`) are sent; `--server` is not the URL. `--json` adds `version_check: {local, ok, message, workers: [{worker_id, schedr_version}]}` when the list was read (`workers` is every active worker, the version text as reported; `message` is the `ok:` or `error:` line). A worker on the same or a newer minor does not fail the check. - **Not chosen:** failing only when every active worker is older (one current worker would hide a leftover old one, and that old one still takes tasks); exit 0 with a `warning:` line (a green check deploys onto workers that queue the DAG); a `warning:` line as well as exit 1 (two lines for one fact); ignoring drained workers (resuming one is then a surprise); filtering by the queues the checked files name (a queue added later is not covered); failing before the file report when the server cannot be asked; comparing the patch (patches add no features; see Upgrades and compatibility); treating an unreadable version as its own older group (dispatch stores it as unknown and still assigns the worker); a silent pass (the `ok:` line names the workers that were compared); a separate HTTP client for this flag (the shared `connect_to` keeps the credentials and the timeouts). - **Unit tests of task functions.** Outside `with Dag(...)` a `@task` function is **the plain function**: calling it runs it and returns the real value (inside a DAG the same call returns an `Output[...]`). For code reading the run context or registering handles, **`schedr.testing.task_context(...)`** sets `dag_ctx` (`planned_for`, `since`, `params`, …), declared secrets and a handle collector; pure Python, no processes: ```python with task_context(planned_for=datetime(2026, 10, 15, 3, tzinfo=UTC), params=ReportParams(region="us"), secrets={"etl/db#password": "x"}) as ctx: export(date(2026, 10, 15)) assert ctx.handles == [TempDir(...), ExportJob("j-1")] ``` Outside a task and outside `task_context`, `dag_ctx` / `cleanup_handle` fail as decided ("must be run inside a schedr task"). - **How it works (1.15b):** `task_context(planned_for=, since=, dag_id=, run_id=, kind=)` is a context manager setting the same context variable the runner sets while it calls a task; it yields those values. Defaults: `planned_for` now (UTC), `since` = `planned_for` (an empty window, as for a run with nothing to go back to), `dag_id` `test`, a new UUIDv7 as `run_id`, kind `manual`. Times must be aware, `since` not after `planned_for`, `kind` a run kind. `params=` and `secrets=` raise `NotImplementedError` naming milestone M4 (like `dag_ctx.params`); the handle collector comes with handles (M4). - **`dag_ctx` fails everywhere else (1.15b):** a field read outside a running task and outside `with Dag(...)` raises `RuntimeError` ("`dag_ctx` must be run inside a schedr task ...; in tests, set values with `schedr.testing.task_context(...)`"), e.g. a module- level `PLANNED = dag_ctx.run.planned_for` or a plain call of a task reading it. Inside `with Dag(...)` it gives references, while a task runs (or in `task_context`) values. - **One whole run: `schedr dags test [--for