-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_lazyfixture.py
938 lines (780 loc) · 26.7 KB
/
test_lazyfixture.py
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# -*- coding: utf-8 -*-
import pytest
from pytest_lazyfixture import sorted_by_dependency, lazy_fixture, _sorted_argnames
try:
import numpy
except ImportError:
numpy = None
def test_fixture_in_parametrize_with_params(testdir):
items = testdir.getitems("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[1,2])
def one(request):
return request.param
@pytest.mark.parametrize('arg1,arg2', [
('val1', lazy_fixture('one')),
('val1', 'val2')
])
def test_func(arg1, arg2):
pass
""")
assert len(items) == 3
assert items[0].callspec.params['one'] == 1
assert items[1].callspec.params['one'] == 2
def test_several_fixtures_in_parametrize_with_params(testdir):
items = testdir.getitems("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[1,2])
def one(request):
return request.param
@pytest.fixture(params=[3,4])
def two(request):
return request.param
@pytest.mark.parametrize('arg1,arg2,arg3', [
('val1', lazy_fixture('one'), lazy_fixture('two')),
])
def test_func(arg1, arg2, arg3):
pass
""")
assert len(items) == 4
expected_results = [
{'one': 1, 'two': 3},
{'one': 1, 'two': 4},
{'one': 2, 'two': 3},
{'one': 2, 'two': 4}
]
def is_subset(subset, superset):
return all(superset[k] == subset[k] for k in subset)
for item in items:
assert any(is_subset(result, item.callspec.params) for result in expected_results)
def test_fixtures_in_parametrize_with_indirect(testdir):
items = testdir.getitems("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one():
pass
@pytest.fixture
def two():
pass
@pytest.mark.parametrize('arg1,one', [
('val1', lazy_fixture('two')),
], indirect=['one'])
def test_func(arg1, one):
pass
""")
assert len(items) == 1
assert items[0].callspec.params['one'].name == 'two'
def test_fixtures_with_params_in_parametrize_with_indirect(testdir):
items = testdir.getitems("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one():
pass
@pytest.fixture(params=[1,2])
def two(request):
return request.param
@pytest.mark.parametrize('arg1,one', [
('val1', lazy_fixture('two')),
], indirect=['one'])
def test_func(arg1, one):
pass
""")
assert len(items) == 2
assert items[0].callspec.params['two'] == 1
assert items[1].callspec.params['two'] == 2
def test_lazy_fixture_is_value_in_parametrize(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one():
return 1
@pytest.fixture
def two():
return 2
@pytest.mark.parametrize('arg1,arg2', [
lazy_fixture(('one', 'two'))
])
def test_func(arg1, arg2):
assert arg1 == 1
assert arg2 == 2
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=1)
def test_lazy_fixture_as_funcarg_in_parametrize_with_indirect(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one():
return 1
@pytest.fixture
def two():
return 2
@pytest.fixture
def three(request):
return request.param
@pytest.mark.parametrize('arg1,arg2,three', [
(lazy_fixture('one'), lazy_fixture('two'), '3')
], indirect=['three'])
def test_func(arg1, arg2, three):
assert arg1 == 1
assert arg2 == 2
assert three == '3'
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=1)
def test_lazy_fixture_is_value_in_parametrize_with_indirect(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one(request):
return request.param
@pytest.fixture
def two():
return 2
@pytest.mark.parametrize('one', [
lazy_fixture('two')
], indirect=True)
def test_func(one):
assert one == 2
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_lazy_fixture_as_param_of_fixture(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[
lazy_fixture('one'),
lazy_fixture('two')
])
def some(request):
return request.param
@pytest.fixture
def one():
return 1
@pytest.fixture
def two():
return 2
def test_func(some):
assert some in [1, 2]
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=2)
def test_lazy_fixture_in_params_which_has_params(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[1, 2, 3])
def one(request):
return str(request.param)
@pytest.fixture
def two():
return 4
@pytest.fixture(params=[
lazy_fixture('one'),
lazy_fixture('two')
])
def some(request):
return request.param
def test_func(some):
assert some in {'1', '2', '3', 4}
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=4)
def test_lazy_fixture_three_times_nested(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[
1, 2, lazy_fixture('three')])
def one(request):
return str(request.param)
@pytest.fixture
def two():
return 4
@pytest.fixture
def three():
return 3
@pytest.fixture(params=[
lazy_fixture('one'),
lazy_fixture('two')
])
def some(request):
return request.param
def test_func(some):
assert some in {'1', '2', '3', 4}
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=4)
def test_lazy_fixture_three_times_nested_with_one_failed(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[
1, 2, lazy_fixture('three')
])
def one(request):
return str(request.param)
@pytest.fixture
def two():
return 4
@pytest.fixture
def three():
return 5
@pytest.fixture(params=[
lazy_fixture('one'),
lazy_fixture('two')
])
def some(request):
return request.param
def test_func(some):
assert some in {'1', '2', '3', 4}
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=3, failed=1)
def test_lazy_fixture_common_dependency(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[1, 2, 3])
def one(request):
return request.param
@pytest.fixture(params=[lazy_fixture('one')])
def as_str(request):
return str(request.param)
@pytest.fixture(params=[lazy_fixture('one')])
def as_hex(request):
return hex(request.param)
def test_as_str(as_str):
assert as_str in {'1', '2', '3'}
def test_as_hex(as_hex):
assert as_hex in {'0x1', '0x2', '0x3'}
def test_as_hex_vs_as_str(as_str, as_hex):
assert int(as_hex, 16) == int(as_str)
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=9)
def test_lazy_fixture_common_dependency_with_getfixturevalue(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[1, 2, 3])
def one(request):
return request.param
@pytest.fixture(params=[lazy_fixture('one')])
def as_str(request):
return str(request.getfixturevalue('one'))
@pytest.fixture(params=[lazy_fixture('one')])
def as_hex(request):
return hex(request.getfixturevalue('one'))
def test_as_str(as_str):
assert as_str in {'1', '2', '3'}
def test_as_hex(as_hex):
assert as_hex in {'0x1', '0x2', '0x3'}
def test_as_hex_vs_as_str(as_str, as_hex):
assert int(as_hex, 16) == int(as_str)
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=9)
def test_issues2(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[1, 2, 3])
def one(request):
return request.param
@pytest.fixture(params=[lazy_fixture('one')])
def as_str(request):
return str(request.getfixturevalue('one'))
@pytest.mark.parametrize('val', ('a', 'b', 'c'))
def test_as_str(val, as_str):
combined = ''.join((val, as_str))
assert combined in {'a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3'}
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=9)
def test_issues2_2(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[1, 2, 3])
def one(request):
return request.param
@pytest.fixture(params=[lazy_fixture('one')])
def as_str(request):
return str(request.getfixturevalue('one'))
@pytest.mark.parametrize('val, one', (
('a', '1'), ('b', '2'), ('c', '3')
), indirect=['one'])
def test_as_str(val, one, as_str):
combined = ''.join((val, as_str))
assert combined in {'a1', 'b2', 'c3'}
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=3)
def test_issues3_autouse_fixtures_should_run_first(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
gl = False
@pytest.fixture(autouse=True)
def auto_one():
global gl
gl = True
@pytest.fixture
def one():
return 1 if gl is True else -1
@pytest.mark.parametrize('arg1', [
lazy_fixture('one')
])
def test_some(arg1):
assert arg1 == 1
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=1)
def test_issues10_xfail(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
def division(a, b):
return a / b
@pytest.fixture(params=[0])
def zero(request):
return request.param
@pytest.mark.parametrize(('a', 'b'), [
pytest.param(1, lazy_fixture('zero'), marks=pytest.mark.xfail(reason=ZeroDivisionError))
])
def test_division(a, b):
division(a, b)
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(skipped=1)
def test_issues11_autouse_fixture_in_test_class(testdir):
testdir.makepyfile("""
import pytest
class TestModels(object):
@pytest.fixture(autouse=True)
def setup(self):
self.var = 15
def test_model_a(self):
assert self.var == 15
def test_model_b(self):
assert self.var == 15
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=2)
def test_issues12_skip_test_function(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one():
return 1
@pytest.mark.parametrize('a', [
pytest.param(lazy_fixture('one'), marks=pytest.mark.skip(reason='skip'))
])
def test_skip1(a):
assert a == 1
@pytest.mark.skip(reason='skip')
@pytest.mark.parametrize('a', [
lazy_fixture('one')
])
def test_skip2(a):
assert a == 1
def test_after_skip(one):
assert one == 1
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(skipped=2, passed=1)
def test_issues12_skip_test_method(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
class TestModels:
@pytest.fixture
def one(self):
return 1
@pytest.mark.skip(reason='skip this')
@pytest.mark.parametrize('a', [
lazy_fixture('one')
])
def test_model_a(self, a):
assert a == 1
@pytest.mark.parametrize('a', [
pytest.param(lazy_fixture('one'), marks=pytest.mark.skip(reason='skip this'))
])
def test_model_b(self, a):
assert a == 1
def test_after_skip(self, one):
assert one == 1
""")
reprec = testdir.runpytest('-s', '-v')
reprec.assert_outcomes(skipped=2, passed=1)
def test_issues12_lf_as_method_of_test_class(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
class TestModels:
@pytest.fixture
def one(self):
return 1
@pytest.mark.parametrize('a', [
lazy_fixture('one')
])
def test_lf(self, a):
assert a == 1
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=1)
def test_issues13_unittest_testcase_class_should_not_fail(testdir):
testdir.makepyfile("""
import unittest
import pytest
class TestModels(unittest.TestCase):
def test_models(self):
assert True
def test_models_fail(self):
assert False
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=1, failed=1)
def test_argnames_initialized_in_right_order(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one():
return [1]
@pytest.fixture
def plus_two(a):
a[0] = a[0] + 2
@pytest.mark.parametrize('a,b', [
(lazy_fixture('one'), lazy_fixture('plus_two'))
])
def test_skip1(a, b):
assert a == [3]
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=1)
# https://github.com/TvoroG/pytest-lazy-fixture/pull/19
def test_argnames_initialized_in_right_order2(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one():
return [1]
@pytest.fixture
def plus_two(a):
a[0] = a[0] + 2
def test_skip1(a):
assert a == [3]
def pytest_generate_tests(metafunc):
metafunc.fixturenames = ['a', 'b']
metafunc.parametrize(argnames=['a', 'b'],
argvalues=[(lazy_fixture('one'), lazy_fixture('plus_two'))],
indirect=['b'])
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=1)
def lf(fname):
return lazy_fixture(fname)
@pytest.mark.parametrize('params,expected_paths', [
(
{'some': lf('one'), 'one': lf('three')},
['one>some'],
),
(
{'grand1': lf('parent1_1'), 'parent1_1': lf('child1'),
'grand2': lf('parent1_2'), 'parent1_2': lf('child1'),
'child1': lf('none')},
['child1>parent1_1>grand1>parent1_2>grand2', 'child1>parent1_2>grand2>parent1_1>grand1']
),
(
{'param1': 'val1', 'param2': 'val2'},
['param1>param2', 'param2>param1']
),
({}, ['']),
({'param1': 'val1'}, ['param1']),
({'param1': lf('some')}, ['param1']),
(
{'one': 1, 'as_str': lf('one'), 'as_hex': lf('one')},
['one>as_str>as_hex', 'one>as_hex>as_str']
)
])
def test_sorted_by_dependency(params, expected_paths):
sp = sorted_by_dependency(params, [])
path = '>'.join(param for param, _ in sp)
assert path in expected_paths
@pytest.mark.parametrize('params,fixturenames,expect_keys', [
({'b': 1, 'a': 0}, ['c', 'a', 'd', 'b'], ['c', 'a', 'd', 'b']),
({'b': 1, 'a': 0}, ['c', 'b'], ['c', 'b', 'a'])
])
def test_sorted_argnames(params, fixturenames, expect_keys):
assert list(_sorted_argnames(params, fixturenames)) == expect_keys
def test_lazy_fixtures_with_subfixtures(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=["a", "A"])
def a(request):
return request.param
@pytest.fixture(params=["b", "B"])
def b(a, request):
return request.param + a
@pytest.fixture
def c(a):
return "c" + a
@pytest.fixture(params=[lazy_fixture('a'), lazy_fixture('b'), lazy_fixture('c')])
def d(request):
return "d" + request.param
@pytest.fixture(params=[lazy_fixture('a'), lazy_fixture('d'), ""])
def e(request):
return "e" + request.param
def test_one(d):
assert d in ("da", "dA", "dba", "dbA", "dBa", "dBA", "dca", "dcA")
def test_two(e):
assert e in ("ea", "eA", "eda", "edA", "edba", "edbA", "edBa", "edBA", "edca", "edcA", "e")
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=19)
def test_lazy_fixtures_in_subfixture(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def a():
return "a"
@pytest.fixture
def b():
return "b"
@pytest.fixture(params=[lazy_fixture('a'), lazy_fixture('b')])
def c(request):
return "c" + request.param
@pytest.fixture
def d(c):
return "d" + c
def test_one(d):
assert d in ("dca", "dcb")
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=2)
@pytest.mark.parametrize('autouse', [False, True])
def test_issues23(testdir, autouse):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture(params=[0, 1], autouse={})
def zero(request):
return request.param
@pytest.fixture(params=[1])
def one(request, zero):
return zero * request.param
@pytest.fixture(params=[
lazy_fixture('one'),
])
def some(request):
return request.param
def test_func(some):
assert some in [0, 1]
""".format(autouse))
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=2)
def test_lazy_fixture_nested_fixtures(testdir):
testdir.makepyfile("""
from pytest_lazyfixture import lazy_fixture
import pytest
@pytest.fixture
def one(request):
return "SOME_VALUE"
@pytest.fixture
def two(request):
return "SOME_VALUE2"
@pytest.fixture(params=[
lazy_fixture("one"),
lazy_fixture("two"),
])
def some_fixture1(request):
return request.param
@pytest.fixture
def some_fixture2(some_fixture1):
return "NEW_" + some_fixture1
def test_func(some_fixture2):
assert ((some_fixture2 == "NEW_SOME_VALUE") or (some_fixture2 == "NEW_SOME_VALUE2"))
""")
reprec = testdir.inline_run('-s')
reprec.assertoutcome(passed=2)
# https://github.com/TvoroG/pytest-lazy-fixture/issues/39
def test_usefixture_runs_before_function_fixtures(testdir):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append('using fixture1')
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append('using fixture2')
return 'fixture2'
@pytest.mark.usefixtures("module_fixture")
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""")
result = testdir.runpytest('-s')
stdout = result.stdout.str()
assert (
'using module fixture using fixture1 using module fixture using fixture2' in stdout
)
# https://github.com/TvoroG/pytest-lazy-fixture/issues/39
def test_autouse_and_usefixture_module_scope_runs_before_function_fixtures(testdir):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture(autouse=True)
def autouse_fixture():
invocation_order.append('using autouse_fixture')
@pytest.fixture(scope='module')
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append('using fixture1')
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append('using fixture2')
return 'fixture2'
@pytest.mark.usefixtures("module_fixture")
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""")
result = testdir.runpytest('-s')
stdout = result.stdout.str()
assert (
# pytest==3.2.5
'using autouse_fixture using module fixture using fixture1 using autouse_fixture using fixture2' in stdout
or
'using module fixture using autouse_fixture using fixture1 using autouse_fixture using fixture2' in stdout
)
@pytest.mark.parametrize('autouse_scope', [
'session',
'module',
pytest.param('function', marks=pytest.mark.xfail)
])
def test_session_autouse_and_usefixture_module_scope_runs_before_function_fixtures(testdir, autouse_scope):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture(autouse=True, scope='{autouse_scope}')
def autouse_fixture():
invocation_order.append('using autouse_fixture')
@pytest.fixture(scope='module')
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append("using fixture1")
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append("using fixture2")
return 'fixture2'
@pytest.mark.usefixtures("module_fixture")
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""".format(autouse_scope=autouse_scope))
result = testdir.runpytest('-s')
assert 'using autouse_fixture using module fixture using fixture1 using fixture2' in result.stdout.str()
# https://github.com/TvoroG/pytest-lazy-fixture/issues/39
def test_module_scope_runs_before_function_fixtures(testdir):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
invocation_order = []
@pytest.fixture(scope='module')
def module_fixture():
invocation_order.append('using module fixture')
@pytest.fixture
def fixture1():
invocation_order.append("using fixture1")
return 'fixture1'
@pytest.fixture
def fixture2():
invocation_order.append("using fixture2")
return 'fixture2'
@pytest.mark.parametrize("fixt", [lazy_fixture("fixture1"), lazy_fixture("fixture2")])
def test_test(fixt, module_fixture):
if fixt == 'fixture2':
print(' '.join(invocation_order))
""")
result = testdir.runpytest('-s')
stdout = result.stdout.str()
assert (
# pytest==3.2.5
'using fixture1 using module fixture using fixture2' in stdout
or
'using module fixture using fixture1 using fixture2' in stdout
)
# https://github.com/TvoroG/pytest-lazy-fixture/issues/42
@pytest.mark.skipif(numpy is None, reason='numpy is not installed')
def test_numpy_array_as_value(testdir):
testdir.makepyfile("""
import pytest
import numpy as np
@pytest.mark.parametrize(
'value',
[
np.arange(10, dtype=np.int64),
np.arange(10, dtype=np.int32),
]
)
def test_bug(value):
assert isinstance(value, np.ndarray)
""")
result = testdir.inline_run('-s')
result.assertoutcome(passed=2)
# https://github.com/TvoroG/pytest-lazy-fixture/issues/46
def test_lazy_fixture_ids(testdir):
testdir.makepyfile("""
import pytest
from pytest_lazyfixture import lazy_fixture
@pytest.fixture()
def foo():
return "foo"
@pytest.fixture(params=['spam', 'eggs'])
def bar(request):
return "bar-{}".format(request.param)
@pytest.mark.parametrize("data", [lazy_fixture("foo"),
lazy_fixture("bar")])
def test_the_thing(data):
assert False
""")
result = testdir.runpytest('--collect-only')
stdout = result.stdout.str()
assert 'test_the_thing[foo]' in stdout
assert 'test_the_thing[bar-spam]' in stdout
assert 'test_the_thing[bar-eggs]' in stdout
def test_eq():
assert lazy_fixture("Lol") == lazy_fixture("Lol")
assert lazy_fixture("Lol") != lazy_fixture("Wut")
assert lazy_fixture("Lol") != 123