How to Run a Dag in One Python Process with dag.test() in Apache Airflow®
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
<dag variable> = <dag function>()
if __name__ == "__main__":
<dag variable>.test()
| Part | What 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:
| Argument | What 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_HOMEset. - Airflow's database created.
airflow standalonecreates it the first time it starts, andairflow db migratecreates it without starting anything. Airflow's debugging guide lists "an initialized metadata database" as the first thingdag.test()needs.
Step 1: Add dag.test() to the Dag File
Create dags/double_a_number.py in your AIRFLOW_HOME:
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()

make_numberreturns21.double(make_number())hands that value todoubleasnumber, and makesdoublerun aftermake_number.doubleprints a sentence. Airflow writes whatever a task prints to the task's log, anddag.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 whendag.test()reads the Dags folder. A Dag created only inside theifblock stopsdag.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:
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:
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

Creating ORM DAG for double_a_numberis Airflow storing the Dag in its database.[DAG TEST] startingand[DAG TEST] endframe each task.make_numberreturned 21, and thendoubleprinted21 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:
airflow dags list-runs double_a_number
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 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
| Mistake | What happens | Do this instead |
|---|---|---|
Creating the Dag only inside the if __name__ == "__main__": block | Cannot create DagRun for DAG … because the dag is not serialized: Airflow's own reading of the file finds no Dag | Create it at the top level, as number_dag = double_a_number() does |
| Running a Dag file that isn't in the Dags folder | The same error: the debugging guide says such a Dag "is not in any bundle" | Keep the file in the Dags folder |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Cannot create DagRun for DAG <dag id> because the dag is not serialized | The Dag is created only inside the if block, or its file isn't in the Dags folder | See 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
- Debugging Airflow Dags —
dag.test(), what it needs and its optional arguments.
Related
- How to Write a Dag with the TaskFlow API in Apache Airflow®
- How to Trigger a Dag from the Command Line with airflow dags trigger
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.
