Skip to main content

Stub packages, modules and attributes.

Project description

https://github.com/idlesign/pytest-stub

release lic ci coverage

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]',
})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-stub-1.1.0.tar.gz (5.9 kB view hashes)

Uploaded Source

Built Distribution

pytest_stub-1.1.0-py2.py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page