Giter VIP home page Giter VIP logo

mocksafe's People

Contributors

dmayo3 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

mocksafe's Issues

Support for mocking modules

Currently there's first class support for mocking classes, but mocking modules in a type-safe way is cumbersome and requires defining a class that mirrors the module structure.

For example, here is a subset of the requests library being defined as a class type. It would never used in production, only to facilitate type-safe mocking. It calls through to the original functions to validate the call signatures line up, but this code is never exercised.

It can then be mocked using mock(RequestsInterface).

class RequestsInterface:
    def get(self, url: str, params: dict | None=None, **kwargs) -> Response:
        return requests.get(url, params, **kwargs)

    def post(self, url: str, data=None, json=None, **kwargs) -> Response:
        return requests.post(url, data, json, **kwargs)

    def delete(self, url: str, **kwargs) -> Response:
        return requests.delete(url, **kwargs)

Support Python < 3.10

Support 3.9 if not earlier.

This means dropping some of the more recent improvements such as the new union type syntax foo | bar.

Set up badges

  • GHA
  • RTD
  • PyPI
  • License
  • Python versions
  • black formatting
  • mypy

Improve error feedback

Use custom exceptions, enhance the error messages, see if we can filter the trace back or something. Document expected error cases.

Add pytest fixture plugin for easy patching

Sometimes it's useful to be able to monkeypatch attributes on a module to swap a real implementation for a mocked version.

It would be useful to provide a fixture that can be easily used to patch and mock an attribute at the same time.

I attempted it, but I couldn't quite figure out how to make it work.

Here is the fixture I came up with while experimenting, which would then need to be exposed by mocksafe as a pytest plugin.

from mocksafe import mock
from typing import TypeVar, Callable

T = TypeVar('T')

class _Patcher:
    def __init__(self, monkeypatch):
        self._monkeypatch = monkeypatch

    def patch(self, obj, attr: str, class_type: type[T]) -> T:
            mock_obj: T = mock(class_type)
            self._monkeypatch.setattr(obj, attr, mock_obj)
            return mock_obj

Patcher = Callable[[object, str, type[T]], T]

@pytest.fixture
def patch(monkeypatch) -> Patcher:
    return _Patcher(monkeypatch).patch

The Patcher is a type that can be imported and used on the patch fixture, something like so:

@pytest.fixture
def mock_foo(patch: Patcher) -> ClassToMock:
    return patch(real_object, "attribute_to_mock", ClassToMock)

Update docs

  • Clarify default stubbing behaviour
  • Explain module mocking workaround
  • Explain limited property mocking support
  • Explain limited field attribute support

Publish test package to PyPI

  • Publish
  • Update README to better suit pypi
  • Link out to the read the docs page
  • Merge pyproject.toml tweaks to main

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.