Skip to main content

How to Run a Dag in One Python Process with dag.test() in Apache Airflow®

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

dag.test() runs a whole Apache Airflow® Dag once, in the same Python process, with no scheduler, Dag processor or UI running: you start it with python, and every task's output appears in the terminal. This guide runs a two-task Dag with dag.test() on Airflow 3.4.0.

Syntax​

Syntax
<dag variable> = <dag function>()

if __name__ == "__main__":
<dag variable>.test()
PartWhat it means
<dag function>The function decorated with @dag. Calling it creates the Dag
<dag variable>A name for the Dag the call creates. Create it at the top level of the file, outside the if block, so Airflow also finds the Dag when it reads the file itself
if __name__ == "__main__":Runs the line under it only when the file is run with python, not when Airflow imports the file
.test()Runs the Dag once: Airflow stores the Dag in its database, creates a run, and runs every task in order, in this process

.test() also takes optional arguments. Airflow 3.4.0's docstring for it lists, among others:

ArgumentWhat the docstring says
logical_date"logical date for the Dag run"
run_conf"configuration to pass to newly created dagrun"
use_executor"if set, uses an executor to test the Dag"
mark_success_pattern"regex of task_ids to mark as success instead of running"

The example below uses none of them.

The Example​

A Dag called double_a_number has two tasks: make_number returns 21, and double prints that number doubled. Running it with dag.test() shows both tasks running in order, and the number passing from one to the other, with nothing else of Airflow running.

Prerequisites​

  • Apache Airflow® installed, and a terminal with its virtual environment active and AIRFLOW_HOME set.
  • Airflow's database created. airflow standalone creates it the first time it starts, and airflow db migrate creates it without starting anything. Airflow's debugging guide lists "an initialized metadata database" as the first thing dag.test() needs.

Step 1: Add dag.test() to the Dag File​

Create dags/double_a_number.py in your AIRFLOW_HOME:

dags/double_a_number.py
from airflow.sdk import dag, task


@dag(schedule=None)
def double_a_number():
@task
def make_number():
return 21

@task
def double(number):
print(f"{number} doubled is {number * 2}")

double(make_number())


number_dag = double_a_number()

if __name__ == "__main__":
number_dag.test()

The double_a_number.py Dag file in an editor window: two tasks, make_number returning 21 and double printing the number doubled, then number_dag = double_a_number() and number_dag.test() under if name == "main".

  • make_number returns 21. double(make_number()) hands that value to double as number, and makes double run after make_number.
  • double prints a sentence. Airflow writes whatever a task prints to the task's log, and dag.test() shows the logs in the terminal.
  • number_dag = double_a_number() creates the Dag at the top level of the file, so Airflow finds it when dag.test() reads the Dags folder. A Dag created only inside the if block stops dag.test() with an error: Fixed: "Cannot create DagRun for DAG … because the dag is not serialized" in Apache Airflow® shows it.
  • number_dag.test() runs the Dag when you run the file.

Step 2: Run the File​

In the folder that holds dags:

Terminal
python dags/double_a_number.py

dag.test() prints Airflow's log lines as it goes, 34 of them for this Dag. These are the ones that show what happened:

Output (excerpt)
2026-12-04T13:33:38.816219Z [info ] Creating ORM DAG for double_a_number [airflow.dag_processing.collection] loc=collection.py:104
2026-12-04T13:33:39.103333Z [info ] [DAG TEST] starting task_id=make_number map_index=-1 [airflow.sdk.definitions.dag] loc=dag.py:1540
2026-12-04T13:33:41.522913Z [info ] Done. Returned value was: 21 [airflow.task.operators.airflow.providers.standard.decorators.python._PythonDecoratedOperator] loc=python.py:233
2026-12-04T13:33:41.552631Z [info ] [DAG TEST] end task task_id=make_number map_index=-1 [airflow.sdk.definitions.dag] loc=dag.py:1613
2026-12-04T13:33:41.555701Z [info ] [DAG TEST] starting task_id=double map_index=-1 [airflow.sdk.definitions.dag] loc=dag.py:1540
21 doubled is 42
2026-12-04T13:33:41.604326Z [info ] [DAG TEST] end task task_id=double map_index=-1 [airflow.sdk.definitions.dag] loc=dag.py:1613
2026-12-04T13:33:41.605618Z [info ] Marking run <DagRun double_a_number @ 2026-12-04 13:33:38.693545+00:00: manual__2026-12-04T13:33:38.790515+00:00, state:running, queued_at: None. run_type: manual> successful [airflow.models.dagrun.DagRun] loc=dagrun.py:1381

The terminal after python dags/double_a_number.py: DAG TEST starting task_id=make_number, Done. Returned value was: 21, then DAG TEST starting task_id=double, the line 21 doubled is 42, and Marking run of double_a_number successful.

  • Creating ORM DAG for double_a_number is Airflow storing the Dag in its database.
  • [DAG TEST] starting and [DAG TEST] end frame each task. make_number returned 21, and then double printed 21 doubled is 42.
  • The last line marks the run successful. The command ended with exit code 0, about three seconds after it started.

No part of Airflow was running on my computer during this run: dag.test() ran both tasks itself.

Step 3: Check the Stored Run​

dag.test() makes a real Dag run, and Airflow keeps it in its database like any other:

Terminal
airflow dags list-runs double_a_number
Output
dag_id | run_id | state | run_after | logical_date | start_date | end_date
================+==========================================+=========+==================================+==================================+==================================+=================================
double_a_number | manual__2026-12-04T13:33:38.790515+00:00 | success | 2026-12-04T13:33:38.790515+00:00 | 2026-12-04T13:33:38.693545+00:00 | 2026-12-04T13:33:39.048458+00:00 | 2026-12-04T13:33:41.605688+00:00

The terminal after airflow dags list-runs double_a_number: one row with the run ID manual__2026-12-04T13:33:38.790515+00:00 and the state success.

The run has the same ID as in the log, and the state success. The command also printed two FileType is deprecated warnings first, which I left out: Fixed: FileType is deprecated explains them.

Common Mistakes​

MistakeWhat happensDo this instead
Creating the Dag only inside the if __name__ == "__main__": blockCannot create DagRun for DAG … because the dag is not serialized: Airflow's own reading of the file finds no DagCreate it at the top level, as number_dag = double_a_number() does
Running a Dag file that isn't in the Dags folderThe same error: the debugging guide says such a Dag "is not in any bundle"Keep the file in the Dags folder

Troubleshooting​

SymptomLikely causeFix
Cannot create DagRun for DAG <dag id> because the dag is not serializedThe Dag is created only inside the if block, or its file isn't in the Dags folderSee Fixed: "Cannot create DagRun for DAG … because the dag is not serialized" in Apache Airflow®

FAQ​

Does dag.test() need the scheduler?​

No. The debugging guide says it runs "through your Dag in a single serialized python process", and the run above worked with nothing else of Airflow running.

What does dag.test() return?​

The run it made. In Airflow 3.4.0's code, test() ends with return dr, the Dag run, so run = number_dag.test() keeps it in run.

Conclusion​

number_dag.test() at the bottom of dags/double_a_number.py, under if __name__ == "__main__":, ran both tasks of the Dag in order when python ran the file: make_number returned 21, and double printed 21 doubled is 42. The Dag object has to exist at the top level of the file, and Airflow stores the run like any other.

Additional Resources​

Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.