Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def decorator_test(func):
mod_name = func.__module__
force_path = kwargs.get("_force_path")
if force_path:
path = force_path
else:
path = Path(inspect.getfile(func)).absolute()
if hasattr(func, "ward_meta"):
func.ward_meta.description = description
func.ward_meta.path = path
else:
func.ward_meta = WardMeta(
description=description,
path=path,
)
collect_into = kwargs.get("_collect_into")
if collect_into is not None:
collect_into[mod_name].append(func)
else:
anonymous_tests[mod_name].append(func)
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def skip(func_or_reason=None, *, reason: str = None):
if func_or_reason is None:
return functools.partial(skip, reason=reason)
if isinstance(func_or_reason, str):
return functools.partial(skip, reason=func_or_reason)
func = func_or_reason
marker = SkipMarker(reason=reason)
if hasattr(func, "ward_meta"):
func.ward_meta.marker = marker
else:
func.ward_meta = WardMeta(marker=marker)
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def xfail(func_or_reason=None, *, reason: str = None):
if func_or_reason is None:
return functools.partial(xfail, reason=reason)
if isinstance(func_or_reason, str):
return functools.partial(xfail, reason=func_or_reason)
func = func_or_reason
marker = XfailMarker(reason=reason)
if hasattr(func, "ward_meta"):
func.ward_meta.marker = marker
else:
func.ward_meta = WardMeta(marker=marker)
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def fixture(func=None, *, scope: Optional[Union[Scope, str]] = Scope.Test):
if not isinstance(scope, Scope):
scope = Scope.from_str(scope)
if func is None:
return partial(fixture, scope=scope)
# By setting is_fixture = True, the framework will know
# that if this fixture is provided as a default arg, it
# is responsible for resolving the value.
path = Path(inspect.getfile(func)).absolute()
if hasattr(func, "ward_meta"):
func.ward_meta.is_fixture = True
func.ward_meta.path = path
else:
func.ward_meta = WardMeta(
is_fixture=True,
scope=scope,
path=path,
)
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
if anon_tests:
for test_fn in anon_tests:
meta: WardMeta = getattr(test_fn, "ward_meta")
yield Test(
fn=test_fn,
module_name=mod_name,
marker=meta.marker,
description=meta.description or "",
)
# Collect named tests from the module
for item in dir(mod):
if item.startswith("test_") and not item == "_":
test_name = item
test_fn = getattr(mod, test_name)
marker: Marker = getattr(test_fn, "ward_meta", WardMeta()).marker
if test_fn:
yield Test(fn=test_fn, module_name=mod_name, marker=marker)
def decorator_using(func):
signature = inspect.signature(func)
bound_args = signature.bind_partial(*using_args, **using_kwargs)
if hasattr(func, "ward_meta"):
func.ward_meta.bound_args = bound_args
else:
func.ward_meta = WardMeta(bound_args=bound_args)
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper