Skip to content

Commit 0af4d2b

Browse files
committed
Merge pull request #29 from nodev-io/marker-interface
Add marker interface to enable search. Closes #28.
2 parents 7d4dff2 + ab219be commit 0af4d2b

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

pytest_nodev/__init__.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,3 @@
22
#
33
# Copyright (c) 2015-2016 Alessandro Amici
44
#
5-
6-
# python 2 support via python-future
7-
from __future__ import absolute_import, unicode_literals
8-
9-
import inspect
10-
11-
12-
def search(target_name):
13-
def wish_decorator(test_func):
14-
def wish_wrapper(wish, monkeypatch, *args, **kwargs):
15-
if '.' in target_name:
16-
monkeypatch.setattr(target_name, wish, raising=False)
17-
else:
18-
monkeypatch.setattr(inspect.getmodule(test_func), target_name, wish, raising=False)
19-
return test_func(*args, **kwargs)
20-
return wish_wrapper
21-
return wish_decorator

pytest_nodev/plugin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from builtins import dict, list, zip
2727

2828
import collections
29+
import inspect
2930
import logging
3031
import os
3132
import sys
@@ -113,6 +114,22 @@ def make_wish_index(config):
113114
return config._wish_index_items
114115

115116

117+
def pytest_pycollect_makeitem(collector, name, obj):
118+
search_marker = getattr(obj, 'search', None)
119+
if search_marker and getattr(search_marker, 'args', []):
120+
target_name = search_marker.args[0]
121+
122+
def wrapper(wish, monkeypatch, *args, **kwargs):
123+
if '.' in target_name:
124+
monkeypatch.setattr(target_name, wish, raising=False)
125+
else:
126+
monkeypatch.setattr(inspect.getmodule(obj), target_name, wish, raising=False)
127+
return obj(*args, **kwargs)
128+
129+
wrapper.__dict__ = obj.__dict__
130+
return list(collector._genfunctions(name, wrapper))
131+
132+
116133
def pytest_generate_tests(metafunc):
117134
if 'wish' not in metafunc.fixturenames:
118135
return

specs/test_object_from_name.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11

22
import os
33

4-
import pytest_nodev
4+
import pytest
55

66

7-
@pytest_nodev.search('object_from_name')
7+
@pytest.mark.search('object_from_name')
88
def test_object_from_name_simple():
99
assert object_from_name('os:O_CREAT') is os.O_CREAT
1010
assert object_from_name('os.path:join') is os.path.join
1111
assert object_from_name('builtins:True') is True
1212
assert object_from_name('builtins:open') is open
1313

1414

15-
@pytest_nodev.search('object_from_name')
15+
@pytest.mark.search('object_from_name')
1616
def test_object_from_name_pep3155():
1717
# instance methods compare by equality, see http://stackoverflow.com/questions/15977808
1818
assert object_from_name('os:O_CREAT.bit_length') == os.O_CREAT.bit_length

tests/test_plugin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ def test_factorial(wish):
2222
assert factorial(21) == 51090942171709440000
2323
'''
2424
TEST_POW_PY = '''
25-
def test_pow(wish):
26-
assert wish(2, 9, 47) == 42
25+
import pytest
26+
@pytest.mark.search('pow')
27+
def test_pow():
28+
assert pow(2, 9, 47) == 42
2729
'''
2830

2931

0 commit comments

Comments
 (0)