File: test_ordering.py

package info (click to toggle)
pytest-order 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 600 kB
  • sloc: python: 3,483; makefile: 159; sh: 13
file content (370 lines) | stat: -rw-r--r-- 7,633 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import pytest


def test_no_marks(item_names_for):
    tests_content = """
        def test_1(): pass

        def test_2(): pass
        """
    assert item_names_for(tests_content) == ["test_1", "test_2"]


def test_first_mark(item_names_for):
    tests_content = """
        import pytest

        def test_1(): pass

        @pytest.mark.order("first")
        def test_2(): pass
        """
    assert item_names_for(tests_content) == ["test_2", "test_1"]


def test_last_mark(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order("last")
        def test_1(): pass

        def test_2(): pass
        """
    assert item_names_for(tests_content) == ["test_2", "test_1"]


def test_first_last_marks(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order("last")
        def test_1(): pass

        @pytest.mark.order("first")
        def test_2(): pass

        def test_3(): pass
        """
    assert item_names_for(tests_content) == ["test_2", "test_3", "test_1"]


def test_order_marks(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order(-1)
        def test_1(): pass

        @pytest.mark.order(-2)
        def test_2(): pass

        @pytest.mark.order(1)
        def test_3(): pass
        """
    assert item_names_for(tests_content) == ["test_3", "test_2", "test_1"]


def test_order_marks_by_index(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order(index=-1)
        def test_1(): pass

        @pytest.mark.order(index=-2)
        def test_2(): pass

        @pytest.mark.order(index=1)
        def test_3(): pass
        """
    assert item_names_for(tests_content) == ["test_3", "test_2", "test_1"]


def test_non_contiguous_positive(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order(10)
        def test_1(): pass

        @pytest.mark.order(20)
        def test_2(): pass

        @pytest.mark.order(5)
        def test_3(): pass
        """
    assert item_names_for(tests_content) == ["test_3", "test_1", "test_2"]


def test_non_contiguous_negative(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order(-10)
        def test_1(): pass

        @pytest.mark.order(-20)
        def test_2(): pass

        @pytest.mark.order(-5)
        def test_3(): pass
        """
    assert item_names_for(tests_content) == ["test_2", "test_1", "test_3"]


def test_non_contiguous_inc_zero(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order(10)
        def test_1(): pass

        @pytest.mark.order(20)
        def test_2(): pass

        @pytest.mark.order(5)
        def test_3(): pass

        @pytest.mark.order(-10)
        def test_4(): pass

        @pytest.mark.order(-20)
        def test_5(): pass

        @pytest.mark.order(-5)
        def test_6(): pass

        @pytest.mark.order(0)
        def test_7(): pass
        """
    assert item_names_for(tests_content) == [
        "test_7",
        "test_3",
        "test_1",
        "test_2",
        "test_5",
        "test_4",
        "test_6",
    ]


def test_non_contiguous_inc_none(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order(5)
        def test_1(): pass

        @pytest.mark.order(0)
        def test_2(): pass

        @pytest.mark.order(1)
        def test_3(): pass

        @pytest.mark.order(-1)
        def test_4(): pass

        @pytest.mark.order(-5)
        def test_5(): pass

        def test_6(): pass
        """
    assert item_names_for(tests_content) == [
        "test_2",
        "test_3",
        "test_1",
        "test_6",
        "test_5",
        "test_4",
    ]


def test_first_mark_class(item_names_for):
    tests_content = """
        import pytest

        def test_1(): pass


        @pytest.mark.order("first")
        class TestSuite:

            def test_3(self): pass

            def test_2(self): pass

        """
    assert item_names_for(tests_content) == [
        "TestSuite::test_3",
        "TestSuite::test_2",
        "test_1",
    ]


def test_last_mark_class(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order("last")
        class TestSuite:

            def test_1(self): pass

            def test_2(self): pass


        def test_3(): pass
        """
    assert item_names_for(tests_content) == [
        "test_3",
        "TestSuite::test_1",
        "TestSuite::test_2",
    ]


def test_first_last_mark_class(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order("last")
        class TestLast:

            def test_1(self): pass

            def test_2(self): pass


        def test_0(): pass


        @pytest.mark.order("first")
        class TestFirst:

            def test_1(self): pass

            def test_2(self): pass

        """
    assert item_names_for(tests_content) == [
        "TestFirst::test_1",
        "TestFirst::test_2",
        "test_0",
        "TestLast::test_1",
        "TestLast::test_2",
    ]


def test_order_mark_class(item_names_for):
    tests_content = """
        import pytest

        @pytest.mark.order(-1)
        class TestLast:
            def test_1(self): pass
            def test_2(self): pass

        @pytest.mark.order(0)
        def test_0(): pass

        @pytest.mark.order(-2)
        class TestFirst:
            def test_1(self): pass
            def test_2(self): pass
        """
    assert item_names_for(tests_content) == [
        "test_0",
        "TestFirst::test_1",
        "TestFirst::test_2",
        "TestLast::test_1",
        "TestLast::test_2",
    ]


def test_run_ordinals(item_names_for):
    test_content = """
        import pytest

        @pytest.mark.order("second_to_last")
        def test_three():
            pass

        @pytest.mark.order("last")
        def test_four():
            pass

        @pytest.mark.order("second")
        def test_two():
            pass

        @pytest.mark.order("first")
        def test_one():
            pass
        """
    assert item_names_for(test_content) == [
        "test_one",
        "test_two",
        "test_three",
        "test_four",
    ]


def test_sparse_numbers(item_names_for):
    test_content = """
        import pytest

        @pytest.mark.order(4)
        def test_two():
            assert True

        def test_three():
            assert True

        @pytest.mark.order(2)
        def test_one():
            assert True
        """
    assert item_names_for(test_content) == ["test_one", "test_two", "test_three"]


def test_quickstart(item_names_for):
    test_content = """
        import pytest

        def test_foo():
            pass

        def test_bar():
            pass
        """
    assert item_names_for(test_content) == ["test_foo", "test_bar"]


def test_quickstart2(item_names_for):
    test_content = """
        import pytest

        @pytest.mark.order(2)
        def test_foo():
            pass

        @pytest.mark.order(1)
        def test_bar():
            pass
        """
    assert item_names_for(test_content) == ["test_bar", "test_foo"]


def test_unsupported_order(item_names_for):
    test_content = """
        import pytest

        @pytest.mark.order("unknown")
        def test_1():
            pass

        def test_2():
            pass
        """
    with pytest.warns(UserWarning, match="Unknown order attribute:'unknown'"):
        assert item_names_for(test_content) == ["test_1", "test_2"]