"""Decorators for FURY tests."""importreimportosSKIP_RE=re.compile("(\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$")
[docs]defdoctest_skip_parser(func):"""Decorator replaces custom skip test markup in doctests. Say a function has a docstring:: >>> something # skip if not HAVE_AMODULE >>> something + else >>> something # skip if HAVE_BMODULE This decorator will evaluate the expresssion after ``skip if``. If this evaluates to True, then the comment is replaced by ``# doctest: +SKIP``. If False, then the comment is just removed. The expression is evaluated in the ``globals`` scope of `func`. For example, if the module global ``HAVE_AMODULE`` is False, and module global ``HAVE_BMODULE`` is False, the returned function will have docstring:: >>> something # doctest: +SKIP >>> something + else >>> something """lines=func.__doc__.split('\n')new_lines=[]forlineinlines:match=SKIP_RE.match(line)ifmatchisNone:new_lines.append(line)continuecode,space,expr=match.groups()ifeval(expr,func.__globals__):code=code+space+"# doctest: +SKIP"new_lines.append(code)func.__doc__="\n".join(new_lines)returnfunc
#### In some cases (e.g., on Travis), we want to use a virtual frame-buffer for# testing. The following decorator runs the tests under xvfb (mediated by# xvfbwrapper) conditioned on an environment variable (that we set in# .travis.yml for these cases):use_xvfb=os.environ.get('TEST_WITH_XVFB',False)
[docs]defxvfb_it(my_test):"""Run a test with xvfbwrapper."""# When we use verbose testing we want the name:fname=my_test.__name__deftest_with_xvfb(*args,**kwargs):ifuse_xvfb:fromxvfbwrapperimportXvfbdisplay=Xvfb(width=1920,height=1080)display.start()my_test(*args,**kwargs)ifuse_xvfb:display.stop()# Plant it back in and return the new function:test_with_xvfb.__name__=fnamereturntest_with_xvfb