This is an automated email from the ASF dual-hosted git repository.

kaxil pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 64943ba6dbb Fix `run_duration` calculation in airflow dags test by 
decoupling start_date (#68837)
64943ba6dbb is described below

commit 64943ba6dbb625ad638a0d37076902da0215d0ab
Author: Sejal Gupta <[email protected]>
AuthorDate: Thu Oct 1 00:57:32 2026 +0530

    Fix `run_duration` calculation in airflow dags test by decoupling 
start_date (#68837)
---
 airflow-core/src/airflow/models/dagrun.py   |  2 +-
 airflow-core/tests/unit/models/test_dag.py  | 19 +++++++++++++++++++
 task-sdk/src/airflow/sdk/definitions/dag.py |  2 +-
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/airflow-core/src/airflow/models/dagrun.py 
b/airflow-core/src/airflow/models/dagrun.py
index 6606041f4f5..c2b165d49cc 100644
--- a/airflow-core/src/airflow/models/dagrun.py
+++ b/airflow-core/src/airflow/models/dagrun.py
@@ -2610,7 +2610,7 @@ def get_or_create_dagrun(
         state=DagRunState.RUNNING,
         triggered_by=triggered_by,
         triggering_user_name=triggering_user_name,
-        start_date=start_date or logical_date,
+        start_date=start_date or timezone.utcnow(),
         session=session,
     )
     log.info("Created dag run.", dagrun=dr)
diff --git a/airflow-core/tests/unit/models/test_dag.py 
b/airflow-core/tests/unit/models/test_dag.py
index bf98950484a..922dca841de 100644
--- a/airflow-core/tests/unit/models/test_dag.py
+++ b/airflow-core/tests/unit/models/test_dag.py
@@ -361,6 +361,25 @@ class TestDag:
         assert "testing" in instantiated
         assert "unrelated" not in instantiated
 
+    def test_dag_test_runtime_start_date_decoupled_from_logical_date(self, 
dag_maker, time_machine):
+        """
+        Ensure DAG.test() decouples its execution start_date from historical 
logical_dates.
+        """
+        past_logical_date = pendulum.datetime(2024, 1, 1, tz="UTC")
+        frozen_now = pendulum.datetime(2026, 6, 22, 12, 0, 0, tz="UTC")
+
+        time_machine.move_to(frozen_now, tick=False)
+
+        with dag_maker(dag_id="test_runtime_duration_isolation", 
start_date=past_logical_date) as dag:
+            EmptyOperator(task_id="task1")
+
+        # Run dag.test against the DB
+        dr = dag.test(logical_date=past_logical_date)
+
+        # Assert directly on the created DagRun object returned from the DB
+        assert dr.logical_date == past_logical_date
+        assert dr.start_date == frozen_now
+
     def teardown_method(self) -> None:
         clear_db_runs()
         clear_db_dags()
diff --git a/task-sdk/src/airflow/sdk/definitions/dag.py 
b/task-sdk/src/airflow/sdk/definitions/dag.py
index 05117729f91..de1cabb34bc 100644
--- a/task-sdk/src/airflow/sdk/definitions/dag.py
+++ b/task-sdk/src/airflow/sdk/definitions/dag.py
@@ -1368,7 +1368,7 @@ class DAG:
 
             dr: DagRun = get_or_create_dagrun(
                 dag=scheduler_dag,
-                start_date=logical_date or run_after,
+                start_date=timezone.coerce_datetime(timezone.utcnow()),
                 logical_date=logical_date,
                 data_interval=data_interval,
                 run_after=run_after,

Reply via email to