pytest-stub
https://github.com/idlesign/pytest-stub
Description
Stub packages, modules and attributes.
This pytest plugin allows you to replace dependencies with stubs.
It can be useful if you want to test some code using a dependency without actually having this dependency, for example if you’re testing your library, which uses some parts of another library.
Requirements
Python 3.6+
pytest >= 2.9.0
How to use
You can stub dependencies either with your own custom objects or you may instruct pytest-stub to generate functions or classes for you. Use stub fixture in your test functions, like this:
def test_django_related(stub):
stub.apply({
# Replace `call_command` with a generated function.
'django.core.management.call_command': '[func]',
# Replace `BaseCommand` with a generated class.
'django.core.management.base.BaseCommand': '[cls]',
# Replace `dummy` with generated MagicMock.
'django.dummy': '[mock]',
# Replace entire `cv2` module.
'cv2': '[mock]',
# Sometimes we need just a persistent (always the same) magic mock.
'numpy': '[mock_persist]',
# Stub multiple attributes in the same module with custom objects.
'django.conf': {
'settings': object(),
'some': True,
},
})
If we want to replace some dependency with a stub not in a fixture but globally, we can use stub_global() function in root conftest.py (this code will apply patch before tests, so tests will be safe to import code using dependencies).
from pytest_stub.toolbox import stub_global
stub_global({
'cv2': '[mock_persist]',
})
Release files for pytest-stub 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest-stub-1.1.0.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_stub-1.1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 11.6 kB
Release files / pytest-stub-1.1.0.tar.gz
| Download URL | pytest-stub-1.1.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
276043d91cbad863ba56b99f6b781a4a1bc19e1d2e928a5f8e76979ee02c8099
|
|
BLAKE2b-256 checksum How to use checksums |
3267d22676636e337c058b15178bcf6e3f1cd71b8fd52782791c46152bd79e4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.6
|
Release files / pytest_stub-1.1.0-py2.py3-none-any.whl
| Download URL | pytest_stub-1.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
5de0e3247f8e51321c4dc6d94bbe53c8a96a51edcf722d3df3cb20d4d32b27d7
|
|
BLAKE2b-256 checksum How to use checksums |
343013f00e1af36577df3e355827cdc02e1a7cbb4fce31966a72a692573ae683
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.6
|