pytest_object_getter package¶
Module contents¶
- pytest_object_getter.generic_object_getter_class(attribute_getter, monkeypatch)[source]¶
Class instances can extract a requested object from within a module and optionally patch any object in the module’s namespace at runtime.
- pytest_object_getter.get_object(object_getter_class)[source]¶
Import an object from a module and optionally mock any object in its namespace.
A callable that can import an object, given a reference (str), from a module , given its “path” (string represented as ‘dotted’ modules: same way python code imports modules), and provide the capability to monkeypatch/mock any object found in the module’s namespace at runtime.
The client code must supply the first 2 arguments at runtime, correspoding to the object’s symbol name (str) and module “path” (str).
The client code can optionally use the ‘overrides’ kwarg to supply a python dictionary to specify what runtime objects to mock and how.
Each dictionary entry should model your intention to monkeypatch one of the module namespace’ objects with a custom ‘mock’ value.
Each dictionary key should be a string corresponding to an object’s reference name (present in the module’s namespace) and each value should be a callable that can construct the ‘mock’ value. The callable should take no arguments and acts as a “factory”, that when called should provide the ‘mock’ value.
Example
def mocked_request_get() business_method = get_object(
“business_method”, “business_package.methods”, overrides={“production”: lambda: ‘mocked’}
)
- Parameters
symbol (str) – the object’s reference name
module (str) – the module ‘path’ represented as module names “joined” by “.” (dots)
overrides (dict, optional) – declare what to monkeypatch and with what “mocks”. Defaults to None.
- Returns
the object imported from the module with its namespace potentially mocked
- Return type
Any
- pytest_object_getter.object_getter_class(generic_object_getter_class)[source]¶
Do a dynamic import of a module and get an object from its namespace.
This fixture returns a Python Class that can do a dynamic import of a module and get an object from its namespace.
Instances of this class are callable’s (they implement the __call__ protocol ) and uppon calling the return a reference to the object “fetched” from the namespace.
Callable instances arguments: * 1st: object with the ‘symbol_namel’: str and ‘object_module_string’: str
attributes expected “on it”
- Returns
Class that can do a dynamic import and get an object
- Return type
ObjectGetter