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.Mock

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

Parameters:

Name Type Description Default
return_value Optional[Any]

return the given value

None
return_call Optional[Callable[..., Optional[Any]]]

return the value returned by the given callable

None
return_once Optional[Any]

return the given value exactly once

None
return_each Optional[Sequence[Any]]

consecutively return each element of the given iterable

None
return_exception Optional[Exception]

raise the given exception

None
**kwargs Any

passed to Mock(...)

{}

Returns:

Type 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.Mock

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

Parameters:

Name Type Description Default
return_value Optional[Any]

return the given value

None
return_call Optional[Callable[..., Optional[Any]]]

return the value returned by the given callable

None
return_once Optional[Any]

return the given value exactly once

None
return_each Optional[Sequence[Any]]

consecutively return each element of the given iterable

None
return_exception Optional[Exception]

raise the given exception

None
**kwargs Any

passed to Mock(...)

{}

Returns:

Type 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.

Parameters:

Name Type Description Default
*args FastAPI

FastAPI instance(s) to patch

()
overrides Optional[Dict[Callable[..., Any], Callable[..., Any]]]

A mapping of overrides, or None to remove all

required
remove bool

Remove the provided overrides

False

Raises:

Type 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)