Bird
Raised Fist0
Apache Airflowdevops~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main reason to write tests for custom Airflow operators?
easy
A. To ensure the operator works as expected before running in production
B. To make the DAG run faster
C. To reduce the number of tasks in the DAG
D. To automatically generate documentation

Solution

  1. Step 1: Understand the purpose of testing

    Testing is done to verify that code behaves correctly and avoids errors in production.
  2. Step 2: Apply this to custom operators

    Custom operators are pieces of code that need validation to ensure they perform their tasks as intended.
  3. Final Answer:

    To ensure the operator works as expected before running in production -> Option A
  4. Quick Check:

    Testing = Verify correctness [OK]
Hint: Testing checks correctness before production use [OK]
Common Mistakes:
  • Thinking tests speed up DAG execution
  • Confusing testing with documentation generation
  • Believing tests reduce task count
2. Which Python module is commonly used to write tests for Airflow custom operators?
easy
A. requests
B. flask
C. unittest
D. pandas

Solution

  1. Step 1: Identify common testing modules in Python

    Python's built-in module for testing is unittest, widely used for writing test cases.
  2. Step 2: Match with Airflow testing practices

    Airflow tests often use unittest to create test classes and methods for operators.
  3. Final Answer:

    unittest -> Option C
  4. Quick Check:

    Python testing = unittest [OK]
Hint: Use unittest for Python testing by default [OK]
Common Mistakes:
  • Choosing unrelated modules like flask or pandas
  • Confusing HTTP libraries with testing tools
3. Given this test snippet for a custom operator:
from airflow.models import DagBag

def test_operator_load():
    dagbag = DagBag()
    assert 'my_custom_dag' in dagbag.dags

What does this test check?
medium
A. If the DAG named 'my_custom_dag' is loaded successfully
B. If the operator runs without errors
C. If the DAG tasks execute in order
D. If the DAG has no syntax errors

Solution

  1. Step 1: Understand DagBag usage

    DagBag loads all DAGs from the Airflow folder and stores them in dagbag.dags dictionary.
  2. Step 2: Analyze the assertion

    The test asserts that 'my_custom_dag' is a key in dagbag.dags, meaning it was loaded.
  3. Final Answer:

    If the DAG named 'my_custom_dag' is loaded successfully -> Option A
  4. Quick Check:

    DagBag loads DAGs = check DAG presence [OK]
Hint: DagBag.dags keys are loaded DAG ids [OK]
Common Mistakes:
  • Thinking it tests operator execution
  • Confusing DAG loading with task order
  • Assuming it checks syntax errors directly
4. You wrote a test for a custom operator but it fails with AttributeError: 'NoneType' object has no attribute 'execute'. What is the likely cause?
medium
A. The DAG file has syntax errors
B. The operator instance was not created properly in the test
C. The Airflow scheduler is not running
D. The test file is missing import statements

Solution

  1. Step 1: Understand the error meaning

    AttributeError on 'NoneType' means the variable expected to be an object is actually None.
  2. Step 2: Relate to operator instance creation

    If the operator instance was not created or assigned, calling execute() on None causes this error.
  3. Final Answer:

    The operator instance was not created properly in the test -> Option B
  4. Quick Check:

    None.execute error = missing instance [OK]
Hint: Check if operator object is None before calling methods [OK]
Common Mistakes:
  • Blaming DAG syntax errors for this runtime error
  • Thinking scheduler status affects unit tests
  • Ignoring missing or wrong object creation
5. You want to test a custom operator that makes an HTTP request. Which approach is best to avoid real network calls during tests?
hard
A. Write the test to call the real HTTP endpoint every time
B. Run the test only when connected to the internet
C. Skip the test if the HTTP server is down
D. Use mocking to replace the HTTP call with a fake response

Solution

  1. Step 1: Understand testing external calls

    Real network calls can be slow, flaky, or cause side effects, so they should be avoided in unit tests.
  2. Step 2: Use mocking for external dependencies

    Mocking replaces the real HTTP call with a controlled fake response, making tests fast and reliable.
  3. Final Answer:

    Use mocking to replace the HTTP call with a fake response -> Option D
  4. Quick Check:

    Mock external calls = reliable tests [OK]
Hint: Mock external calls to avoid real network in tests [OK]
Common Mistakes:
  • Running tests dependent on internet availability
  • Skipping tests instead of fixing design
  • Calling real endpoints causing flaky tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard