Mocking replaces an operation or object with controlled behavior so a test can run without calling an external system. In Airflow operator tests, a mock can make a hook return a test connection instead of querying the metastore; the test can also check whether the hook was called as expected. Mocking requires knowing which internals to replace, including the correct import location. section 10.1.4 section 10.4.1
Listing 10.15 Mocking an object in a test
def test_movielenspopularityoperator(mocker):
mocker.patch.object(
MovielensHook,
"get_connection",
return_value=Connection(
conn_id="test",
login="airflow",
password="airflow",
),
)
task = MovielensPopularityOperator(
task_id="test_id",
conn_id="test",
start_date="2015-01-01",
end_date="2015-01-03",
top_n=5,
)
result = task.execute(context=None)
assert len(result) == 3
Example: patch a MovieLens hook to return a test connection, then execute the operator without querying the metastore. section 10.1.4
Listing 10.17 Validating the behavior of a mocked function
mock_get = mocker.patch.object( #1
MovielensHook,
"get_connection",
return_value=Connection(...),
)
task = MovielensPopularityOperator(..., conn_id="testconn")
task.execute(...)
assert mock_get.call_count == 1 #2
mock_get.assert_called_with("testconn") #3
A mock can expose call details so a test can check both how often a function ran and which argument it received. section 10.1.4




















