|
3 | 3 | import pytest |
4 | 4 | import sys |
5 | 5 |
|
| 6 | +from functools import partial, wraps |
6 | 7 | from six import iteritems as items |
7 | 8 |
|
8 | 9 | from . import mock |
9 | 10 |
|
10 | 11 | sentinel = object() |
11 | 12 |
|
12 | 13 |
|
| 14 | +class fixture_with_options(object): |
| 15 | + """Pytest fixture with options specified in separate decrorator. |
| 16 | +
|
| 17 | + The decorated fixture MUST take the request fixture as first argument, |
| 18 | + but is free to use other fixtures. |
| 19 | +
|
| 20 | + Example: |
| 21 | + @fixture_with_options() |
| 22 | + def sftp(request, |
| 23 | + username='test_username', |
| 24 | + password='test_password'): |
| 25 | + return {'username': username, 'password': password} |
| 26 | +
|
| 27 | + @sftp.options(username='foo', password='bar') |
| 28 | + def test_foo(sftp): |
| 29 | + assert sftp['username'] == 'foo' |
| 30 | + assert sftp['password'] == 'bar' |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, marker_name=None): |
| 34 | + self.marker_name = marker_name |
| 35 | + |
| 36 | + def __call__(self, fun): |
| 37 | + marker_name = self.marker_name or fun.__name__ |
| 38 | + |
| 39 | + @pytest.fixture() |
| 40 | + @wraps(fun) |
| 41 | + def _inner(request, *args, **kwargs): |
| 42 | + marker = request.node.get_marker(marker_name) |
| 43 | + return fun(request, *args, **dict(marker.kwargs, **kwargs)) |
| 44 | + _inner.options = partial(getattr(pytest.mark, marker_name)) |
| 45 | + _inner.__wrapped__ = fun |
| 46 | + return _inner |
| 47 | + |
| 48 | + |
13 | 49 | class _patching(object): |
14 | 50 |
|
15 | 51 | def __init__(self, monkeypatch, request): |
|
0 commit comments