Files
cthulhu/tests/cthulhu_test_fixtures.py

86 lines
2.8 KiB
Python

# Orca Test Fixtures - Pytest Integration
#
# Copyright 2025 Igalia, S.L.
# Author: Joanmarie Diggs <jdiggs@igalia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA 02110-1301 USA.
"""Pytest fixtures for Cthulhu screen reader tests.
This module provides pytest fixtures that integrate the CthulhuTestContext
with pytest's fixture system, providing clean, isolated test environments.
The fixtures are designed to be simple to use while providing complete
test isolation and preventing cross-test contamination.
Usage:
def test_presenter(orca_test):
cthulhu_test.setup_where_am_i_presenter_dependencies()
from cthulhu.where_am_i_presenter import WhereAmIPresenter
presenter = WhereAmIPresenter()
result = presenter.some_method()
assert result is True
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from cthulhu_test_context import CthulhuTestContext
if TYPE_CHECKING:
from collections.abc import Generator
from pytest_mock import MockerFixture
@pytest.fixture
def test_context(mocker: MockerFixture, monkeypatch) -> Generator[CthulhuTestContext, None, None]:
"""Provides clean, isolated Orca test environment.
This is the primary fixture for Cthulhu tests. It provides a complete
test isolation context that prevents cross-test contamination.
Usage:
class TestMyModule:
def _setup_dependencies(self, cthulhu_test):
# Set up only what your module needs
return cthulhu_test.setup_shared_dependencies(["cthulhu.debug"])
def test_my_functionality(self, cthulhu_test):
self._setup_dependencies(orca_test)
from cthulhu.my_module import MyClass
# Your test code here
instance = MyClass()
result = instance.some_method()
assert result is True
Args:
mocker: pytest-mock mocker fixture (automatically injected)
monkeypatch: pytest monkeypatch fixture (automatically injected)
Yields:
CthulhuTestContext instance with clean isolation
"""
with CthulhuTestContext(mocker, monkeypatch) as context:
yield context