本文整理汇总了Python中pytest.fixture函数的典型用法代码示例。如果您正苦于以下问题:Python fixture函数的具体用法?Python fixture怎么用?Python fixture使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fixture函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
def __new__(cls, clsname, bases, dct):
klass = type.__new__(cls, clsname, bases, dct)
for app_name in set(settings.INSTALLED_APPS):
name = app_name.split('.')[-1]
setattr(klass, name, pytest.fixture(scope='session')(django_app(app_name)))
for model in apps.get_models():
name = model._meta.object_name
setattr(klass, name, pytest.fixture(scope='session')(django_model(model)))
return klass
示例2: decorator
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
示例3: decorate
def decorate(fn):
name = fn.__name__
self._factories[name] = fn
# conn, teardown = self.connection_for(name)
def wrapper(*args, **kw):
# This check might be unnecessary
assert self._current_connection == name
return fn(*args, **kw)
wrapper.__wrapped__ = fn
pytest.fixture(scope="session")(wrapper)
return wrapper
示例4: given
def given(name, fixture=None, converters=None, scope='function', target_fixture=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>}.
:scope: Optional fixture scope
:param target_fixture: Target fixture name to replace by steps definition 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()
def step_func(request):
return request.getfuncargvalue(fixture)
step_func.step_type = GIVEN
step_func.converters = converters
step_func.__name__ = name
step_func.fixture = fixture
func = pytest.fixture(scope=scope)(lambda: step_func)
func.__doc__ = 'Alias for the "{0}" fixture.'.format(fixture)
_, name = parse_line(name)
contribute_to_module(module, get_step_fixture_name(name, GIVEN), func)
return _not_a_fixture_decorator
return _step_decorator(GIVEN, name, converters=converters, scope=scope, target_fixture=target_fixture)
示例5: simple_fixture
def simple_fixture(name, params, fmt=None):
"""Helper to create a pytest fixture using only name and params.
Parameters
----------
name : str
Name of the parameters used for the ``ids`` argument
to `pytest.fixture`.
params : sequence
Values to be taken as parameters in the fixture. They are
used as ``params`` argument to `pytest.fixture`.
fmt : str, optional
Use this format string for the generation of the ``ids``.
For each value, the id string is generated as::
fmt.format(name=name, value=value)
hence the format string must use ``{name}`` and ``{value}``.
Default: ``" {name} = '{value}' "`` for string parameters,
otherwise ``" {name} = {value} "``
"""
if fmt is None:
try:
params[0] + ''
except TypeError:
# Not a string type
fmt = " {name} = {value} "
else:
# String type
fmt = " {name} = '{value}' "
ids = [fmt.format(name=name, value=value) for value in params]
wrapper = pytest.fixture(scope='module', ids=ids, params=params)
return wrapper(lambda request: request.param)
示例6: decorator
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
示例7: given
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)
示例8: decorator
def decorator(func):
step_func = func
if step_type == GIVEN:
if not hasattr(func, "_pytestfixturefunction"):
# Avoid multiple wrapping of a fixture
func = pytest.fixture(func)
step_func = lambda request: request.getfuncargvalue(func.__name__)
step_func.__doc__ = func.__doc__
step_func.fixture = func.__name__
step_func.__name__ = force_encode(step_name)
step_func.step_type = step_type
step_func.converters = converters
@pytest.fixture
def lazy_step_func():
return step_func
# Preserve the docstring
lazy_step_func.__doc__ = func.__doc__
if pattern:
lazy_step_func.pattern = pattern
if converters:
lazy_step_func.converters = converters
contribute_to_module(get_caller_module(), step_name, lazy_step_func)
return func
示例9: _make_fixture
def _make_fixture(cls, name):
factory = getattr(cls, name + '_factory')
def fixture(self):
return factory.create()
fixture.__name__ = name
return pytest.fixture(fixture)
示例10: _get_role_fixture
def _get_role_fixture(role_name):
def fixture(testuser_id, webapp_without_login):
with webapp_without_login.app.app_context():
user = models.User.query.get(testuser_id)
user.roles.append(models.Role.query.filter_by(name=role_name).one())
models.db.session.commit()
fixture.__name__ = "{}_role".format(role_name)
return pytest.fixture(fixture)
示例11: calc_motor_fixture
def calc_motor_fixture(name):
def get_motor(beacon):
m = beacon.get(name)
m.no_offset = False
yield m
m.stop()
m.wait_move()
get_motor.__name__ = name
return pytest.fixture(get_motor)
示例12: make_uri_fixture
def make_uri_fixture(name):
"""
Create a pytest fixture named NAME that resolves
to a URI object '<ex://NAME>'.
"""
# noinspection PyShadowingNames
def func(conn):
return conn.createURI('ex://' + name)
func.__name__ = name
return pytest.fixture(func, name=name)
示例13: instance_fixture
def instance_fixture(func):
"""
Mark function as an instance fixture.
It marks function as a fixture and also applies pytest.mark.instance_fixture
"""
func = pytest.fixture(func)
func = pytest.mark.instance_fixture(func)
func.__is_instance_fixture = True
return func
示例14: make_fixture
def make_fixture(arg):
args_attr = 'base_args__' + arg
kwargs_attr = 'base_kwargs__' + arg
def fixture(self, cls):
args = getattr(self, args_attr)
kwargs = getattr(self, kwargs_attr)
return cls(*args, **kwargs)
fixture.__name__ = arg
return pytest.fixture(fixture)
示例15: fixture
def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
"""
When running under pytest, this is the same as the pytest.fixture decorator.
See https://docs.pytest.org/en/latest/reference.html#pytest-fixture
"""
if _use_native_pytest:
# XXX sorting of fixtures based on scope does not work, see
# https://github.com/pytest-dev/pytest/issues/4143#issuecomment-431794076
# When ran under pytest, use native functionality.
return pytest.fixture(scope, params, autouse, ids, name)
init_fallback_fixtures_once()
return _fallback.fixture(scope, params, autouse, ids, name)