0
0
Apache Airflowdevops~20 mins

Testing custom operators in Apache Airflow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple custom operator execution

Given the following Airflow custom operator code snippet, what will be the output when the operator runs successfully?

Apache Airflow
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults

class HelloOperator(BaseOperator):
    @apply_defaults
    def __init__(self, name: str, **kwargs):
        super().__init__(**kwargs)
        self.name = name

    def execute(self, context):
        message = f"Hello, {self.name}!"
        print(message)
        return message

# Assume this operator is instantiated and run with name='Airflow'
AHello, Airflow!
BHello, name!
CError: missing required argument
DNone
Attempts:
2 left
💡 Hint

Look at the execute method and what it prints and returns.

Configuration
intermediate
2:00remaining
Correct DAG configuration to use a custom operator

Which DAG configuration snippet correctly uses a custom operator MyOperator with task_id task1?

Apache Airflow
from airflow import DAG
from datetime import datetime
from my_operators import MyOperator

default_args = {
    'start_date': datetime(2024, 1, 1),
}

dag = DAG('my_dag', default_args=default_args, schedule_interval='@daily')

# Fill in the correct task definition below
Atask1 = MyOperator(task_id='task1')
Btask1 = MyOperator(dag=dag)
Ctask1 = MyOperator('task1', dag=dag)
Dtask1 = MyOperator(task_id='task1', dag=dag)
Attempts:
2 left
💡 Hint

Remember that task_id is a required named argument for operators.

Troubleshoot
advanced
2:00remaining
Error raised when execute method is missing

What error will Airflow raise if a custom operator class does not implement the execute method?

Apache Airflow
from airflow.models import BaseOperator

class BrokenOperator(BaseOperator):
    pass

# When Airflow tries to run BrokenOperator, what happens?
AAttributeError: 'BrokenOperator' object has no attribute 'execute'
BTypeError: Can't instantiate abstract class BrokenOperator with abstract methods execute
CRuntimeError: execute method missing
DNo error, runs with default execute
Attempts:
2 left
💡 Hint

BaseOperator uses Python's abstract base class mechanism to enforce execute implementation.

🔀 Workflow
advanced
2:00remaining
Order of execution in DAG with custom operators

Given a DAG with two custom operators op1 and op2 where op2 depends on op1, what is the correct order of execution?

Apache Airflow
op1 = MyOperator(task_id='op1', dag=dag)
op2 = MyOperator(task_id='op2', dag=dag)
op1 >> op2
ABoth run in parallel
B2,1
C1,2
DNo guaranteed order
Attempts:
2 left
💡 Hint

The >> operator sets task dependencies in Airflow.

Best Practice
expert
3:00remaining
Best practice for unit testing a custom operator's execute method

Which approach is best for unit testing the execute method of a custom Airflow operator?

AMock Airflow context and dependencies, then call <code>execute</code> directly and assert results
BRun the operator in a real Airflow environment and check logs manually
CTest only the DAG, not the operator code
DUse print statements inside <code>execute</code> and verify output visually
Attempts:
2 left
💡 Hint

Unit tests should be automated and isolated from external systems.