Registry / testing / pytest-stub

pytest-stub

JSON →
library1.1.0pypypi✓ verified 89d ago

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.

pip install pytest-stub
INSTALL
IMPORT
SIG · PYTEST-STUB
P
pytest-stub
testingpythonv1.1.0
Install
2.7s avg
Import
664ms
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.708s · 30.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 2.7s · import 0.620s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stub
✓ from pytest_stub import stub
✗ import pytest_stub
Using 'import pytest_stub' works but you then need to call pytest_stub.stub(...); direct import is typical.

Define a fixture that stubs `random.randint` to always return 42.

import pytest from pytest_stub import stub @pytest.fixture def stubbed_random(): with stub('random.randint') as m: m.return_value = 42 yield def test_random(stubbed_random): import random assert random.randint(1, 10) == 42
Debug
Known issues
gotchaDo not use `stub` in a global context; always use as a context manager or fixture. Otherwise stubs may persist across tests.
fix
Use `with stub('...') as m:` inside a test or fixture.
affects: all
gotchaThe stub target must be an importable path; e.g., 'package.module.attr'. Stubbing builtins (like 'print') does not work because builtins are not modules.
fix
Only stub attributes reachable via a module path.
affects: all
Errors
Common errors & fixes
AttributeError: module 'pytest_stub' has no attribute 'stub'
Outdated version of pytest-stub (<1.0) or incorrect import: the `stub` function may not exist in older versions.
fix
Upgrade to latest: `pip install --upgrade pytest-stub` and import `from pytest_stub import stub`.
TypeError: 'str' object is not callable
Attempting to call the stub object (returned from stub() context manager) directly instead of using `.return_value`.
fix
Access the mock object via the context variable and set `.return_value`: e.g., `with stub('x') as m: m.return_value = 1`.
Upgrade
Version history
1.1.0latest on PyPI · released Apr 28, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
pytest-stub — pip install pytest-stub · libregistry