forked from dds-bridge/dds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
38 lines (32 loc) · 1.15 KB
/
Copy pathtest_utils.py
File metadata and controls
38 lines (32 loc) · 1.15 KB
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
38
"""Shared test utilities for Python test modules."""
import re
from typing import Optional, Type, Union
def assert_raises(
expected_exception: Union[Type[BaseException], tuple],
func,
*args,
match: Optional[str] = None,
**kwargs
) -> None:
"""Assert that a callable raises an expected exception type.
Args:
expected_exception: Exception type or tuple of types to expect
func: Callable to invoke
*args: Positional arguments to pass to func
match: Optional regex pattern to match in exception message
**kwargs: Keyword arguments to pass to func
Raises:
AssertionError: If expected exception is not raised
"""
try:
func(*args, **kwargs)
except expected_exception as exc:
if match is not None:
if not re.search(match, str(exc)):
raise AssertionError(f"Pattern '{match}' not found in '{exc}'")
return
except Exception as exc:
raise AssertionError(
f"Expected {expected_exception}, got {type(exc).__name__}: {exc}"
)
raise AssertionError(f"Expected {expected_exception} to be raised")