Import any object from a 3rd party module while mocking its namespace on demand.
Project description
PYTEST OBJECT GETTER
Project entirely generated using Generator https://github.com/boromir674/cookiecutter-python-package/
Features
pytest_object_getter python package
Dynamically import an object from a module
Optionally mock any object that is present in the module’s namespace
Construct the mock object at runtime
Tested against multiple platforms and python versions
Development
Here are some useful notes related to doing development on this project.
Test Suite, using pytest, located in tests dir
Parallel Execution of Unit Tests, on multiple cpu’s
Documentation Pages, hosted on readthedocs server, located in docs dir
Automation, using tox, driven by single tox.ini file
CI Pipeline, running on Github Actions, defined in .github/
Job Matrix, spanning different platform’s and python version’s
Platforms: ubuntu-latest, macos-latest
Python Interpreters: 3.6, 3.7, 3.8, 3.9, 3.10
Parallel Job execution, generated from the matrix, that runs the Test Suite
Prerequisites
You need to have Python installed.
Quickstart
Using pip is the approved way for installing pytest_object_getter.
python3 -m pip install pytest_object_getter
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
License
Free software: GNU Affero General Public License v3.0
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
Built Distribution
Hashes for pytest_object_getter-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e827c4c1ab8da0de9f8377da6509e1b980a380cb0b983cf9a916a61421d3f78a |
|
MD5 | 5ba0e7800e05e768042a08a4b0fd0e06 |
|
BLAKE2b-256 | 7b5ccf6bc3eaf6100e66744473aade5d65ac272e650cc273dac33ca1c0b6e670 |
Hashes for pytest_object_getter-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4f6c18267e29094fa9f63f0559dd8dc80fa385b38907b61d4d37e4d3d96f1e3 |
|
MD5 | 7d6523bcc914e8d3a76ab00687f3bac3 |
|
BLAKE2b-256 | 0a7fa29673457fb41a2d8808945e6013b06f4411ef270fca59ab8cd730b82512 |