apache / apache/airflow

Standardize test usage of context: Context across operator tests (avoid context=None)

Open
#52,679 5 comments 0 reactions 0 assignees View on GitHub
area:dev-env kind:feature needs-triage
Dominant language
Python
Stars
46.9k
Forks
17.8k
Avg merge
2d 10h
Merged PRs (30d)
483

Description

### Description

### Title

Standardize test usage of `context: Context` across operator tests (avoid `context=None`)

---

### Description

Many operator tests across the Airflow codebase currently invoke `execute()` like this:
related:

https://github.com/apache/airflow/blob/main/providers/microsoft/azure/src/airflow/providers/microsoft/azure/operators/wasb_delete_blob.py

https://github.com/apache/airflow/blob/main/providers/microsoft/azure/tests/unit/microsoft/azure/operators/test_wasb_delete_blob.py

test code

```python
operator.execute(None)
```

However, the `execute()` method is explicitly typed as:

```python
def execute(self, context: Context) -> None:
```

To align with this contract and improve typing correctness and future maintainability, we should replace `None` with a minimal `Context` object (e.g., `{}` or `MagicMock(spec=Context)`).

#### Benefits of this change:
- Aligns test cases with actual method signature (`context: Context`)
- Avoids potential mypy/type-checking issues (especially in Airflow 3+)
- Prevents misleading behavior in tests by not passing `None` when `context` is required
- Improves clarity and correctness in test expectations

---

### Suggested Migration Pattern

**Before:**
```python
operator.execute(None)
```

**After:**
```python

if TYPE_CHECKING:
try:
from airflow.sdk.definitions.context import Context
except ImportError:
# TODO: Remove once provider drops support for Airflow 2
from airflow.utils.context import Context

mock_context: Context = {}
operator.execute(mock_context)
```

**Or (stricter typing):**
```python
from unittest.mock import MagicMock

if TYPE_CHECKING:
try:
from airflow.sdk.definitions.context import Context
except ImportError:
# TODO: Remove once provider drops support for Airflow 2
from airflow.utils.context import Context

mock_context = MagicMock(spec=Context)
operator.execute(mock_context)
```

### Use case/motivation

_No response_

### Related issues

_No response_

### Are you willing to submit a PR?

- [x] Yes I am willing to submit a PR!

### Code of Conduct

- [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)

Contributor guide

Open the contributing guide

Research direction

Start with providers/microsoft/azure/tests/unit/microsoft/azure/operators/test_wasb_delete_blob.py and the related wasb_delete_blob.py operator. Inspect how the tests call execute(None), then use the proposed Context-based pattern consistently across the operator tests. Run the referenced test file and confirm the tests pass without passing None to execute().

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.