Add a fixture to easily replace ti.run usage#48439
Merged
Merged
Conversation
As we are replacing BaseOperator usage from Core to Task SDK, we are running into several issues, one of the common one being over-usage of `task.run()`. While some cases can be easily replaced by `task.execute()` others needs execution of the tasks, sharing of XCom's in between, checking task state, correct exception etc. To make this easier I have added `run_task` fixture which I have been using in apache#48244 and it has worked out well. Example:
ashb
approved these changes
Mar 27, 2025
ashb
left a comment
Member
There was a problem hiding this comment.
I almost wonder if it's worth defining some of the classes and protocols in another file inside tests_common package, so that the fixture can be smaller.
For example something like this
from ... import RunTaskCallable
@pytest.fixture
def run_task(create_runtime_ti, mock_supervisor_comms, spy_agency) -> RunTaskCallable:
"""
Fixture to run a task without defining a dag file.
This fixture builds on top of create_runtime_ti to provide a convenient way to execute tasks and get their results.
The fixture provides:
- run_task.state - Get the task state
- run_task.msg - Get the task message
- run_task.error - Get the task error
- run_task.xcom.get(key) - Get an XCom value
- run_task.xcom.assert_pushed(key, value, ...) - Assert an XCom was pushed
Example usage: ::
def test_custom_task(run_task):
class MyTaskOperator(BaseOperator):
def execute(self, context):
return "hello"
task = MyTaskOperator(task_id="test_task")
run_task(task)
assert run_task.state == TerminalTIState.SUCCESS
assert run_task.error is None
"""
return RunTaskWithXCom(create_runtime_ti, mock_supervisor_comms, spy_agency)
Member
Author
Good idea, might take a stab at it it later on with other fixtures too. |
pankajkoti
pushed a commit
to astronomer/airflow
that referenced
this pull request
Mar 28, 2025
As we are replacing BaseOperator usage from Core to Task SDK, we are running into several issues, one of the common one being over-usage of `task.run()`. While some cases can be easily replaced by `task.execute()` others needs execution of the tasks, sharing of XCom's in between, checking task state, correct exception etc. To make this easier I have added `run_task` fixture which I have been using in apache#48244 and it has worked out well. Example:
shubham-pyc
pushed a commit
to shubham-pyc/airflow
that referenced
this pull request
Apr 2, 2025
As we are replacing BaseOperator usage from Core to Task SDK, we are running into several issues, one of the common one being over-usage of `task.run()`. While some cases can be easily replaced by `task.execute()` others needs execution of the tasks, sharing of XCom's in between, checking task state, correct exception etc. To make this easier I have added `run_task` fixture which I have been using in apache#48244 and it has worked out well. Example:
aaron-y-chen
pushed a commit
to aaron-y-chen/airflow
that referenced
this pull request
Apr 4, 2025
As we are replacing BaseOperator usage from Core to Task SDK, we are running into several issues, one of the common one being over-usage of `task.run()`. While some cases can be easily replaced by `task.execute()` others needs execution of the tasks, sharing of XCom's in between, checking task state, correct exception etc. To make this easier I have added `run_task` fixture which I have been using in apache#48244 and it has worked out well. Example:
simonprydden
pushed a commit
to simonprydden/airflow
that referenced
this pull request
Apr 8, 2025
As we are replacing BaseOperator usage from Core to Task SDK, we are running into several issues, one of the common one being over-usage of `task.run()`. While some cases can be easily replaced by `task.execute()` others needs execution of the tasks, sharing of XCom's in between, checking task state, correct exception etc. To make this easier I have added `run_task` fixture which I have been using in apache#48244 and it has worked out well. Example:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As we are replacing BaseOperator usage from Core to Task SDK, we are running into several issues, one of the common one being over-usage of
task.run().While some cases can be easily replaced by
task.execute()others needs execution of the tasks, sharing of XCom's in between, checking task state, correct exception etc.To make this easier I have added
run_taskfixture which I have been using in #48244 and it has worked out well. This also takes care of creating a DAG if not provided.Example Usage
Basic Task Execution
Testing XCom Operations
Testing Task Failures
Migration Guide
Replace existing
ti.run()usage with the new fixture:Before:
After:
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rstor{issue_number}.significant.rst, in airflow-core/newsfragments.