Debian Codegraph @

_pytest/impl

raw
1 Sorting per-resource
2 -----------------------------
3
4 for any given set of items:
5
6 - collect items per session-scoped parametrized funcarg
7 - re-order until items no parametrizations are mixed
8
9 examples:
10
11 test()
12 test1(s1)
13 test1(s2)
14 test2()
15 test3(s1)
16 test3(s2)
17
18 gets sorted to:
19
20 test()
21 test2()
22 test1(s1)
23 test3(s1)
24 test1(s2)
25 test3(s2)
26
27
28 the new @setup functions
29 --------------------------------------
30
31 Consider a given @setup-marked function::
32
33 @pytest.mark.setup(maxscope=SCOPE)
34 def mysetup(request, arg1, arg2, ...)
35 ...
36 request.addfinalizer(fin)
37 ...
38
39 then FUNCARGSET denotes the set of (arg1, arg2, ...) funcargs and
40 all of its dependent funcargs. The mysetup function will execute
41 for any matching test item once per scope.
42
43 The scope is determined as the minimum scope of all scopes of the args
44 in FUNCARGSET and the given "maxscope".
45
46 If mysetup has been called and no finalizers have been called it is
47 called "active".
48
49 Furthermore the following rules apply:
50
51 - if an arg value in FUNCARGSET is about to be torn down, the
52 mysetup-registered finalizers will execute as well.
53
54 - There will never be two active mysetup invocations.
55
56 Example 1, session scope::
57
58 @pytest.mark.funcarg(scope="session", params=[1,2])
59 def db(request):
60 request.addfinalizer(db_finalize)
61
62 @pytest.mark.setup
63 def mysetup(request, db):
64 request.addfinalizer(mysetup_finalize)
65 ...
66
67 And a given test module:
68
69 def test_something():
70 ...
71 def test_otherthing():
72 pass
73
74 Here is what happens::
75
76 db(request) executes with request.param == 1
77 mysetup(request, db) executes
78 test_something() executes
79 test_otherthing() executes
80 mysetup_finalize() executes
81 db_finalize() executes
82 db(request) executes with request.param == 2
83 mysetup(request, db) executes
84 test_something() executes
85 test_otherthing() executes
86 mysetup_finalize() executes
87 db_finalize() executes
88
89 Example 2, session/function scope::
90
91 @pytest.mark.funcarg(scope="session", params=[1,2])
92 def db(request):
93 request.addfinalizer(db_finalize)
94
95 @pytest.mark.setup(scope="function")
96 def mysetup(request, db):
97 ...
98 request.addfinalizer(mysetup_finalize)
99 ...
100
101 And a given test module:
102
103 def test_something():
104 ...
105 def test_otherthing():
106 pass
107
108 Here is what happens::
109
110 db(request) executes with request.param == 1
111 mysetup(request, db) executes
112 test_something() executes
113 mysetup_finalize() executes
114 mysetup(request, db) executes
115 test_otherthing() executes
116 mysetup_finalize() executes
117 db_finalize() executes
118 db(request) executes with request.param == 2
119 mysetup(request, db) executes
120 test_something() executes
121 mysetup_finalize() executes
122 mysetup(request, db) executes
123 test_otherthing() executes
124 mysetup_finalize() executes
125 db_finalize() executes
126
127
128 Example 3 - funcargs session-mix
129 ----------------------------------------
130
131 Similar with funcargs, an example::
132
133 @pytest.mark.funcarg(scope="session", params=[1,2])
134 def db(request):
135 request.addfinalizer(db_finalize)
136
137 @pytest.mark.funcarg(scope="function")
138 def table(request, db):
139 ...
140 request.addfinalizer(table_finalize)
141 ...
142
143 And a given test module:
144
145 def test_something(table):
146 ...
147 def test_otherthing(table):
148 pass
149 def test_thirdthing():
150 pass
151
152 Here is what happens::
153
154 db(request) executes with param == 1
155 table(request, db)
156 test_something(table)
157 table_finalize()
158 table(request, db)
159 test_otherthing(table)
160 table_finalize()
161 db_finalize
162 db(request) executes with param == 2
163 table(request, db)
164 test_something(table)
165 table_finalize()
166 table(request, db)
167 test_otherthing(table)
168 table_finalize()
169 db_finalize
170 test_thirdthing()
171
172 Data structures
173 --------------------
174
175 pytest internally maintains a dict of active funcargs with cache, param,
176 finalizer, (scopeitem?) information:
177
178 active_funcargs = dict()
179
180 if a parametrized "db" is activated:
181
182 active_funcargs["db"] = FuncargInfo(dbvalue, paramindex,
183 FuncargFinalize(...), scopeitem)
184
185 if a test is torn down and the next test requires a differently
186 parametrized "db":
187
188 for argname in item.callspec.params:
189 if argname in active_funcargs:
190 funcarginfo = active_funcargs[argname]
191 if funcarginfo.param != item.callspec.params[argname]:
192 funcarginfo.callfinalizer()
193 del node2funcarg[funcarginfo.scopeitem]
194 del active_funcargs[argname]
195 nodes_to_be_torn_down = ...
196 for node in nodes_to_be_torn_down:
197 if node in node2funcarg:
198 argname = node2funcarg[node]
199 active_funcargs[argname].callfinalizer()
200 del node2funcarg[node]
201 del active_funcargs[argname]
202
203 if a test is setup requiring a "db" funcarg:
204
205 if "db" in active_funcargs:
206 return active_funcargs["db"][0]
207 funcarginfo = setup_funcarg()
208 active_funcargs["db"] = funcarginfo
209 node2funcarg[funcarginfo.scopeitem] = "db"
210
211 Implementation plan for resources
212 ------------------------------------------
213
214 1. Revert FuncargRequest to the old form, unmerge item/request
215 (done)
216 2. make funcarg factories be discovered at collection time
217 3. Introduce funcarg marker
218 4. Introduce funcarg scope parameter
219 5. Introduce funcarg parametrize parameter
220 6. make setup functions be discovered at collection time
221 7. (Introduce a pytest_fixture_protocol/setup_funcargs hook)
222
223 methods and data structures
224 --------------------------------
225
226 A FuncarcManager holds all information about funcarg definitions
227 including parametrization and scope definitions. It implements
228 a pytest_generate_tests hook which performs parametrization as appropriate.
229
230 as a simple example, let's consider a tree where a test function requires
231 a "abc" funcarg and its factory defines it as parametrized and scoped
232 for Modules. When collections hits the function item, it creates
233 the metafunc object, and calls funcargdb.pytest_generate_tests(metafunc)
234 which looks up available funcarg factories and their scope and parametrization.
235 This information is equivalent to what can be provided today directly
236 at the function site and it should thus be relatively straight forward
237 to implement the additional way of defining parametrization/scoping.
238
239 conftest loading:
240 each funcarg-factory will populate the session.funcargmanager
241
242 When a test item is collected, it grows a dictionary
243 (funcargname2factorycalllist). A factory lookup is performed
244 for each required funcarg. The resulting factory call is stored
245 with the item. If a function is parametrized multiple items are
246 created with respective factory calls. Else if a factory is parametrized
247 multiple items and calls to the factory function are created as well.
248
249 At setup time, an item populates a funcargs mapping, mapping names
250 to values. If a value is funcarg factories are queried for a given item
251 test functions and setup functions are put in a class
252 which looks up required funcarg factories.
253
254