Search code examples
dbt

Can dbt tests be dependent on one another?


I was wondering if we can create tests depending on each other. For example, if the first fails, run the second on and so on and skip downwards tests wherever the tests succeed.

The goal would be to save CPU consumption on superfluous tests if the major one passes.

Any idea on how to achieve something similar? or how to build a test with these criteria?


Solution

  • dbt has many ways to select subsets of tasks. the docs give a good selection of test selection examples.

    My advice would be to group your tests into distinct phases using either tags or YAML selectors. You might even get away with having your phase one tests be --schema and phase two be --data.

    In your orchestrator (Jenkins, Circle CI, GitHub Actions) you'd add your control flow (if-then) logic. Below is a hacky bash script example.

    echo 'phase one: running'
    if dbt test --tag phase_one; then
        echo 'phase one: passed'
    else
        echo 'phase one: failed'
        echo 'phase two: running,
        if dbt test --tag phase_two; then
            echo 'phase two: passed'
        else
            echo 'phase two: failed'
        fi
    fi