Repository navigation
Closed
Labels
Description
Apache Airflow version
3.1.0
If "Other Airflow 2/3 version" selected, which one?
No response
What happened?
Airflow dag.test() stopped working in Airflow 3.1.
What you think should happen instead?
The command dag.test() should work as in Airlfow 3.0.
How to reproduce
Create this DAG file as example_bug.py:
from airflow import DAG
from airflow.decorators import dag, task
@dag(
dag_id="simple_dag",
)
def simple_dag():
"""A simple DAG with two Python tasks."""
@task
def start():
print("Starting the DAG...")
return "Hello from start!"
@task
def end(message: str):
print(f"Received message: {message}")
print("Ending the DAG!")
msg = start()
end(msg)
dag = simple_dag()
if __name__ == "__main__":
dag.test()
And run in a Python virtual environment with:
python example_bug.py
Observe stacktrace similar to:
Traceback (most recent call last):
File "/Users/tatiana.alchueyr/Code/astronomer-cosmos/example_bug.py", line 27, in <module>
dag.test()
File "/Users/tatiana.alchueyr/Library/Application Support/hatch/env/virtual/astronomer-cosmos/4lAAXoPP/tests.py3.10-3.1-1.10/lib/python3.10/site-packages/airflow/sdk/definitions/dag.py", line 1202, in test
dr: DagRun = get_or_create_dagrun(
File "/Users/tatiana.alchueyr/Library/Application Support/hatch/env/virtual/astronomer-cosmos/4lAAXoPP/tests.py3.10-3.1-1.10/lib/python3.10/site-packages/airflow/models/dagrun.py", line 2170, in get_or_create_dagrun
dr = dag.create_dagrun(
File "/Users/tatiana.alchueyr/Library/Application Support/hatch/env/virtual/astronomer-cosmos/4lAAXoPP/tests.py3.10-3.1-1.10/lib/python3.10/site-packages/airflow/utils/session.py", line 98, in wrapper
return func(*args, **kwargs)
File "/Users/tatiana.alchueyr/Library/Application Support/hatch/env/virtual/astronomer-cosmos/4lAAXoPP/tests.py3.10-3.1-1.10/lib/python3.10/site-packages/airflow/serialization/serialized_objects.py", line 3257, in create_dagrun
orm_dagrun = _create_orm_dagrun(
File "/Users/tatiana.alchueyr/Library/Application Support/hatch/env/virtual/astronomer-cosmos/4lAAXoPP/tests.py3.10-3.1-1.10/lib/python3.10/site-packages/airflow/utils/session.py", line 98, in wrapper
return func(*args, **kwargs)
File "/Users/tatiana.alchueyr/Library/Application Support/hatch/env/virtual/astronomer-cosmos/4lAAXoPP/tests.py3.10-3.1-1.10/lib/python3.10/site-packages/airflow/serialization/serialized_objects.py", line 2314, in _create_orm_dagrun
raise AirflowException(f"Cannot create DagRun for DAG {dag.dag_id} because the dag is not serialized")
airflow.exceptions.AirflowException: Cannot create DagRun for DAG simple_dag because the dag is not serialized
Operating System
MacOS
Versions of Apache Airflow Providers
No response
Deployment
Other
Deployment details
This issue happens in the local environment, not in a deployed Airflow environment.
Anything else?
No response
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
ndellosa95 commentedon Nov 3, 2025
I'm still seeing this issue with Airflow 3.1.1 for tests where we construct a dag in the test and then run it using
dag.test. I suppose this is not supported behavior fordag.testspecifically anymore but I'm at my wits end trying to figure out how to replicate the old functionality. These dags are parametrized so there's no easy way to just copy them into a separate dag file.thehalleffect commentedon Nov 6, 2025
This is still a problem in
3.1.2underpython 3.10.18.@ndellosa95 a suggestion: perhaps pre-generating the dags into discrete dagfiles in a temp dir, pointing a
DagBagat that temp dir, and then invoking the parametrized tests against each generated test dag would work? Disclaimer: YMMV - just an idea, and I have not tried this myself.danielvdende commentedon Nov 11, 2025
I just tried this with the
3.1.3rc1version, but I still have the same problem...thehalleffect commentedon Nov 11, 2025
For whoever is trying to work around this still, I've untangled whats going on a bit. Whenever you import ANYTHING that transitively imports the config module, it will eagerly load defaults in a way that kind of hoses dagbag loading in a pretty opaque fashion. i've come up with this as a (kinda crappy) workaround in one of the
conftest.pys in our project:(
path_foris just a little utility shim for grabbing the absolute path of some project resource relative to the project root)DAG.testshould now work.if you also need to run specific task instances manually in your tests without dealing with the boilerplate (and execution time) of invoking
teston the WHOLE dag (which is a thing we do with our project), you can also add this hackiness (ripped fromDAG.testand simplified/massaged a little):all that said: this was WAY too much digging to get functionality back that worked alright before.
DAG.testdefinitely needs some regression testing (it's just tests all the way down, i guess? 🤪) wrapped around it, and the unreasonably eager conf loading needs to be looked at by someone more familiar with the intimate details of how Airflow bootstraps itself when modules are loaded.@ephraimbuddy fyi