summaryrefslogtreecommitdiff
path: root/system/blueberry/utils/asserts.py
blob: e0a3530b4228946ebea9dc1c7e0f9776703c20be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Assertions for Blueberry package."""

import contextlib
from typing import Iterator, Type

from mobly import signals


@contextlib.contextmanager
def assert_not_raises(
    exception: Type[Exception] = Exception) -> Iterator[None]:
  """Asserts that the exception is not raised.

  This assertion function is used to catch a specified exception
  (or any exceptions) and raise signal.TestFailure instead.

  Usage:
    ```
    with asserts.assert_not_raises(signals.TestError):
      foo()
    ```

  Args:
    exception: Exception to be catched. If not specify, catch any exceptions as
      default.

  Yields:
    A context which may raise the exception.

  Raises:
    signals.TestFailure: raised when the exception is catched in the context.
  """

  try:
    yield
  except exception as e:  # pylint: disable=broad-except
    raise signals.TestFailure(e)