From 8d112befa07a4d2b18fb47790778d7b3c0ecd774 Mon Sep 17 00:00:00 2001 From: Jan Hnatek Date: Wed, 1 Feb 2023 10:38:43 +0100 Subject: [PATCH] Remove the restriction on usability for tests only --- pytest_data/functions.py | 21 ++++++++++++++++----- setup.py | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pytest_data/functions.py b/pytest_data/functions.py index d2e8238..681ae71 100644 --- a/pytest_data/functions.py +++ b/pytest_data/functions.py @@ -80,16 +80,24 @@ def user(request): user_data = get_data(request, 'user_data', {'name': 'Jerry'}) return User(user_data) """ - TARGETS = (request.module, request.cls, request.function) + targets = [request.module] + try: + targets.append(request.cls) + except AttributeError: + pass + try: + targets.append(request.function) + except AttributeError: + pass if isinstance(default_data, dict): getter = partial(_getter, attribute_name=attribute_name, default={}) - dicts = [default_data] + list(map(getter, TARGETS)) + [_getter(request, 'param', {})] + dicts = [default_data] + list(map(getter, targets)) + [_getter(request, 'param', {})] data = _merge(*dicts) elif isinstance(default_data, list): getter = partial(_getter, attribute_name=attribute_name, default=[]) - dicts = [default_data] + list(map(getter, TARGETS)) + [_getter(request, 'param', [])] + dicts = [default_data] + list(map(getter, targets)) + [_getter(request, 'param', [])] max_len = max(map(len, dicts)) data = [_merge(*datas) for datas in islice(zip_longest(*map(cycle, dicts)), max_len)] @@ -146,13 +154,16 @@ def test_foo(): .. versionchanged:: 0.3 ``use_data`` can be used only for tests, not fixtures. So since version 0.3 there is check that it's used only with tests (function starts with `test`). + .. versionchanged:: 0.5 + The restriction on usability on tests only has been removed. This is useful + for creating module-scoped fixtures. """ - def wrapper(func): - assert func.__name__.startswith('test'), 'use_data can be used only for tests' + def wrapper(func): for key, value in data.items(): setattr(func, key, value) return func + return wrapper diff --git a/setup.py b/setup.py index 9ffa876..b722326 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='pytest-data', - version='0.4', + version='0.5', packages=['pytest_data'], url='https://github.com/horejsek/python-pytest-data',