| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/test_base.py |
| 4 |
# |
| 5 |
# Copyright 2019 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
|
| 11 |
import re |
| 12 |
import unittest |
| 13 |
from contextlib import contextmanager |
| 14 |
from typing import Type |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.usefixtures('unit_test_console_tool_class', 'b2_uri_args') |
| 20 |
class TestBase(unittest.TestCase): |
| 21 |
console_tool_class: Type |
| 22 |
|
| 23 |
@contextmanager |
| 24 |
def assertRaises(self, exc, msg=None): |
| 25 |
try: |
| 26 |
yield |
| 27 |
except exc as e: |
| 28 |
if msg is not None: |
| 29 |
if msg != str(e): |
| 30 |
assert False, f"expected message '{msg}', but got '{str(e)}'" |
| 31 |
else: |
| 32 |
assert False, f'should have thrown {exc}' |
| 33 |
|
| 34 |
@contextmanager |
| 35 |
def assertRaisesRegexp(self, expected_exception, expected_regexp): |
| 36 |
try: |
| 37 |
yield |
| 38 |
except expected_exception as e: |
| 39 |
if not re.search(expected_regexp, str(e)): |
| 40 |
assert False, f"expected message '{expected_regexp}', but got '{str(e)}'" |
| 41 |
else: |
| 42 |
assert False, f'should have thrown {expected_exception}' |