0
0
Apache Airflowdevops~10 mins

Testing custom operators in Apache Airflow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the base class for custom Airflow operators.

Apache Airflow
from airflow.operators.[1] import BaseOperator
Drag options to blanks, or click blank then click option'
Abash
Bbase
Cpython
Ddummy
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BaseOperator from bash or python operators instead of base.
Trying to import BaseOperator directly from airflow.operators.
2fill in blank
medium

Complete the code to define the execute method in a custom operator.

Apache Airflow
def execute(self, context):
    self.[1]('Running custom operator logic')
Drag options to blanks, or click blank then click option'
Alog.info
Breturn
Cprint
Draise
Attempts:
3 left
💡 Hint
Common Mistakes
Using print which outputs to stdout instead of structured task logs.
Using log.info without self..
Using return instead of logging.
3fill in blank
hard

Fix the error in the custom operator's init method by completing the super call.

Apache Airflow
def __init__(self, param1, **kwargs):
    super().[1](param1=param1, **kwargs)
Drag options to blanks, or click blank then click option'
Arun
Bexecute
Cstart
D__init__
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super().execute instead of __init__.
Forgetting to call the parent class's init method.
4fill in blank
hard

Complete the code to create a test case that checks if the custom operator runs without errors.

Apache Airflow
def test_custom_operator():
    operator = CustomOperator(task_id='test_task')
    result = operator.[1]({})
    assert result is None
Drag options to blanks, or click blank then click option'
Astart
Brun
Cexecute
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Calling __init__ or run instead of execute.
Not passing the context dictionary to the execute method.
5fill in blank
hard

Fill all three blanks to complete a test that mocks the logging inside the custom operator.

Apache Airflow
import unittest.mock as mock

def test_logging_in_operator():
    operator = CustomOperator(task_id='log_test')
    with mock.patch.object(operator, 'log') as mock_log:
        operator.[1]({})
        mock_log.[2].assert_called_with('[3]')
Drag options to blanks, or click blank then click option'
Aexecute
Binfo
CRunning custom operator logic
Ddebug
Attempts:
3 left
💡 Hint
Common Mistakes
Using debug instead of info for logging.
Not mocking the correct log method or message.
Wrong patch target for the operator's log.