Skip to content

mockish.Mock

Functions

AsyncMock

AsyncMock(
    *,
    return_value: Optional[Any] = None,
    return_call: Optional[
        Callable[..., Optional[Any]]
    ] = None,
    return_once: Optional[Any] = None,
    return_each: Optional[Sequence[Any]] = None,
    return_exception: Optional[Exception] = None,
    **kwargs: Any
) -> Mock

Same as mockish.Mock, but returns an async Mock.

PARAMETER DESCRIPTION
return_value

return the given value

TYPE: Optional[Any] DEFAULT: None

return_call

return the value returned by the given callable

TYPE: Optional[Callable[..., Optional[Any]]] DEFAULT: None

return_once

return the given value exactly once

TYPE: Optional[Any] DEFAULT: None

return_each

consecutively return each element of the given iterable

TYPE: Optional[Sequence[Any]] DEFAULT: None

return_exception

raise the given exception

TYPE: Optional[Exception] DEFAULT: None

**kwargs

passed to Mock(...)

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Mock

...

Mock

Mock(
    *,
    return_value: Optional[Any] = None,
    return_call: Optional[
        Callable[..., Optional[Any]]
    ] = None,
    return_once: Optional[Any] = None,
    return_each: Optional[Sequence[Any]] = None,
    return_exception: Optional[Exception] = None,
    **kwargs: Any
) -> Mock

A thin wrapper around unittest.mock.Mock to abstract away the use of side_effect in favor of these explicit return_X parameters:

PARAMETER DESCRIPTION
return_value

return the given value

TYPE: Optional[Any] DEFAULT: None

return_call

return the value returned by the given callable

TYPE: Optional[Callable[..., Optional[Any]]] DEFAULT: None

return_once

return the given value exactly once

TYPE: Optional[Any] DEFAULT: None

return_each

consecutively return each element of the given iterable

TYPE: Optional[Sequence[Any]] DEFAULT: None

return_exception

raise the given exception

TYPE: Optional[Exception] DEFAULT: None

**kwargs

passed to Mock(...)

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Mock

...

Examples:

Import

>>> from mockish import Mock
  • return_value
>>> obj = Mock(return_value="hello world")
>>> obj()
'hello world'
  • return_call
>>> func = lambda: "hello world"
>>> obj = Mock(return_call=func)
>>> obj()
'hello world'
  • return_once
>>> obj = Mock(return_once="hello world")
>>> obj()
'hello world'
>>> obj()
Traceback (most recent call last):
...
StopIteration
  • return_each
>>> obj = Mock(return_each=[1, 2, 3])
>>> obj()
1
>>> obj()
2
>>> obj()
3
>>> obj()
Traceback (most recent call last):
...
StopIteration
  • return_exception:
>>> obj = Mock(return_exception=ValueError("hello world"))
>>> obj()
Traceback (most recent call last):
...
ValueError: hello world

patch_fastapi_dependencies

patch_fastapi_dependencies(
    *args: FastAPI,
    overrides: Optional[
        dict[Callable[..., Any], Callable[..., Any]]
    ],
    remove: bool = False
) -> None

Recursively patch dependencies of FastAPI instance(s).

Read about FastAPI test dependencies.

Note: fastapi must be installed.

PARAMETER DESCRIPTION
*args

FastAPI instance(s) to patch

TYPE: FastAPI DEFAULT: ()

overrides

A mapping of overrides, or None to remove all

TYPE: Optional[dict[Callable[..., Any], Callable[..., Any]]]

remove

Remove the provided overrides

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
TypeError

raised if any of args is not a FastAPI instance

Examples:

from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI(...)

mockish.patch_fastapi_dependencies(
    app,
    overrides={
        get_app_settings: lambda: app_settings,
    },
)


@app.get("/")
def get() -> dict[str, str]:
    return {"hello": "world"}


return TestClient(app)