~crunch.io/ubuntu/precise/codespeak-lib/unstable

« back to all changes in this revision

Viewing changes to py/_plugin/pytest_default.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-08-01 16:24:01 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100801162401-g37v49d1p148alpm
Tags: 1.3.3-1
* New upstream release.
* Bump Standards-Version to 3.9.1.
* Fix typo in py.test manpage.
* Prefer Breaks: over Conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
""" default hooks and general py.test options. """ 
 
1
""" default hooks and general py.test options. """
2
2
 
3
3
import sys
4
4
import py
5
5
 
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)
11
11
        else:
13
13
            testfunction(**funcargs)
14
14
 
15
15
def pytest_collect_file(path, parent):
16
 
    ext = path.ext 
 
16
    ext = path.ext
17
17
    pb = path.purebasename
18
18
    if pb.startswith("test_") or pb.endswith("_test") or \
19
19
       path in parent.config._argfspaths:
20
20
        if ext == ".py":
21
 
            return parent.Module(path, parent=parent) 
 
21
            return parent.ihook.pytest_pycollect_makemodule(
 
22
                path=path, parent=parent)
 
23
 
 
24
def pytest_pycollect_makemodule(path, parent):
 
25
    return parent.Module(path, parent)
22
26
 
23
27
def pytest_funcarg__pytestconfig(request):
24
28
    """ the pytest config object with access to command line opts."""
25
29
    return request.config
26
30
 
 
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")
 
35
    if excludeopt:
 
36
        ignore_paths.extend([py.path.local(x) for x in excludeopt])
 
37
    return path in ignore_paths
 
38
    # XXX more refined would be:
 
39
    if ignore_paths:
 
40
        for p in ignore_paths:
 
41
            if path == p or path.relto(p):
 
42
                return True
 
43
 
 
44
 
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):
36
54
                break
37
55
        else:
38
 
            return 
39
 
    Directory = parent.config._getcollectclass('Directory', path) 
 
56
            return
 
57
    Directory = parent.config._getcollectclass('Directory', path)
40
58
    return Directory(path, parent=parent)
41
59
 
42
60
def pytest_report_iteminfo(item):
44
62
 
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,
 
66
               dest="exitfirst",
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,
64
85
        metavar="dir",
65
86
        help="only load conftest.py's relative to specified dir.")
66
87
 
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.")
71
92
 
72
93
def pytest_configure(config):
73
94
    setsession(config)
 
95
    # compat
 
96
    if config.getvalue("exitfirst"):
 
97
        config.option.maxfail = 1
74
98
 
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)
80
 
      
 
104
 
81
105
# pycollect related hooks and code, should move to pytest_pycollect.py
82
 
 
 
106
 
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:
90
 
            return res 
 
114
            return res
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:
95
 
            return res 
 
119
            return res
96
120
        if is_generator(obj):
97
 
            # XXX deprecation warning 
 
121
            # XXX deprecation warning
98
122
            return collector.Generator(name, parent=collector)
99
123
        else:
100
 
            return collector._genfunctions(name, obj) 
 
124
            return collector._genfunctions(name, obj)
101
125
 
102
126
def is_generator(func):
103
127
    try:
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
107
 
        return False 
 
131
        return False