def _step_decorator(step_type, step_name): """Step decorator for the type and the name. :param step_type: Step type (GIVEN, WHEN or THEN). :param step_name: Step name as in the feature file. :return: Decorator function for the step. :note: If the step type is GIVEN it will automatically apply the pytest fixture decorator to the step function. """ step_name = remove_prefix(step_name) def decorator(func): step_func = func if step_type == GIVEN: if not hasattr(func, '_pytestfixturefunction'): # avoid overfixturing of a fixture func = pytest.fixture(func) step_func = lambda request: request.getfuncargvalue(func.func_name) step_func.__name__ = step_name setattr(get_caller_module(), step_name, pytest.fixture(lambda: step_func)) return func return decorator
def given(name, fixture=None, converters=None): """Given step decorator. :param name: Given step name. :param fixture: Optional name of the fixture to reuse. :param converters: Optional `dict` of the argument or parameter converters in form {<param_name>: <converter function>}. :raises: StepError in case of wrong configuration. :note: Can't be used as a decorator when the fixture is specified. """ if fixture is not None: module = get_caller_module() step_func = lambda request: request.getfuncargvalue(fixture) step_func.step_type = GIVEN step_func.converters = converters step_func.__name__ = name step_func.fixture = fixture func = pytest.fixture(lambda: step_func) func.__doc__ = 'Alias for the "{0}" fixture.'.format(fixture) contribute_to_module(module, remove_prefix(name), func) return _not_a_fixture_decorator return _step_decorator(GIVEN, name, converters=converters)
def _decorate_step(func, step_type, step_name): """Decorate the step function. :param func: Step function. :param step_type: Step type (GIVEN, WHEN or THEN). :param step_name: Step name as in the feature file. :raises: StepError when step types mismatch. """ old_type = getattr(func, '__step_type__', None) if old_type and old_type != step_type: raise StepError('Step type mismatched') func.__step_type__ = step_type if not hasattr(func, '__step_names__'): func.__step_names__ = [] func.__step_names__.append(remove_prefix(step_name))
def given(name, fixture=None): """Given step decorator. :param name: Given step name. :param fixture: Optional name of the fixture to reuse. :raises: StepError in case of wrong configuration. :note: Can't be used as a decorator when the fixture is specified. """ name = remove_prefix(name) if fixture is not None: module = get_caller_module() func = getattr(module, fixture, lambda request: request.getfuncargvalue(fixture)) setattr(module, name, pytest.fixture(lambda: func)) return _not_a_fixture_decorator return _step_decorator(GIVEN, name)
def given(name, fixture=None): """Given step decorator. :param name: Given step name. :param fixture: Optional name of the fixture to reuse. :raises: StepError in case of wrong configuration. :note: Can't be used as a decorator when the fixture is specified. """ name = remove_prefix(name) if fixture is not None: module = get_caller_module() func = lambda: lambda request: request.getfuncargvalue(fixture) contribute_to_module(module, name, pytest.fixture(func)) return _not_a_fixture_decorator return _step_decorator(GIVEN, name)
def _step_decorator(step_type, step_name): """Step decorator for the type and the name. :param step_type: Step type (GIVEN, WHEN or THEN). :param step_name: Step name as in the feature file. :return: Decorator function for the step. :note: If the step type is GIVEN it will automatically apply the pytest fixture decorator to the step function. """ step_name = remove_prefix(step_name) def decorator(func): step_func = func if step_type == GIVEN: if not hasattr(func, '_pytestfixturefunction'): # Avoid multiple wrapping a fixture func = pytest.fixture(func) step_func = lambda request: request.getfuncargvalue(func.__name__) step_func.__doc__ = func.__doc__ step_func.__name__ = step_name @pytest.fixture def lazy_step_func(): return step_func # Preserve a docstring lazy_step_func.__doc__ = func.__doc__ contribute_to_module( get_caller_module(), step_name, lazy_step_func, ) return func return decorator