suii2210 commented on issue #58893:
URL: https://github.com/apache/airflow/issues/58893#issuecomment-3673916591
Thanks for the feedback,
TaskInstance.run() is intentionally a low-level API and does not accept
arguments for @task-decorated functions. For TaskFlow tasks, inputs are
resolved via XComs during DAG execution, not passed directly to the
TaskInstance.
For unit tests that involve XCom arguments, the recommended approach is to
use dag.create_dagrun(), which mirrors real TaskFlow execution and allows
Airflow to handle XCom resolution:
`@task
def add_numbers(a: int, b: int):
return a + b
with DAG(dag_id="test_dag", start_date=pendulum.datetime(2024, 1, 1)) as dag:
add_numbers(1, 2)
dagrun = dag.create_dagrun(run_id="test_run")
ti = dagrun.get_task_instance("add_numbers")
ti.run(ignore_ti_state=True)
assert ti.xcom_pull(task_ids="add_numbers") == 3
`
For narrow unit tests that only validate task logic, mocking xcom_pull() is
also an option, but it does not reflect full DAG execution.
I can add a short TaskFlow/XCom example to the docs if that would be helpful.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]