Skip to content

Commit 3f6146e

Browse files
committed
Adds pytest.fixture_with_options
1 parent 8af7163 commit 3f6146e

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

case/pytest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,49 @@
33
import pytest
44
import sys
55

6+
from functools import partial, wraps
67
from six import iteritems as items
78

89
from . import mock
910

1011
sentinel = object()
1112

1213

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+
1349
class _patching(object):
1450

1551
def __init__(self, monkeypatch, request):

0 commit comments

Comments
 (0)