1
""" default hooks and general py.test options. """
1
""" default hooks and general py.test options. """
6
6
def pytest_pyfunc_call(__multicall__, pyfuncitem):
7
7
if not __multicall__.execute():
8
testfunction = pyfuncitem.obj
8
testfunction = pyfuncitem.obj
9
9
if pyfuncitem._isyieldedfunction():
10
10
testfunction(*pyfuncitem._args)
13
13
testfunction(**funcargs)
15
15
def pytest_collect_file(path, parent):
17
17
pb = path.purebasename
18
18
if pb.startswith("test_") or pb.endswith("_test") or \
19
19
path in parent.config._argfspaths:
21
return parent.Module(path, parent=parent)
21
return parent.ihook.pytest_pycollect_makemodule(
22
path=path, parent=parent)
24
def pytest_pycollect_makemodule(path, parent):
25
return parent.Module(path, parent)
23
27
def pytest_funcarg__pytestconfig(request):
24
28
""" the pytest config object with access to command line opts."""
25
29
return request.config
31
def pytest_ignore_collect(path, config):
32
ignore_paths = config.getconftest_pathlist("collect_ignore", path=path)
33
ignore_paths = ignore_paths or []
34
excludeopt = config.getvalue("ignore")
36
ignore_paths.extend([py.path.local(x) for x in excludeopt])
37
return path in ignore_paths
38
# XXX more refined would be:
40
for p in ignore_paths:
41
if path == p or path.relto(p):
27
45
def pytest_collect_directory(path, parent):
28
# XXX reconsider the following comment
29
# not use parent.Directory here as we generally
30
# want dir/conftest.py to be able to
31
# define Directory(dir) already
32
if not parent.recfilter(path): # by default special ".cvs", ...
46
# XXX reconsider the following comment
47
# not use parent.Directory here as we generally
48
# want dir/conftest.py to be able to
49
# define Directory(dir) already
50
if not parent.recfilter(path): # by default special ".cvs", ...
33
51
# check if cmdline specified this dir or a subdir directly
34
52
for arg in parent.config._argfspaths:
35
53
if path == arg or arg.relto(path):
39
Directory = parent.config._getcollectclass('Directory', path)
57
Directory = parent.config._getcollectclass('Directory', path)
40
58
return Directory(path, parent=parent)
42
60
def pytest_report_iteminfo(item):
45
63
def pytest_addoption(parser):
46
64
group = parser.getgroup("general", "running and selection options")
47
group._addoption('-x', '--exitfirst',
48
action="store_true", dest="exitfirst", default=False,
65
group._addoption('-x', '--exitfirst', action="store_true", default=False,
49
67
help="exit instantly on first error or failed test."),
68
group._addoption('--maxfail', metavar="num",
69
action="store", type="int", dest="maxfail", default=0,
70
help="exit after first num failures or errors.")
50
71
group._addoption('-k',
51
72
action="store", dest="keyword", default='',
52
73
help="only run test items matching the given "
58
79
group.addoption('--collectonly',
59
80
action="store_true", dest="collectonly",
60
81
help="only collect tests, don't execute them."),
61
group.addoption("--ignore", action="append", metavar="path",
82
group.addoption("--ignore", action="append", metavar="path",
62
83
help="ignore path during collection (multi-allowed).")
63
group.addoption('--confcutdir', dest="confcutdir", default=None,
84
group.addoption('--confcutdir', dest="confcutdir", default=None,
65
86
help="only load conftest.py's relative to specified dir.")
67
group = parser.getgroup("debugconfig",
88
group = parser.getgroup("debugconfig",
68
89
"test process debugging and configuration")
69
90
group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir",
70
91
help="base temporary directory for this test run.")
72
93
def pytest_configure(config):
96
if config.getvalue("exitfirst"):
97
config.option.maxfail = 1
75
99
def setsession(config):
76
100
val = config.getvalue
77
101
if val("collectonly"):
78
102
from py._test.session import Session
79
103
config.setsessionclass(Session)
81
105
# pycollect related hooks and code, should move to pytest_pycollect.py
83
107
def pytest_pycollect_makeitem(__multicall__, collector, name, obj):
84
108
res = __multicall__.execute()
85
109
if res is not None:
87
111
if collector._istestclasscandidate(name, obj):
88
112
res = collector._deprecated_join(name)
89
113
if res is not None:
91
115
return collector.Class(name, parent=collector)
92
116
elif collector.funcnamefilter(name) and hasattr(obj, '__call__'):
93
117
res = collector._deprecated_join(name)
94
118
if res is not None:
96
120
if is_generator(obj):
97
# XXX deprecation warning
121
# XXX deprecation warning
98
122
return collector.Generator(name, parent=collector)
100
return collector._genfunctions(name, obj)
124
return collector._genfunctions(name, obj)
102
126
def is_generator(func):
104
return py.code.getrawcode(func).co_flags & 32 # generator function
128
return py.code.getrawcode(func).co_flags & 32 # generator function
105
129
except AttributeError: # builtin functions have no bytecode
106
130
# assume them to not be generators