Airflow Testing DAGs

Testing proves that your pipeline works before it touches real data. A small set of automatic checks catches typos, broken imports, and wrong dependencies in seconds. Teams that test their DAGs ship changes with confidence and avoid failures at midnight.

The Car Inspection Picture

A car goes through inspection before it enters the road. Inspectors check brakes, lights, and tires one by one. DAG testing follows the same idea. Each check looks at one part, and a failed check stops the release.

Level 1: Does the file load?            → DAG integrity test
Level 2: Is the structure correct?      → DAG structure test
Level 3: Does each function work?       → Unit test
Level 4: Does the whole DAG run?        → dag.test() / airflow dags test

Level 1: DAG Integrity Test

An integrity test loads all DAG files and reports import errors. A single syntax error would hide a DAG from the UI, so this check catches the most common problem.

from airflow.models import DagBag

def test_no_import_errors():
    dag_bag = DagBag(dag_folder="dags/", include_examples=False)
    assert dag_bag.import_errors == {}, f"Errors found: {dag_bag.import_errors}"

Level 2: Structure Test

A structure test confirms the DAG contains the tasks and links you expect.

def test_tea_shop_structure():
    dag_bag = DagBag(dag_folder="dags/", include_examples=False)
    dag = dag_bag.get_dag("tea_shop")

    assert dag is not None
    assert len(dag.tasks) == 3

    brew = dag.get_task("brew_tea")
    assert "collect_orders" in brew.upstream_task_ids
    assert "serve_tea" in brew.downstream_task_ids

Rules Worth Testing Across All DAGs

def test_all_dags_have_owner_and_tags():
    dag_bag = DagBag(dag_folder="dags/", include_examples=False)
    for dag_id, dag in dag_bag.dags.items():
        assert dag.tags, f"{dag_id} has no tags"
        assert dag.default_args.get("owner"), f"{dag_id} has no owner"

Rules like these enforce team standards without manual review.

Level 3: Unit Tests for Task Logic

Keep business logic in plain Python functions. Plain functions test easily, since they need no Airflow objects.

# dags/helpers.py
def calculate_total(prices):
    return round(sum(prices), 2)

# tests/test_helpers.py
from dags.helpers import calculate_total

def test_calculate_total():
    assert calculate_total([1.10, 2.20, 3.30]) == 6.6

def test_calculate_total_empty():
    assert calculate_total([]) == 0

Mock Outside Systems

Tests should never call a real database or API. Replace the outside call with a fake.

from unittest.mock import patch
from dags.helpers import fetch_orders

@patch("dags.helpers.requests.get")
def test_fetch_orders(mock_get):
    mock_get.return_value.json.return_value = [{"id": 1}]
    assert fetch_orders() == [{"id": 1}]

Level 4: Run the Whole DAG

From Python

if __name__ == "__main__":
    dag.test()

Run the file with python my_dag.py. The call executes every task in one process and prints the logs. A debugger works here, so you can set breakpoints and step through the code.

From the CLI

airflow dags test tea_shop 2024-01-15
airflow tasks test tea_shop brew_tea 2024-01-15

Running Tests with pytest

pip install pytest
pytest tests/ -v

Pytest finds every function whose name starts with test_ and prints a pass or fail line for each.

Testing in a CI Pipeline

 Developer pushes code
          │
          ▼
 ┌──────────────────┐
 │ CI server runs   │
 │ pytest tests/    │
 └────────┬─────────┘
          │
     pass │ fail
          ▼        ▼
   Deploy DAGs   Block release

Test Types at a Glance

TestSpeedCatches
IntegrityVery fastSyntax and import errors
StructureVery fastWrong or missing links
UnitFastLogic bugs in functions
Full DAG runSlowerIntegration problems

Good Habits

  • Run the integrity test on every code change.
  • Separate logic from Airflow code so unit tests stay simple.
  • Use a test database and test connections, never production ones.
  • Add a test each time a bug appears, so the bug cannot return.

Key Takeaways

  • Integrity tests catch broken files in seconds.
  • Plain Python functions make unit testing easy.
  • dag.test() and airflow dags test run a full DAG locally.
  • Automatic tests in CI protect production.