Testing Examples
Each example is a DAG and its test file. Save both in workflows/ and run dagu test workflows/. Steps under real run their commands. Mocked steps run no commands, so the scripts they name do not have to exist. See Workflow Tests for how tests work.
Most of these tests check only the part of a command, request body, or log that the case is about, so they keep passing when the other parts change.
Run a Step for Real
# workflows/sales.yaml
steps:
- id: fetch
run: curl -fsS https://api.example.com/orders/today
output: ORDERS
- id: total
depends: fetch
action: js.run
with:
input: ${ORDERS}
script: |
return input.reduce((sum, order) => sum + order.amount, 0);
output: TOTAL
- id: post
depends: total
action: http.request
with:
method: POST
url: https://hooks.example.com/sales
body: '{"total": ${TOTAL}}'# workflows/sales.test.yaml
real: [total]
mocks:
fetch:
stdout: '[{"amount": 120}, {"amount": 80}]'
post: {}
cases:
- name: posts the day's total
expect:
steps:
post:
called_with:
with:
body:
total: 200
- name: a day without orders posts zero
mocks:
fetch:
stdout: '[]'
expect:
status: succeeded
steps:
post:
called_with:
with:
body:
total: 0total runs its real script on the mocked orders. fetch and post are mocked, so no request is sent. The mocked stdout of fetch becomes ORDERS. A map under body reads the body as JSON and checks only the keys it lists, so the test keeps passing when the body gains a field. Without the initial value 0, both cases fail: the first posts {"total": [object Object]80}, and in the second, reduce throws on the empty array.
Check What a Step Prints
# workflows/summary.yaml
steps:
- id: count
run: ./count.sh
outputs:
- name: files
- id: summary
depends: count
run: echo "${steps.count.outputs.files} files changed"# workflows/summary.test.yaml
real: [summary]
mocks:
count:
outputs:
files: "3"
cases:
- name: the summary reports the count
expect:
steps:
summary:
stdout:
contains: 3 files changedsummary runs for real on the mocked count. stdout takes contains, matches, or both, so the test keeps passing when the step prints more lines. A mocked step prints only the stdout of its mock, so check the stdout of steps that run for real.
Alert When the Build Fails
# workflows/build.yaml
steps:
- id: build
run: make build
handler_on:
failure:
run: ./notify.sh "build failed"# workflows/build.test.yaml
mocks:
build: {}
handler_on.failure: {}
cases:
- name: a passing build sends no alert
expect:
status: succeeded
steps:
handler_on.failure: not_started
- name: a failed build sends an alert
mocks:
build:
exit_code: 1
expect:
status: failed
steps:
handler_on.failure: succeededA test mocks and checks a handler by its key, handler_on.failure. not_started means the handler never ran.
Succeed After Retries
# workflows/fetch.yaml
steps:
- id: fetch
run: curl -fsS https://api.example.com/data
retry_policy:
limit: 3
interval_sec: 30# workflows/fetch.test.yaml
cases:
- name: succeeds on the third try
mocks:
fetch:
results:
- exit_code: 1
- exit_code: 1
- exit_code: 0
expect:
steps:
fetch:
status: succeeded
attempts: 3
- name: fails after three retries
mocks:
fetch:
exit_code: 1
expect:
status: failed
steps:
fetch:
attempts: 4results gives each attempt its result in turn. The test does not wait 30 seconds between retries. limit: 3 allows three retries, so a step that keeps failing runs four times.
Tag Only Production Releases
# workflows/deploy.yaml
params:
- name: env
default: staging
steps:
- id: deploy
run: ./deploy.sh ${params.env}
- id: tag_release
depends: deploy
preconditions:
- condition: ${params.env}
expected: prod
run: git tag release# workflows/deploy.test.yaml
mocks:
deploy: {}
tag_release: {}
cases:
- name: staging skips the release tag
expect:
steps:
tag_release: skipped
- name: prod tags the release
params:
env: prod
expect:
steps:
deploy:
called_with:
run: ./deploy.sh prod
tag_release: succeededA case sets its own params. The precondition compares values, so it is checked as in a real run. called_with compares the whole command deploy would have run, because all of it matters here: contains: prod would also pass ./deploy.sh preprod.
Route on a Step's Output
# workflows/triage.yaml
steps:
- id: check
run: ./check.sh
outputs:
- name: severity
- id: route
depends: check
action: router.route
with:
value: ${steps.check.outputs.severity}
routes:
"high": [page_oncall]
"low": [open_ticket]
- id: page_oncall
run: ./page.sh
- id: open_ticket
run: ./ticket.sh# workflows/triage.test.yaml
mocks:
page_oncall: {}
open_ticket: {}
cases:
- name: high severity pages on-call
mocks:
check:
outputs:
severity: high
expect:
steps:
page_oncall: succeeded
open_ticket: skipped
- name: low severity opens a ticket
mocks:
check:
outputs:
severity: low
expect:
steps:
page_oncall: skipped
open_ticket: succeededThe router has no side effects, so it runs for real on the mocked severity and needs no mock. A step the router does not pick ends skipped.
Mock a Sub-DAG
# workflows/nightly.yaml
params:
- name: date
required: true
steps:
- id: etl
action: dag.run
with:
dag: etl
params:
date: ${params.date}
- id: report
depends: etl
run: ./report.sh# workflows/nightly.test.yaml
params:
date: 2026-12-01
mocks:
etl: {}
report: {}
cases:
- name: passes the date to the ETL run
expect:
steps:
etl:
called_with:
with:
params:
date: 2026-12-01
report: succeeded
- name: a failed ETL run stops the report
mocks:
etl:
exit_code: 1
expect:
status: failed
steps:
report: abortedThe mock replaces the child run, so the etl DAG does not run and does not have to exist. called_with checks the params the child would have received.
Continue After an Optional Step Fails
# workflows/cleanup.yaml
steps:
- id: clear_cache
run: ./clear-cache.sh
continue_on: failed
- id: backup
depends: clear_cache
run: ./backup.sh# workflows/cleanup.test.yaml
mocks:
backup: {}
cases:
- name: a failed cache clear does not stop the backup
mocks:
clear_cache:
exit_code: 1
expect:
status: partially_succeeded
steps:
clear_cache: failed
backup: succeededA mocked step keeps its continue_on. The run ends partially_succeeded because one step failed.
Load the Previous Day
# workflows/daily-load.yaml
schedule: "0 2 * * *"
dates:
yesterday: -1d
steps:
- id: load
run: ./load.sh ${dates.yesterday}# workflows/daily-load.test.yaml
mocks:
load: {}
cases:
- name: the March 1 run loads February 28
scheduled_at: 2026-03-01
expect:
steps:
load:
called_with:
run:
contains: 2026-02-28scheduled_at runs the case as the run of the March 1 slot. contains checks only the date, so the test keeps passing when load.sh gains a flag. Add a case for each date the schedule must get right, such as the first day of a month or a year.

