Example #1
0
    def decorator_test(func):
        unwrapped = inspect.unwrap(func)
        module_name: str = unwrapped.__module__
        is_home_module: bool = "." not in module_name
        if is_test_module_name(module_name) and is_home_module:
            force_path: Path = kwargs.get("_force_path")
            if force_path:
                path = force_path.absolute()
            else:
                path = get_absolute_path(unwrapped)

            if hasattr(unwrapped, "ward_meta"):
                unwrapped.ward_meta.description = description
                unwrapped.ward_meta.tags = tags
                unwrapped.ward_meta.path = path
            else:
                unwrapped.ward_meta = WardMeta(
                    description=description,
                    tags=tags,
                    path=path,
                )

            collect_into = kwargs.get("_collect_into", anonymous_tests)
            collect_into[path].append(unwrapped)

            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                return func(*args, **kwargs)

            return wrapper

        return func
Example #2
0
    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(inspect.unwrap(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
Example #3
0
def _(func=example_test):
    out_func = testable_test(func)

    assert out_func.ward_meta == WardMeta(
        marker=None,
        description="testable test description",
        is_fixture=False,
        scope=Scope.Test,
        bound_args=None,
        path=FORCE_TEST_PATH,
    )
Example #4
0
    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
Example #5
0
    def decorator_test(func):
        if func.__name__ == "_":
            mod_name = func.__module__
            if hasattr(func, "ward_meta"):
                func.ward_meta.description = description
            else:
                func.ward_meta = WardMeta(description=description)
            anonymous_tests[mod_name].append(func)

        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)

        return wrapper
Example #6
0
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
Example #7
0
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.
    if hasattr(func, "ward_meta"):
        func.ward_meta.is_fixture = True
    else:
        func.ward_meta = WardMeta(is_fixture=True, scope=scope)

    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    return wrapper
Example #8
0
def get_tests_in_modules(modules: Iterable) -> Generator[Test, None, None]:
    for mod in modules:
        mod_name = mod.__name__
        # Collect anonymous tests from the module
        anon_tests: List[Callable] = anonymous_tests[mod_name]
        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)
Example #9
0
def skip(
    func_or_reason: Union[str, Callable] = None,
    *,
    reason: str = None,
    when: Union[bool, Callable] = True,
):
    if func_or_reason is None:
        return functools.partial(skip, reason=reason, when=when)

    if isinstance(func_or_reason, str):
        return functools.partial(skip, reason=func_or_reason, when=when)

    func = func_or_reason
    marker = SkipMarker(reason=reason, when=when)
    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