DAG unit testing

A sample unit test for a Python DAG.

Code sample

Python

To authenticate to Cloud Composer, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  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 .

Create a Mobile Website
View Site in Mobile | Classic
Share by: