Skip unit tests which require optional dependencies (#9031)

If we are lacking an optional dependency, skip the tests that rely on it.
This commit is contained in:
Richard van der Hoff 2021-01-07 11:41:28 +00:00 committed by GitHub
parent eee3c3c52f
commit 8d3d264052
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 14 deletions

View file

@ -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