DAG unit testing

A sample unit test for a Python DAG.

Code sample


from airflow import models
from airflow.utils.dag_cycle_tester import test_cycle

def assert_has_valid_dag(module):
    """Assert that a module contains a valid DAG."""

    no_dag_found = True

    for dag in vars(module).values():
        if isinstance(dag, models.DAG):
            no_dag_found = False
            test_cycle(dag)  # Throws if a task cycle is found.

    if no_dag_found:
        raise AssertionError('module does not contain a valid DAG')

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.