Skip to main content

Import any object from a 3rd party module while mocking its namespace on demand.

Project description

PYTEST OBJECT GETTER

A Pytest Plugin providing the get_object fixture.

GitHub Workflow Status (branch) Read the Docs (version) Codecov Code Climate Maintainability Better Code Hub Technical Debt
Production Version PyPI - Wheel Supported Python versions GitHub GitHub commits since tagged version (branch) GitHub commits since latest release (by SemVer)

Highlights

  1. pytest_object_getter python package, hosted on pypi.org

    • Installable with pip

    • get_object fixture available to your tests

      1. Dynamically import an object from a module

      2. Optionally mock any object that is present in the module’s namespace

      3. Construct the mock object at runtime

      4. Alter the bahaviour of an object at runtime

  2. Tested against multiple platforms and python versions

    • platforms: Ubuntu, MacOS

    • python: 3.6, 3.7, 3.8, 3.9, 3.10

For more, see the CI Pipeline and the Test workflow, defined in test.yaml.

You can read more on pytest and fixtures in pytest latest documentation.

Quickstart

Prerequisites

You need to have Python installed.

Installing

Using pip is the approved way for installing pytest_object_getter.

python3 -m pip install pytest_object_getter

After installation the get_object pytest fixture should be available in your tests.

A Use Case

Let’s see how to write a test and use the ‘get_object’ fixture to mock the requests.get method to avoid actual network communication:

python3 -m pip install ask-pypi
import pytest

@pytest.fixture
def mock_response():
    def init(self, package_name: str):
        self.status_code = 200 if package_name == 'existing-package' else 404
    return type('MockResponse', (), {
        '__init__': init
    })

@pytest.fixture
def create_mock_requests(mock_response):
    def _create_mock_requests():
        def mock_get(*args, **kwargs):
            package_name = args[0].split('/')[-1]
            return mock_response(package_name)
        return type('MockRequests', (), {
            'get': mock_get,
        })
    return _create_mock_requests

def test_fixture(get_object, create_mock_requests):

    from ask_pypi import is_pypi_project

    assert is_pypi_project('numpy') == True
    assert is_pypi_project('pandas') == True
    assert is_pypi_project('existing-package') == False

    get_object('is_project', 'ask_pypi.pypi_project',
        overrides={'requests': lambda: create_mock_requests()})

    assert is_pypi_project('existing-package') == True

    assert is_pypi_project('numpy') == False
    assert is_pypi_project('pandas') == False
    assert is_pypi_project('so-magic') == False

License

Free software:

GitHub

Development

Here are some useful notes related to doing development on this project.

  1. Test Suite, using pytest, located in tests dir

  2. Parallel Execution of Unit Tests, on multiple cpu’s

  3. Documentation Pages, hosted on readthedocs server, located in docs dir

  4. Automation, using tox, driven by single tox.ini file

    1. Code Coverage measuring

    2. Build Command, using the build python package

    3. Pypi Deploy Command, supporting upload to both pypi.org and test.pypi.org servers

    4. Type Check Command, using mypy

    5. Lint Check and Apply commands, using isort and black

  5. CI Pipeline, running on Github Actions, defined in .github/

    1. Job Matrix, spanning different platform’s and python version’s

      1. Platforms: ubuntu-latest, macos-latest

      2. Python Interpreters: 3.6, 3.7, 3.8, 3.9, 3.10

    2. Parallel Job execution, generated from the matrix, that runs the Test Suite

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_object_getter-1.0.2.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_object_getter-1.0.2-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_object_getter-1.0.2.tar.gz.

File metadata

  • Download URL: pytest_object_getter-1.0.2.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.0 importlib_metadata/4.11.4 packaging/21.3 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.10

File hashes

Hashes for pytest_object_getter-1.0.2.tar.gz
Algorithm Hash digest
SHA256 482c6e407d3e67a0de977437434f923a5fcd02503663c19bfccda9985b9d0649
MD5 2f51f041e38528924b6e506fc56d775c
BLAKE2b-256 02fed80930717c5e71028c9fd245cbf647da2062994d49d627c0b3147309abf6

See more details on using hashes here.

File details

Details for the file pytest_object_getter-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_object_getter-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.0 importlib_metadata/4.11.4 packaging/21.3 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.10

File hashes

Hashes for pytest_object_getter-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1ca9f06f6827ceeb1621e49e9095401130ae051b45beb516882ca1b4af598dd7
MD5 9f80f387ff32a1c661bbf53c02d2112b
BLAKE2b-256 d67346e99f3099f325310261482f9342c9cd719b08c78e23e83615cf464fa20f

See more details on using hashes here.

Supported by

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