Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/idlesign/pytest-stub
Stub packages, modules and attributes.
https://github.com/idlesign/pytest-stub
pytest pytest-plugin python python3 stub stubbing testing
Last synced: 24 days ago
JSON representation
Stub packages, modules and attributes.
- Host: GitHub
- URL: https://github.com/idlesign/pytest-stub
- Owner: idlesign
- License: bsd-3-clause
- Created: 2018-12-27T14:37:42.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-28T10:31:20.000Z (over 4 years ago)
- Last Synced: 2024-10-03T11:24:02.749Z (about 1 month ago)
- Topics: pytest, pytest-plugin, python, python3, stub, stubbing, testing
- Language: Python
- Homepage: https://github.com/idlesign/pytest-stub
- Size: 21.5 KB
- Stars: 9
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG
- Contributing: CONTRIBUTING
- License: LICENSE
Awesome Lists containing this project
README
pytest-stub
===========
https://github.com/idlesign/pytest-stub|release| |lic| |ci| |coverage|
.. |release| image:: https://img.shields.io/pypi/v/pytest-stub.svg
:target: https://pypi.python.org/pypi/pytest-stub.. |lic| image:: https://img.shields.io/pypi/l/pytest-stub.svg
:target: https://pypi.python.org/pypi/pytest-stub.. |ci| image:: https://img.shields.io/travis/idlesign/pytest-stub/master.svg
:target: https://travis-ci.org/idlesign/pytest-stub.. |coverage| image:: https://img.shields.io/coveralls/idlesign/pytest-stub/master.svg
:target: https://coveralls.io/r/idlesign/pytest-stubDescription
-----------*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.0How 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:.. code-block:: python
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)... code-block:: python
from pytest_stub.toolbox import stub_global
stub_global({
'cv2': '[mock_persist]',
})