Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Apache Airflow tasks that uses XCom

I'm trying to figure out a way to test a DAG where I have a couple of tasks communicating using XCom.

Since the console command only allow me to run tasks from a DAG, is there a way to test the communication without having to run the DAG via the UI?

Thanks

like image 650
Bruno Faria Avatar asked Feb 26 '17 22:02

Bruno Faria


People also ask

What is XCom variable in Airflow?

What is an Airflow XCom ? XCom stands for “cross-communication” and allows to exchange messages or small amount of data between tasks. You can think of an XCom as a little object with the following fields: that is stored IN the metadata database of Airflow.

How do you use an environment variable in Airflow?

Storing Variables in Environment Variables Airflow Variables can also be created and managed using Environment Variables. The environment variable naming convention is AIRFLOW_VAR_{VARIABLE_NAME} , all uppercase. So if your variable key is FOO then the variable name should be AIRFLOW_VAR_FOO .


1 Answers

Here's a way that worked for me.

Even though the Airflow web page states that the test command does not generate or keeps any state, running the airflow test command in sequence worked.

Basically you do:

Airflow test my_dag task1 date
Airflow test my_dag task2 date

where task1 returns the value or uses xcom_push method to send the value and then task2 run xcom_pull to get the value and move on.

It worked for me. Please comment if you have different approaches or other questions.

like image 197
Bruno Faria Avatar answered Nov 02 '22 19:11

Bruno Faria