liveBook · Manning

Dag.test

As explained in Data Pipelines with Apache Airflow, Second Edition

dag.test is an Airflow DAG method that executes a DAG in a local test context to simulate a DAG run.

dag.test() executes an entire Airflow DAG in a local test context, helping validate workflows when development cannot reproduce production data or infrastructure. section 10.4 section 10.4.1

Use dag.test() to:

  • Run the DAG programmatically instead of merely parsing the Python file and creating task objects. section 10.4.1
  • Test interactions across the whole workflow, including effects from additional tasks that modify PostgreSQL. section 10.4.1
  • Keep testing logic separate by calling dag.test() from a test rather than embedding mocks and assertions in the DAG file. section 10.4.1

Minimal local run

Add the call at the bottom of a DAG file so running that file invokes the workflow: if __name__ == "__main__": dag.test(). A separate test can instead import a variable-style DAG definition such as dagtestdag = DAG(...), then execute the complete workflow under test infrastructure.

Listing 10.29 Adding dag.test()
if __name__ == "__main__":
    dag.test()

View the minimal code pattern that turns direct DAG-file execution into a local test run. section 10.4.1

How Data Pipelines with Apache Airflow, Second Edition covers it

How deep it goes
Explains invocation, test placement, and whole-DAG scope, while noting external-system challenges and Airflow 3.1 serialization problems.
Examples
Uses a minimal dag.test() entry point and a Movielens-to-PostgreSQL DAG tested with pytest and a PostgreSQL container.
What you can do after
Diagnose workflow-level issues by executing and validating a complete DAG rather than only individual operators.
How it connects
Connects DAG testing with unit testing, integration testing, mocking, external systems, PostgreSQL, and CI/CD pipelines.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Pipelines with Apache Airflow, Second Edition. The section links point to the parts of the book it draws on.