Skip to content

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 ​

yaml
# 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}}'
yaml
# 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: 0

total 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.

Learn more →

Check What a Step Prints ​

yaml
# workflows/summary.yaml
steps:
  - id: count
    run: ./count.sh
    outputs:
      - name: files
  - id: summary
    depends: count
    run: echo "${steps.count.outputs.files} files changed"
yaml
# 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 changed

summary 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.

Learn more →

Alert When the Build Fails ​

yaml
# workflows/build.yaml
steps:
  - id: build
    run: make build
handler_on:
  failure:
    run: ./notify.sh "build failed"
yaml
# 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: succeeded

A test mocks and checks a handler by its key, handler_on.failure. not_started means the handler never ran.

Learn more →

Succeed After Retries ​

yaml
# workflows/fetch.yaml
steps:
  - id: fetch
    run: curl -fsS https://api.example.com/data
    retry_policy:
      limit: 3
      interval_sec: 30
yaml
# 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: 4

results 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.

Learn more →

Tag Only Production Releases ​

yaml
# 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
yaml
# 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: succeeded

A 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.

Learn more →

Route on a Step's Output ​

yaml
# 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
yaml
# 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: succeeded

The 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.

Learn more →

Mock a Sub-DAG ​

yaml
# 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
yaml
# 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: aborted

The 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.

Learn more →

Continue After an Optional Step Fails ​

yaml
# workflows/cleanup.yaml
steps:
  - id: clear_cache
    run: ./clear-cache.sh
    continue_on: failed
  - id: backup
    depends: clear_cache
    run: ./backup.sh
yaml
# 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: succeeded

A mocked step keeps its continue_on. The run ends partially_succeeded because one step failed.

Learn more →

Load the Previous Day ​

yaml
# workflows/daily-load.yaml
schedule: "0 2 * * *"
dates:
  yesterday: -1d
steps:
  - id: load
    run: ./load.sh ${dates.yesterday}
yaml
# 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-28

scheduled_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.

Learn more →

Dagu is open source under the GNU General Public License v3.0.