{"id":21813,"library":"pytest-stub","title":"pytest-stub","description":"pytest-stub is a plugin for the pytest framework that allows you to conveniently stub packages, modules, attributes, and functions for testing purposes. Version 1.1.0 is actively maintained. Release cadence is low, with occasional updates.","status":"active","version":"1.1.0","language":"python","source_language":"en","source_url":"https://github.com/idlesign/pytest-stub","tags":["testing","pytest","stub","mock","plugin"],"install":[{"cmd":"pip install pytest-stub","lang":"bash","label":"Install via pip"}],"dependencies":[],"imports":[{"note":"Using 'import pytest_stub' works but you then need to call pytest_stub.stub(...); direct import is typical.","wrong":"import pytest_stub","symbol":"stub","correct":"from pytest_stub import stub"}],"quickstart":{"code":"import pytest\nfrom pytest_stub import stub\n\n@pytest.fixture\ndef stubbed_random():\n    with stub('random.randint') as m:\n        m.return_value = 42\n        yield\n\ndef test_random(stubbed_random):\n    import random\n    assert random.randint(1, 10) == 42","lang":"python","description":"Define a fixture that stubs `random.randint` to always return 42."},"warnings":[{"fix":"Use `with stub('...') as m:` inside a test or fixture.","message":"Do not use `stub` in a global context; always use as a context manager or fixture. Otherwise stubs may persist across tests.","severity":"gotcha","affected_versions":"all"},{"fix":"Only stub attributes reachable via a module path.","message":"The stub target must be an importable path; e.g., 'package.module.attr'. Stubbing builtins (like 'print') does not work because builtins are not modules.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Upgrade to latest: `pip install --upgrade pytest-stub` and import `from pytest_stub import stub`.","cause":"Outdated version of pytest-stub (<1.0) or incorrect import: the `stub` function may not exist in older versions.","error":"AttributeError: module 'pytest_stub' has no attribute 'stub'"},{"fix":"Access the mock object via the context variable and set `.return_value`: e.g., `with stub('x') as m: m.return_value = 1`.","cause":"Attempting to call the stub object (returned from stub() context manager) directly instead of using `.return_value`.","error":"TypeError: 'str' object is not callable"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}