SIGN IN SIGN UP
apache / airflow UNCLAIMED

Apache Airflow - A platform to programmatically author, schedule, and monitor workflows

0 0 0 Python

Fix pytest collection failure for classes decorated with context managers (#55915)

Classes decorated with `@conf_vars` and other context managers were disappearing
during pytest collection, causing tests to be silently skipped. This affected
several test classes including `TestWorkerStart` in the Celery provider tests.

Root cause: `ContextDecorator` transforms decorated classes into callable wrappers.
Since pytest only collects actual type objects as test classes, these wrapped
classes are ignored during collection.

Simple reproduction (no Airflow needed):

```py
import contextlib
import inspect

@contextlib.contextmanager
def simple_cm():
    yield

@simple_cm()
class TestExample:
    def test_method(self):
        pass

print(f'Is class? {inspect.isclass(TestExample)}')  # False - pytest won't collect
```

and then run

```shell
pytest test_example.py --collect-only
```

Airflow reproduction:

```shell
breeze run pytest providers/celery/tests/unit/celery/cli/test_celery_command.py --collect-only -v

breeze run pytest providers/celery/tests/unit/celery/cli/test_celery_command.py --collect-only -v
```

Solution:
1. Fixed affected test files by replacing class-level `@conf_vars` decorators
   with pytest fixtures
2. Created pytest fixtures to apply configuration changes
3. Used `@pytest.mark.usefixtures` to apply configuration to test classes
4. Added custom linter to prevent future occurrences and integrated it
   into pre-commit hooks

Files changed:
- Fixed 3 test files with problematic class decorators
- Added custom linter with pre-commit integration

This ensures pytest properly collects all test classes and prevents similar
issues in the future through automated detection.
K
Kaxil Naik committed
e95f24f866fb5d9c81b37fd8f2ce7247889656c1
Parent: e6ebf6d
Committed by GitHub <noreply@github.com> on 9/23/2025, 3:42:46 AM