Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the restriction on usability for tests only #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pytest_data/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down