Coverage for bioimageio/core/utils/testing.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-19 09:02 +0000

1# TODO: move to tests/ 

2from functools import wraps 

3from typing import Any, Protocol, Type 

4 

5 

6class test_func(Protocol): 

7 def __call__(*args: Any, **kwargs: Any): ... 

8 

9 

10def skip_on(exception: Type[Exception], reason: str): 

11 """adapted from https://stackoverflow.com/a/63522579""" 

12 import pytest 

13 

14 # Func below is the real decorator and will receive the test function as param 

15 def decorator_func(f: test_func): 

16 @wraps(f) 

17 def wrapper(*args: Any, **kwargs: Any): 

18 try: 

19 # Try to run the test 

20 return f(*args, **kwargs) 

21 except exception: 

22 # If exception of given type happens 

23 # just swallow it and raise pytest.Skip with given reason 

24 pytest.skip(reason) 

25 

26 return wrapper 

27 

28 return decorator_func