Skip to content
Latchkey

How to Validate Airflow DAGs With an Import Test in CI

Loading every DAG through DagBag surfaces import errors that only appear when the scheduler parses the file.

Run a pytest that builds a DagBag from your dags folder and asserts import_errors is empty, catching parse failures before deploy.

Steps

  • Install apache-airflow matching your production version.
  • Point AIRFLOW__CORE__DAGS_FOLDER at your dags directory.
  • Assert DagBag().import_errors is empty in a test.
  • Run the test on every pull request.

Test

tests/test_dag_validation.py
from airflow.models import DagBag

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

def test_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} is missing tags"

Workflow

.github/workflows/ci.yml
on: [pull_request]
jobs:
  dags:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - run: pip install "apache-airflow==2.9.3" pytest
      - run: pytest tests/test_dag_validation.py -q

Gotchas

  • Pin the Airflow version to match production so provider imports resolve the same way.
  • A slow DAG top-level (network calls at parse time) will slow every scheduler loop; catch it here.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →