pytest-object-getter 1.0.2

Last updated:

0 purchases

pytest-object-getter 1.0.2 Image
pytest-object-getter 1.0.2 Images
Add to Cart

Description:

pytestobjectgetter 1.0.2

PYTEST OBJECT GETTER
A Pytest Plugin providing the get_object fixture.






Code: https://github.com/boromir674/pytest-object-getter
Docs: https://pytest-object-getter.readthedocs.io/en/master/
PyPI: https://pypi.org/project/pytest-object-getter/
CI: https://github.com/boromir674/pytest-object-getter/actions/



Highlights

pytest_object_getter python package, hosted on pypi.org

Installable with pip
get_object fixture available to your tests

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
Alter the bahaviour of an object at runtime




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:

GNU Affero General Public License v3.0





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

Code Coverage measuring
Build Command, using the build python package
Pypi Deploy Command, supporting upload to both pypi.org and test.pypi.org servers
Type Check Command, using mypy
Lint Check and Apply commands, using isort and black


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

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.