summary refs log tree commit diff
path: root/tests/unittest.py
diff options
context:
space:
mode:
authorRichard van der Hoff <1389908+richvdh@users.noreply.github.com>2021-01-07 11:41:28 +0000
committerGitHub <noreply@github.com>2021-01-07 11:41:28 +0000
commit8d3d264052adffaf9ef36a6d9235aeeb8bef5fb5 (patch)
tree3383923f7df6ea3f5cce37eca113258350309379 /tests/unittest.py
parentHandle updating schema version without any deltas. (#9033) (diff)
downloadsynapse-8d3d264052adffaf9ef36a6d9235aeeb8bef5fb5.tar.xz
Skip unit tests which require optional dependencies (#9031)
If we are lacking an optional dependency, skip the tests that rely on it.
Diffstat (limited to '')
-rw-r--r--tests/unittest.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/tests/unittest.py b/tests/unittest.py

index af7f752c5a..bbd295687c 100644 --- a/tests/unittest.py +++ b/tests/unittest.py
@@ -20,7 +20,7 @@ import hmac import inspect import logging import time -from typing import Dict, Iterable, Optional, Tuple, Type, TypeVar, Union +from typing import Callable, Dict, Iterable, Optional, Tuple, Type, TypeVar, Union from mock import Mock, patch @@ -736,3 +736,29 @@ def override_config(extra_config): return func return decorator + + +TV = TypeVar("TV") + + +def skip_unless(condition: bool, reason: str) -> Callable[[TV], TV]: + """A test decorator which will skip the decorated test unless a condition is set + + For example: + + class MyTestCase(TestCase): + @skip_unless(HAS_FOO, "Cannot test without foo") + def test_foo(self): + ... + + Args: + condition: If true, the test will be skipped + reason: the reason to give for skipping the test + """ + + def decorator(f: TV) -> TV: + if not condition: + f.skip = reason # type: ignore + return f + + return decorator