sources for item.py [rev. unknown]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import py
from inspect import isclass, ismodule
from py.__.test.outcome import Skipped, Failed, Passed
from py.__.test.collect import FunctionMixin
_dummy = object()
class SetupState(object):
    """ shared state for setting up/tearing down tests. """
    def __init__(self):
        self.stack = []
    def teardown_all(self): 
        while self.stack: 
            col = self.stack.pop() 
            col.teardown() 
     
    def prepare(self, colitem): 
        """ setup objects along the collector chain to the test-method
            Teardown any unneccessary previously setup objects. 
        """
        needed_collectors = colitem.listchain() 
        while self.stack: 
            if self.stack == needed_collectors[:len(self.stack)]: 
                break 
            col = self.stack.pop() 
            col.teardown()
        for col in needed_collectors[len(self.stack):]: 
            #print "setting up", col
            col.setup() 
            self.stack.append(col) 
class Item(py.test.collect.Collector): 
    def startcapture(self): 
        self._config._startcapture(self, path=self.fspath)
    def finishcapture(self): 
        self._config._finishcapture(self)
class Function(FunctionMixin, Item): 
    """ a Function Item is responsible for setting up  
        and executing a Python callable test object.
    """
    _state = SetupState()
    def __init__(self, name, parent, args=(), obj=_dummy, sort_value = None):
        super(Function, self).__init__(name, parent) 
        self._args = args
        if obj is not _dummy: 
            self._obj = obj 
        self._sort_value = sort_value
        
    def __repr__(self): 
        return "<%s %r>" %(self.__class__.__name__, self.name)
    def _getsortvalue(self):  
        if self._sort_value is None:
            return self._getpathlineno()
        return self._sort_value
    def run(self):
        """ setup and execute the underlying test function. """
        self._state.prepare(self) 
        self.execute(self.obj, *self._args)
    def execute(self, target, *args):
        """ execute the given test function. """
        target(*args)
#
# triggering specific outcomes while executing Items
#
class BaseReason(object):
    def __init__(self, msg="unknown reason", **kwds):
        self.msg = msg
        self.__dict__.update(kwds)
    def __repr__(self):
        return self.msg
class Broken(BaseReason):
    def __repr__(self):
        return "Broken: %s" % (self.msg,)
class _NotImplemented(BaseReason):
    def __repr__(self):
        return "Not implemented: %s" % (self.msg,)
# whatever comes here....
def skip(msg=BaseReason()):
    """ skip with the given Message. """
    __tracebackhide__ = True
    raise Skipped(msg=msg) 
def fail(msg="unknown failure"):
    """ fail with the given Message. """
    __tracebackhide__ = True
    raise Failed(msg=msg)