0
0
AirflowHow-ToBeginner ยท 3 min read

How to Use airflow test Command for Task Debugging

Use the airflow test command to run a single task instance of a DAG for a specific execution date without changing the DAG's state. The syntax is airflow test DAG_ID TASK_ID EXECUTION_DATE, which helps debug tasks quickly.
๐Ÿ“

Syntax

The airflow test command runs a single task instance for a given DAG and execution date without updating the task state in the Airflow metadata database.

  • DAG_ID: The ID of the DAG containing the task.
  • TASK_ID: The ID of the task to run.
  • EXECUTION_DATE: The logical date for the task run in YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS format.
bash
airflow test DAG_ID TASK_ID EXECUTION_DATE
๐Ÿ’ป

Example

This example runs the task print_date from the DAG example_bash_operator for the execution date 2023-06-01. It helps verify the task runs correctly without affecting the DAG's state.

bash
airflow test example_bash_operator print_date 2023-06-01
Output
Running <TaskInstance: example_bash_operator.print_date 2023-06-01T00:00:00+00:00 [running]>\n[2023-06-01 00:00:00,000] {{ taskinstance.py:xxxx }} INFO - Executing command: date\n[2023-06-01 00:00:00,000] {{ taskinstance.py:xxxx }} INFO - Output:\nThu Jun 1 00:00:00 UTC 2023\n[2023-06-01 00:00:00,000] {{ taskinstance.py:xxxx }} INFO - Task exited with return code 0
โš ๏ธ

Common Pitfalls

1. Misunderstanding state changes: airflow test does not update task or DAG run states, so it won't mark tasks as success or failure in the UI.

2. Wrong execution date format: Use ISO format YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS. Incorrect formats cause errors.

3. Running tasks with dependencies: airflow test runs only the specified task without upstream or downstream tasks, so it may fail if dependencies are required.

bash
Wrong usage example (missing execution date):
airflow test example_bash_operator print_date

Correct usage:
airflow test example_bash_operator print_date 2023-06-01
๐Ÿ“Š

Quick Reference

Tips for using airflow test:

  • Use it to debug individual tasks quickly without affecting DAG state.
  • Always provide a valid execution date in ISO format.
  • Remember it does not run task dependencies.
  • Check task logs for detailed output after running.
โœ…

Key Takeaways

Use airflow test DAG_ID TASK_ID EXECUTION_DATE to run a single task instance without changing DAG state.
Provide the execution date in ISO format like YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.
airflow test does not run upstream or downstream tasks, only the specified task.
It is ideal for debugging tasks quickly without affecting the Airflow UI state.
Check task logs after running to see detailed output and errors.