-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Add a fixture to easily replace ti.run usage
#48439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
Good idea, might take a stab at it it later on with other fixtures too. |
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:
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:
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:
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:
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.