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-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:
.. 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]',
})
Raw data
{
"_id": null,
"home_page": "https://github.com/idlesign/pytest-stub",
"name": "pytest-stub",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Igor `idle sign` Starikov",
"author_email": "idlesign@yandex.ru",
"download_url": "https://files.pythonhosted.org/packages/32/67/d22676636e337c058b15178bcf6e3f1cd71b8fd52782791c46152bd79e4c/pytest-stub-1.1.0.tar.gz",
"platform": "",
"description": "pytest-stub\n===========\nhttps://github.com/idlesign/pytest-stub\n\n|release| |lic| |ci| |coverage|\n\n.. |release| image:: https://img.shields.io/pypi/v/pytest-stub.svg\n :target: https://pypi.python.org/pypi/pytest-stub\n\n.. |lic| image:: https://img.shields.io/pypi/l/pytest-stub.svg\n :target: https://pypi.python.org/pypi/pytest-stub\n\n.. |ci| image:: https://img.shields.io/travis/idlesign/pytest-stub/master.svg\n :target: https://travis-ci.org/idlesign/pytest-stub\n\n.. |coverage| image:: https://img.shields.io/coveralls/idlesign/pytest-stub/master.svg\n :target: https://coveralls.io/r/idlesign/pytest-stub\n\n\nDescription\n-----------\n\n*Stub packages, modules and attributes.*\n\nThis pytest plugin allows you to replace dependencies with stubs.\n\nIt can be useful if you want to test some code using a dependency without actually having this dependency,\nfor example if you're testing your library, which uses some parts of another library.\n\n\nRequirements\n------------\n\n* Python 3.6+\n* pytest >= 2.9.0\n\n\nHow to use\n----------\n\nYou can stub dependencies either with your own custom objects or you may instruct ``pytest-stub``\nto generate functions or classes for you. Use ``stub`` fixture in your test functions, like this:\n\n.. code-block:: python\n\n def test_django_related(stub):\n\n stub.apply({\n # Replace `call_command` with a generated function.\n 'django.core.management.call_command': '[func]',\n\n # Replace `BaseCommand` with a generated class.\n 'django.core.management.base.BaseCommand': '[cls]',\n\n # Replace `dummy` with generated MagicMock.\n 'django.dummy': '[mock]',\n\n # Replace entire `cv2` module.\n 'cv2': '[mock]',\n\n # Sometimes we need just a persistent (always the same) magic mock.\n 'numpy': '[mock_persist]',\n\n # Stub multiple attributes in the same module with custom objects.\n 'django.conf': {\n 'settings': object(),\n 'some': True,\n },\n\n })\n\nIf we want to replace some dependency with a stub not in a fixture but globally, we can use ``stub_global()`` function\nin root ``conftest.py`` (this code will apply patch before tests, so tests will be safe to import code using dependencies).\n\n.. code-block:: python\n\n from pytest_stub.toolbox import stub_global\n\n stub_global({\n 'cv2': '[mock_persist]',\n })\n\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License",
"summary": "Stub packages, modules and attributes.",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/idlesign/pytest-stub"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "343013f00e1af36577df3e355827cdc02e1a7cbb4fce31966a72a692573ae683",
"md5": "cda735b5059c6cc8cbd0b498873751b1",
"sha256": "5de0e3247f8e51321c4dc6d94bbe53c8a96a51edcf722d3df3cb20d4d32b27d7"
},
"downloads": -1,
"filename": "pytest_stub-1.1.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "cda735b5059c6cc8cbd0b498873751b1",
"packagetype": "bdist_wheel",
"python_version": "3.6",
"requires_python": null,
"size": 5700,
"upload_time": "2020-04-28T10:31:23",
"upload_time_iso_8601": "2020-04-28T10:31:23.943262Z",
"url": "https://files.pythonhosted.org/packages/34/30/13f00e1af36577df3e355827cdc02e1a7cbb4fce31966a72a692573ae683/pytest_stub-1.1.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3267d22676636e337c058b15178bcf6e3f1cd71b8fd52782791c46152bd79e4c",
"md5": "0218753ad620667547479794c1f4a1dc",
"sha256": "276043d91cbad863ba56b99f6b781a4a1bc19e1d2e928a5f8e76979ee02c8099"
},
"downloads": -1,
"filename": "pytest-stub-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "0218753ad620667547479794c1f4a1dc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5852,
"upload_time": "2020-04-28T10:31:22",
"upload_time_iso_8601": "2020-04-28T10:31:22.426263Z",
"url": "https://files.pythonhosted.org/packages/32/67/d22676636e337c058b15178bcf6e3f1cd71b8fd52782791c46152bd79e4c/pytest-stub-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-04-28 10:31:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "idlesign",
"github_project": "pytest-stub",
"travis_ci": true,
"coveralls": true,
"github_actions": false,
"tox": true,
"lcname": "pytest-stub"
}