marsh.testing

Tools that can be used for testing purposes.

catch_cause(base=<class 'marsh.errors.MarshError'>, cause=None)[source]

Assert that an error is thrown in this context.

Similar to pytest.raises() but allows for inspecting and finding a specific cause for the error raised by traversing the __cause__ attribute chain.

Parameters:
  • base (Type[Exception]) – The base exception class to catch. If not caught, the test fails.

  • cause (Optional[Type[Exception]]) – If not None, at least one of the exceptions in the cause chain must be an instance of this class.

Return type:

Iterator[None]

Returns:

Context manager.

marshal_succeeds(value, element)[source]

Assert that a value can be marshaled.

The element is also compared to the expected output for the marshal.

Parameters:
Return type:

Union[None, int, float, bool, str, Sequence[Any], Mapping[str, Any]]

Returns:

The marshaled value if successful.

marshal_fails(value, exception=None)[source]

Assert that a value can not be marshaled.

Requires that marshaling of the input value raises marsh.errors.MarshalError.

Parameters:
  • value (Any) – Value to marshal.

  • exception (Optional[Type[Exception]]) – Optional cause that must exist in the exception chain if given.

Return type:

None

unmarshal_succeeds(type_, element, value)[source]

Assert that a value is unmarshaled correctly.

Parameters:
Return type:

TypeVar(_T)

Returns:

The unmarshaled result if successful.

unmarshal_fails(type_, element, exception=None)[source]

Assert that a value can not be unmarshaled.

Requires that unmarshaling raises marsh.errors.UnmarshalError.

Parameters:
Return type:

None