parametrize_from_file.error(exc_spec, *, globals=None)

Create a context manager that will check that the specified exception is raised.

  • exc_spec (str, list, dict) –

    This argument specifies what kind of exception to expect (and optionally how to check various aspects of it).

    If string: A string that evaluates to an exception type. This can also be "none" to specify that the returned context manager should not expect any exception to be raised.

    If list: A list of the above strings that evaluate to exception types. The actual exception must be one of these types.

    If dict: The following keys are understood:

    • ”type” (required): A string or list of strings that evaluate to exception types. The context manager will require that an exception of one of the given types is raised.

    • ”message” (optional): A string or list of strings that should appear verbatim in the error message.

    • ”pattern” (optional): A string or list of strings representing patterns that should appear in the error message. Each string is interpreted as a regular expression, in the same manner as the match argument to pytest.raises.

    • ”attrs” (optional): A dictionary of attributes that the exception object should have. The dictionary keys give the attribute names, and should be strings. The dictionary values give the attribute values, and should also be strings. Each value string will be evaluated in the context of the given namespace to get the expected values.

    • ”assertions” (optional): A string containing python code that will be executed when the expected exception is detected. Any globals specified will be available to this code. In addition, the exception object itself will be available via the exc variable. This field is typically used to make free-form assertions about the exception object.

    • ”cause” (optional): An integer (or string that can be converted to an integer) ‘n’. All other checks will apply to the n-th direct cause of the caught exception, instead of the exception itself. This is useful in cases where the exception you want to test is wrapped in some other exception.

    Note that string values are accepted for every one of these keys, because this method is meant to help with parsing exception information from a text file, e.g. in the NestedText format. All evaluations and executions are deferred for as long as possible.

  • globals (dict) – The global variables to use when evaluating/executing code related to the exception. This can be any kind of mapping, including Namespace. By default, only the built-in variables are available.


A context manager that can be used to check if the kind of exception specified by exc_spec was raised.


Using a built-in exception (so no need to specify a namespace) and not checking the error message:

>>> p = {'type': 'ZeroDivisionError'}
>>> with error(p):
...    1/0

Using a custom exception:

>>> class MyError(Exception):
...     pass
>>> with_err = Namespace(MyError=MyError)
>>> p = {'type': 'MyError', 'pattern': r'\d+'}
>>> with with_err.error(p):
...    raise MyError('404')


The returned context manager is re-entrant, which makes it possible to stack @parametrize_from_file invocations that make use of method (e.g. via the schema argument).