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
