Skip to content

Basic fixtures

provide.testkit.threading.basic_fixtures

Basic threading test fixtures.

Core fixtures for creating threads, thread pools, mocks, and thread-local storage.

Functions

mock_thread

mock_thread() -> Mock

Create a mock thread for testing without actual threading.

Returns:

Type Description
Mock

Mock thread object.

Source code in provide/testkit/threading/basic_fixtures.py
@pytest.fixture
def mock_thread() -> Mock:
    """
    Create a mock thread for testing without actual threading.

    Returns:
        Mock thread object.
    """
    mock = Mock(spec=threading.Thread)
    mock.is_alive.return_value = False
    mock.daemon = False
    mock.name = "MockThread"
    mock.ident = 12345
    mock.start = Mock()
    mock.join = Mock()
    mock.run = Mock()

    return mock

test_thread

test_thread() -> Callable[
    [
        Callable[..., Any],
        tuple[Any, ...],
        dict[str, Any] | None,
        bool,
    ],
    threading.Thread,
]

Create a test thread with automatic cleanup.

Returns:

Type Description
Callable[[Callable[..., Any], tuple[Any, ...], dict[str, Any] | None, bool], Thread]

Function to create and manage test threads.

Source code in provide/testkit/threading/basic_fixtures.py
@pytest.fixture
def test_thread() -> Callable[
    [Callable[..., Any], tuple[Any, ...], dict[str, Any] | None, bool],
    threading.Thread,
]:
    """
    Create a test thread with automatic cleanup.

    Returns:
        Function to create and manage test threads.
    """
    threads: list[threading.Thread] = []

    def _create_thread(
        target: Callable[..., Any],
        args: tuple[Any, ...] = (),
        kwargs: dict[str, Any] | None = None,
        daemon: bool = True,
    ) -> threading.Thread:
        """
        Create a test thread.

        Args:
            target: Function to run in thread
            args: Positional arguments for target
            kwargs: Keyword arguments for target
            daemon: Whether thread should be daemon

        Returns:
            Started thread instance
        """
        kwargs = kwargs or {}
        thread = threading.Thread(target=target, args=args, kwargs=kwargs, daemon=daemon)
        threads.append(thread)
        thread.start()
        return thread

    yield _create_thread

    # Cleanup: wait for all threads to complete
    for thread in threads:
        if thread.is_alive():
            thread.join(timeout=1.0)

thread_local_storage

thread_local_storage() -> threading.local

Create thread-local storage for testing.

Returns:

Type Description
local

Thread-local storage object.

Source code in provide/testkit/threading/basic_fixtures.py
@pytest.fixture
def thread_local_storage() -> threading.local:
    """
    Create thread-local storage for testing.

    Returns:
        Thread-local storage object.
    """
    return threading.local()

thread_pool

thread_pool() -> ThreadPoolExecutor

Create a thread pool executor for testing.

Returns:

Type Description
ThreadPoolExecutor

ThreadPoolExecutor instance with automatic cleanup.

Source code in provide/testkit/threading/basic_fixtures.py
@pytest.fixture
def thread_pool() -> ThreadPoolExecutor:
    """
    Create a thread pool executor for testing.

    Returns:
        ThreadPoolExecutor instance with automatic cleanup.
    """
    executor = ThreadPoolExecutor(max_workers=4)
    yield executor
    executor.shutdown(wait=True, cancel_futures=True)