Product
Testing Dagu workflows
dagu test runs a workflow's own steps for real and mocks the API calls around them.
TL;DR Dagu v2.28 adds dagu test. A test file next to a DAG runs the steps you choose for real and replaces the rest, such as API calls and deploys, with mocks. It then checks how the run ended and what the steps printed. Steps the test does not script take a default mock, so nothing outside runs unless you list it under real. In GitHub Actions, a failed case shows up on the pull request with its reason.
A Dagu workflow is a YAML file, but it holds code: shell commands, JavaScript in js.run steps, and rules for what happens when a step fails. Before dagu test, the only way to test that code was to run the workflow. A run calls the real APIs and sends the real notifications.
The workflow
A daily job fetches today's orders, writes a one-line summary, and posts it to a webhook. If a step fails, it pages on-call.
# workflows/sales.yaml
steps:
- id: fetch
run: curl -fsS https://api.example.com/orders/today
output: ORDERS
- id: summary
depends: fetch
action: js.run
with:
input: ${ORDERS}
script: |
const total = input.map((order) => order.amount).reduce((a, b) => a + b);
const today = new Date().toISOString().slice(0, 10);
return today + ": " + input.length + " orders, $" + total;
output: SUMMARY
- id: post
depends: summary
action: http.request
with:
method: POST
url: https://hooks.example.com/sales
body: '{"text": "${SUMMARY}"}'
handler_on:
failure:
run: ./page-oncall.sh
On November 30, with two orders, summary prints 2026-11-30: 2 orders, $200. That script is the code worth testing. fetch and post talk to other systems.
The test
# workflows/sales.test.yaml
real: [summary]
default_mock: {}
mocks:
fetch:
stdout: '[{"amount": 120}, {"amount": 80}]'
cases:
- name: sums up the day's orders
expect:
status: succeeded
steps:
summary:
stdout:
contains: "2 orders, $200"
$ dagu test workflows/
PASS workflows/sales.test.yaml: sums up the day's orders 0.38s
Cases: 1 passed
sales.test.yamlsits next tosales.yaml, as Go test files sit next to the code they test. The scheduler never runs it.real: [summary]runs the JavaScript.fetchis mocked. ItsstdoutbecomesORDERS, as curl's output would. No request is sent.default_mock: {}mocks every other step, herepostand the failure handler, so nothing is posted and nobody is paged.dagu test -vlists them. Start every test file with it: a step added to the DAG later then takes the default mock instead of breaking the test.- The summary starts with the date, which changes every day, so the test cannot compare the whole line.
containschecks the part that matters.
The bug
Add a day with no orders:
- name: a day without orders sums up to zero
mocks:
fetch:
stdout: '[]'
expect:
status: succeeded
steps:
summary:
stdout:
contains: "0 orders, $0"
handler_on.failure: not_started
$ dagu test workflows/
PASS workflows/sales.test.yaml: sums up the day's orders 0.33s
FAIL workflows/sales.test.yaml: a day without orders sums up to zero 0.26s
Run: expected status succeeded, got failed.
Step "summary": stdout does not contain "0 orders, $0".
Step "handler_on.failure": expected status not_started, got succeeded.
Cases: 1 passed, 1 failed
reduce on an empty array with no initial value throws. dagu test --keep keeps the case's logs, and the stderr of summary reads TypeError: No initial value. In production, the first day without orders would post nothing and page on-call. The fix is an initial value:
script: |
- const total = input.map((order) => order.amount).reduce((a, b) => a + b);
+ const total = input.reduce((sum, order) => sum + order.amount, 0);
The failure path
curl exits with 22 when the server returns an HTTP error:
- name: an API error pages on-call
mocks:
fetch:
exit_code: 22
expect:
status: failed
steps:
summary: aborted
post: aborted
handler_on.failure: succeeded
$ dagu test workflows/
PASS workflows/sales.test.yaml: sums up the day's orders 0.16s
PASS workflows/sales.test.yaml: a day without orders sums up to zero 0.15s
PASS workflows/sales.test.yaml: an API error pages on-call 0.11s
Cases: 3 passed
Nothing is posted, and the failure handler runs. No API had to go down.
What runs
A case runs through the same runner as dagu start. Dependencies, retries, continue_on, and handlers work as in a real run. Only the step actions differ:
| Step | Under test |
|---|---|
Listed under real |
Runs its real action |
router.route, outputs.write, log.write, noop |
Runs. These actions have no side effects |
| Any other step, with a mock | Returns the mock result. {} exits with 0 |
| Any other step, without a mock | Takes default_mock. In a test file without it, makes the case invalid, and no step runs |
Retry and repeat intervals are not waited for, except by real steps. mail_on notifications are not sent, and dagu test -v lists them. Each case keeps its run history and logs in its own temporary directory. The full list is in the reference.
Adding a step
Someone adds a Slack message after the post:
- id: notify
depends: post
action: http.request
with:
method: POST
url: https://hooks.slack.com/services/T000/B000/XXXX
body: '{"text": "${SUMMARY}"}'
The tests still pass, and nothing reaches Slack. -v shows why: notify takes the default mock.
$ dagu test -v workflows/
PASS workflows/sales.test.yaml: sums up the day's orders 0.23s
Default mock: post, notify, handler_on.failure.
PASS workflows/sales.test.yaml: a day without orders sums up to zero 0.24s
Default mock: post, notify, handler_on.failure.
PASS workflows/sales.test.yaml: an API error pages on-call 0.15s
Default mock: post, notify, handler_on.failure.
Cases: 3 passed
Mock notify yourself when a case depends on what it returns.
Without default_mock, the new step would make every case invalid, and Dagu would print the mock to add before any step runs. Either way, a step with side effects runs only when the test lists it under real. See the fail-closed rule.
Keeping tests current
dagu test --init workflows/sales.yamlwrites a first test file. It starts withdefault_mock: {}and one case that expects how that run ended. List the steps you want to run for real underreal, then add cases.- When you change the DAG on purpose, its tests fail, because they still expect the old behavior.
dagu test --updatereplaces the expected results in the test file with what the DAG does now. Check the change withgit diffbefore you commit it.
Running tests in GitHub Actions
Two steps run the tests: one installs Dagu, and one runs dagu test.
# .github/workflows/dagu-test.yaml
name: dagu-test
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: dagucloud/setup-dagu@v1 # installs Dagu
- run: dagu test workflows/ # fails the job when a case fails
dagu test exits with 1 when a case fails and 2 when a test file, a case, or a DAG is invalid, so the job fails with the test. The test above needs no interpreter, network access, or secrets on the runner: js.run runs in Dagu's embedded sandbox, and the other steps are mocked. Make the job a required check on main, and a DAG whose tests fail cannot be merged.
dagu test workflows/ checks every DAG that the server loads from workflows/, not only the ones with test files. A misspelled field, such as depnds for depends, fails the job, so CI needs no separate dagu validate step.
Show the failed case on the pull request
To see why a case failed without opening the job log, write a JUnit report and pass it to a JUnit reporter, such as action-junit-report. Let the workflow write checks:
permissions:
contents: read
checks: write
Then replace the dagu test step with these two:
- - run: dagu test workflows/
+ - run: dagu test --format junit workflows/ > dagu-test.xml
+ - uses: mikepenz/action-junit-report@v6
+ if: always()
+ with:
+ report_paths: dagu-test.xml
The check counts the cases and shows why each failed case failed. if: always() publishes the report even when the dagu test step fails. The whole workflow is in dagucloud/dagu-test-example, with the pull request in the screenshot. The GitHub Actions page also covers running DAGs on a schedule.
Coding agents
The Dagu skill covers test files. Install it with the GitHub CLI. The command asks which tool to install it for.
gh skill install dagucloud/dagu dagu
Ask the agent to write tests for a DAG and to run dagu test after each edit. A failed case shows the expected and actual values, and an invalid case prints its fix. The fail-closed rule applies to the agent's tests too.
Documentation
- Guide: https://docs.dagu.sh/writing-workflows/workflow-tests
- Reference: https://docs.dagu.sh/writing-workflows/workflow-test-reference
- Examples: https://docs.dagu.sh/writing-workflows/examples/testing
- Spec: https://github.com/dagucloud/dagu/blob/main/specs/101-workflow-tests.md
Report bugs on Discord, X, or GitHub issues.
Written by
Contributor to the Dagu project.
More from Yota Hamada