On September 15, 2026
, all Cloud Composer 1 versions and versions 2.0.x of Cloud Composer 2 will reach their planned end of life
. You will not be able to use environments with these versions. We recommend planning migration to Cloud Composer 3
. Cloud Composer 2 versions 2.1.x and later are still supported and are not impacted by this change.
DAG unit testing
Stay organized with collections
Save and categorize content based on your preferences.
A sample unit test for a Python DAG.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code demonstrates how to create a unit test for a Python Directed Acyclic Graph (DAG) in Airflow.\u003c/p\u003e\n"],["\u003cp\u003eThe test validates that a module contains a valid DAG and detects any task cycles within it using the \u003ccode\u003etest_cycle\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eApplication Default Credentials are required to authenticate to Cloud Composer.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Cloud sample browser can be used to discover more examples.\u003c/p\u003e\n"]]],[],null,["A sample unit test for a Python DAG.\n\nCode sample \n\nPython\n\n\nTo authenticate to Cloud Composer, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from airflow import models\n from airflow.utils.dag_cycle_tester import test_cycle\n\n\n def assert_has_valid_dag(module):\n \"\"\"Assert that a module contains a valid DAG.\"\"\"\n\n no_dag_found = True\n\n for dag in vars(module).values():\n if isinstance(dag, models.DAG):\n no_dag_found = False\n test_cycle(dag) # Throws if a task cycle is found.\n\n if no_dag_found:\n raise AssertionError(\"module does not contain a valid DAG\")\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=composer)."]]