DAG unit testing

A sample unit test for a Python DAG.

Code sample

Python

from airflow import models


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
            dag.test_cycle()  # 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