Debian Codegraph @

pypy/tool/pytest/fake_pytest/app_pytest.py

raw symbols SCIP
1 import pytest
2
3
4 def importorskip(name):
5 try:
6 return __import__(name)
7 except ImportError:
8 pytest.skip('Module %s not available' % name)
9
10
11 class MarkGenerator:
12 """ Factory for :class:`MarkDecorator` objects - exposed as
13 a ``pytest.mark`` singleton instance. Example::
14
15 import pytest
16 @pytest.mark.slowtest
17 def test_function():
18 pass
19
20 will set a 'slowtest' :class:`MarkInfo` object
21 on the ``test_function`` object. """
22
23 def __getattr__(self, name):
24 if name[0] == "_":
25 raise AttributeError("Marker name must NOT start with underscore")
26 return MarkDecorator(name)
27
28 mark = MarkGenerator()
29
30 def istestfunc(func):
31 return hasattr(func, "__call__") and \
32 getattr(func, "__name__", "<lambda>") != "<lambda>"
33
34 class MarkDecorator:
35 """ A decorator for test functions and test classes. When applied
36 it will create :class:`MarkInfo` objects which may be
37 :ref:`retrieved by hooks as item keywords <excontrolskip>`.
38 MarkDecorator instances are often created like this::
39
40 mark1 = pytest.mark.NAME # simple MarkDecorator
41 mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator
42
43 and can then be applied as decorators to test functions::
44
45 @mark2
46 def test_function():
47 pass
48
49 When a MarkDecorator instance is called it does the following:
50 1. If called with a single class as its only positional argument and no
51 additional keyword arguments, it attaches itself to the class so it
52 gets applied automatically to all test cases found in that class.
53 2. If called with a single function as its only positional argument and
54 no additional keyword arguments, it attaches a MarkInfo object to the
55 function, containing all the arguments already stored internally in
56 the MarkDecorator.
57 3. When called in any other case, it performs a 'fake construction' call,
58 i.e. it returns a new MarkDecorator instance with the original
59 MarkDecorator's content updated with the arguments passed to this
60 call.
61
62 Note: The rules above prevent MarkDecorator objects from storing only a
63 single function or class reference as their positional argument with no
64 additional keyword or positional arguments.
65
66 """
67 def __init__(self, name, args=None, kwargs=None):
68 self.name = name
69 self.args = args or ()
70 self.kwargs = kwargs or {}
71
72 def __repr__(self):
73 d = self.__dict__.copy()
74 name = d.pop('name')
75 return "<MarkDecorator %r %r>" % (name, d)
76
77 def __call__(self, *args, **kwargs):
78 """ if passed a single callable argument: decorate it with mark info.
79 otherwise add *args/**kwargs in-place to mark information. """
80 if args and not kwargs:
81 func = args[0]
82 if len(args) == 1 and istestfunc(func):
83 holder = getattr(func, self.name, None)
84 if holder is None:
85 holder = MarkInfo(
86 self.name, self.args, self.kwargs
87 )
88 setattr(func, self.name, holder)
89 else:
90 holder.add(self.args, self.kwargs)
91 return func
92 kw = self.kwargs.copy()
93 kw.update(kwargs)
94 args = self.args + args
95 return self.__class__(self.name, args=args, kwargs=kw)
96
97
98 class MarkInfo:
99 """ Marking object created by :class:`MarkDecorator` instances. """
100 def __init__(self, name, args, kwargs):
101 #: name of attribute
102 self.name = name
103 #: positional argument list, empty if none specified
104 self.args = args
105 #: keyword argument dictionary, empty if nothing specified
106 self.kwargs = kwargs.copy()
107 self._arglist = [(args, kwargs.copy())]
108
109 def __repr__(self):
110 return "<MarkInfo %r args=%r kwargs=%r>" % (
111 self.name, self.args, self.kwargs
112 )
113
114 def add(self, args, kwargs):
115 """ add a MarkInfo with the given args and kwargs. """
116 self._arglist.append((args, kwargs))
117 self.args += args
118 self.kwargs.update(kwargs)
119
120 def __iter__(self):
121 """ yield MarkInfo objects each relating to a marking-call. """
122 for args, kwargs in self._arglist:
123 yield MarkInfo(self.name, args, kwargs)