1import sys
2import textwrap
3
4import pytest
5from _pytest import fixtures
6from _pytest.compat import getfuncargnames
7from _pytest.config import ExitCode
8from _pytest.fixtures import FixtureRequest
9from _pytest.pathlib import Path
10from _pytest.pytester import get_public_names
11
12
13def test_getfuncargnames_functions():
14    """Test getfuncargnames for normal functions"""
15
16    def f():
17        raise NotImplementedError()
18
19    assert not getfuncargnames(f)
20
21    def g(arg):
22        raise NotImplementedError()
23
24    assert getfuncargnames(g) == ("arg",)
25
26    def h(arg1, arg2="hello"):
27        raise NotImplementedError()
28
29    assert getfuncargnames(h) == ("arg1",)
30
31    def j(arg1, arg2, arg3="hello"):
32        raise NotImplementedError()
33
34    assert getfuncargnames(j) == ("arg1", "arg2")
35
36
37def test_getfuncargnames_methods():
38    """Test getfuncargnames for normal methods"""
39
40    class A:
41        def f(self, arg1, arg2="hello"):
42            raise NotImplementedError()
43
44    assert getfuncargnames(A().f) == ("arg1",)
45
46
47def test_getfuncargnames_staticmethod():
48    """Test getfuncargnames for staticmethods"""
49
50    class A:
51        @staticmethod
52        def static(arg1, arg2, x=1):
53            raise NotImplementedError()
54
55    assert getfuncargnames(A.static, cls=A) == ("arg1", "arg2")
56
57
58def test_getfuncargnames_partial():
59    """Check getfuncargnames for methods defined with functools.partial (#5701)"""
60    import functools
61
62    def check(arg1, arg2, i):
63        raise NotImplementedError()
64
65    class T:
66        test_ok = functools.partial(check, i=2)
67
68    values = getfuncargnames(T().test_ok, name="test_ok")
69    assert values == ("arg1", "arg2")
70
71
72def test_getfuncargnames_staticmethod_partial():
73    """Check getfuncargnames for staticmethods defined with functools.partial (#5701)"""
74    import functools
75
76    def check(arg1, arg2, i):
77        raise NotImplementedError()
78
79    class T:
80        test_ok = staticmethod(functools.partial(check, i=2))
81
82    values = getfuncargnames(T().test_ok, name="test_ok")
83    assert values == ("arg1", "arg2")
84
85
86@pytest.mark.pytester_example_path("fixtures/fill_fixtures")
87class TestFillFixtures:
88    def test_fillfuncargs_exposed(self):
89        # used by oejskit, kept for compatibility
90        assert pytest._fillfuncargs == fixtures.fillfixtures
91
92    def test_funcarg_lookupfails(self, testdir):
93        testdir.copy_example()
94        result = testdir.runpytest()  # "--collect-only")
95        assert result.ret != 0
96        result.stdout.fnmatch_lines(
97            """
98            *def test_func(some)*
99            *fixture*some*not found*
100            *xyzsomething*
101            """
102        )
103
104    def test_detect_recursive_dependency_error(self, testdir):
105        testdir.copy_example()
106        result = testdir.runpytest()
107        result.stdout.fnmatch_lines(
108            ["*recursive dependency involving fixture 'fix1' detected*"]
109        )
110
111    def test_funcarg_basic(self, testdir):
112        testdir.copy_example()
113        item = testdir.getitem(Path("test_funcarg_basic.py"))
114        item._request._fillfixtures()
115        del item.funcargs["request"]
116        assert len(get_public_names(item.funcargs)) == 2
117        assert item.funcargs["some"] == "test_func"
118        assert item.funcargs["other"] == 42
119
120    def test_funcarg_lookup_modulelevel(self, testdir):
121        testdir.copy_example()
122        reprec = testdir.inline_run()
123        reprec.assertoutcome(passed=2)
124
125    def test_funcarg_lookup_classlevel(self, testdir):
126        p = testdir.copy_example()
127        result = testdir.runpytest(p)
128        result.stdout.fnmatch_lines(["*1 passed*"])
129
130    def test_conftest_funcargs_only_available_in_subdir(self, testdir):
131        testdir.copy_example()
132        result = testdir.runpytest("-v")
133        result.assert_outcomes(passed=2)
134
135    def test_extend_fixture_module_class(self, testdir):
136        testfile = testdir.copy_example()
137        result = testdir.runpytest()
138        result.stdout.fnmatch_lines(["*1 passed*"])
139        result = testdir.runpytest(testfile)
140        result.stdout.fnmatch_lines(["*1 passed*"])
141
142    def test_extend_fixture_conftest_module(self, testdir):
143        p = testdir.copy_example()
144        result = testdir.runpytest()
145        result.stdout.fnmatch_lines(["*1 passed*"])
146        result = testdir.runpytest(str(next(Path(str(p)).rglob("test_*.py"))))
147        result.stdout.fnmatch_lines(["*1 passed*"])
148
149    def test_extend_fixture_conftest_conftest(self, testdir):
150        p = testdir.copy_example()
151        result = testdir.runpytest()
152        result.stdout.fnmatch_lines(["*1 passed*"])
153        result = testdir.runpytest(str(next(Path(str(p)).rglob("test_*.py"))))
154        result.stdout.fnmatch_lines(["*1 passed*"])
155
156    def test_extend_fixture_conftest_plugin(self, testdir):
157        testdir.makepyfile(
158            testplugin="""
159            import pytest
160
161            @pytest.fixture
162            def foo():
163                return 7
164        """
165        )
166        testdir.syspathinsert()
167        testdir.makeconftest(
168            """
169            import pytest
170
171            pytest_plugins = 'testplugin'
172
173            @pytest.fixture
174            def foo(foo):
175                return foo + 7
176        """
177        )
178        testdir.makepyfile(
179            """
180            def test_foo(foo):
181                assert foo == 14
182        """
183        )
184        result = testdir.runpytest("-s")
185        assert result.ret == 0
186
187    def test_extend_fixture_plugin_plugin(self, testdir):
188        # Two plugins should extend each order in loading order
189        testdir.makepyfile(
190            testplugin0="""
191            import pytest
192
193            @pytest.fixture
194            def foo():
195                return 7
196        """
197        )
198        testdir.makepyfile(
199            testplugin1="""
200            import pytest
201
202            @pytest.fixture
203            def foo(foo):
204                return foo + 7
205        """
206        )
207        testdir.syspathinsert()
208        testdir.makepyfile(
209            """
210            pytest_plugins = ['testplugin0', 'testplugin1']
211
212            def test_foo(foo):
213                assert foo == 14
214        """
215        )
216        result = testdir.runpytest()
217        assert result.ret == 0
218
219    def test_override_parametrized_fixture_conftest_module(self, testdir):
220        """Test override of the parametrized fixture with non-parametrized one on the test module level."""
221        testdir.makeconftest(
222            """
223            import pytest
224
225            @pytest.fixture(params=[1, 2, 3])
226            def spam(request):
227                return request.param
228        """
229        )
230        testfile = testdir.makepyfile(
231            """
232            import pytest
233
234            @pytest.fixture
235            def spam():
236                return 'spam'
237
238            def test_spam(spam):
239                assert spam == 'spam'
240        """
241        )
242        result = testdir.runpytest()
243        result.stdout.fnmatch_lines(["*1 passed*"])
244        result = testdir.runpytest(testfile)
245        result.stdout.fnmatch_lines(["*1 passed*"])
246
247    def test_override_parametrized_fixture_conftest_conftest(self, testdir):
248        """Test override of the parametrized fixture with non-parametrized one on the conftest level."""
249        testdir.makeconftest(
250            """
251            import pytest
252
253            @pytest.fixture(params=[1, 2, 3])
254            def spam(request):
255                return request.param
256        """
257        )
258        subdir = testdir.mkpydir("subdir")
259        subdir.join("conftest.py").write(
260            textwrap.dedent(
261                """\
262                import pytest
263
264                @pytest.fixture
265                def spam():
266                    return 'spam'
267                """
268            )
269        )
270        testfile = subdir.join("test_spam.py")
271        testfile.write(
272            textwrap.dedent(
273                """\
274                def test_spam(spam):
275                    assert spam == "spam"
276                """
277            )
278        )
279        result = testdir.runpytest()
280        result.stdout.fnmatch_lines(["*1 passed*"])
281        result = testdir.runpytest(testfile)
282        result.stdout.fnmatch_lines(["*1 passed*"])
283
284    def test_override_non_parametrized_fixture_conftest_module(self, testdir):
285        """Test override of the non-parametrized fixture with parametrized one on the test module level."""
286        testdir.makeconftest(
287            """
288            import pytest
289
290            @pytest.fixture
291            def spam():
292                return 'spam'
293        """
294        )
295        testfile = testdir.makepyfile(
296            """
297            import pytest
298
299            @pytest.fixture(params=[1, 2, 3])
300            def spam(request):
301                return request.param
302
303            params = {'spam': 1}
304
305            def test_spam(spam):
306                assert spam == params['spam']
307                params['spam'] += 1
308        """
309        )
310        result = testdir.runpytest()
311        result.stdout.fnmatch_lines(["*3 passed*"])
312        result = testdir.runpytest(testfile)
313        result.stdout.fnmatch_lines(["*3 passed*"])
314
315    def test_override_non_parametrized_fixture_conftest_conftest(self, testdir):
316        """Test override of the non-parametrized fixture with parametrized one on the conftest level."""
317        testdir.makeconftest(
318            """
319            import pytest
320
321            @pytest.fixture
322            def spam():
323                return 'spam'
324        """
325        )
326        subdir = testdir.mkpydir("subdir")
327        subdir.join("conftest.py").write(
328            textwrap.dedent(
329                """\
330                import pytest
331
332                @pytest.fixture(params=[1, 2, 3])
333                def spam(request):
334                    return request.param
335                """
336            )
337        )
338        testfile = subdir.join("test_spam.py")
339        testfile.write(
340            textwrap.dedent(
341                """\
342                params = {'spam': 1}
343
344                def test_spam(spam):
345                    assert spam == params['spam']
346                    params['spam'] += 1
347                """
348            )
349        )
350        result = testdir.runpytest()
351        result.stdout.fnmatch_lines(["*3 passed*"])
352        result = testdir.runpytest(testfile)
353        result.stdout.fnmatch_lines(["*3 passed*"])
354
355    def test_override_autouse_fixture_with_parametrized_fixture_conftest_conftest(
356        self, testdir
357    ):
358        """Test override of the autouse fixture with parametrized one on the conftest level.
359        This test covers the issue explained in issue 1601
360        """
361        testdir.makeconftest(
362            """
363            import pytest
364
365            @pytest.fixture(autouse=True)
366            def spam():
367                return 'spam'
368        """
369        )
370        subdir = testdir.mkpydir("subdir")
371        subdir.join("conftest.py").write(
372            textwrap.dedent(
373                """\
374                import pytest
375
376                @pytest.fixture(params=[1, 2, 3])
377                def spam(request):
378                    return request.param
379                """
380            )
381        )
382        testfile = subdir.join("test_spam.py")
383        testfile.write(
384            textwrap.dedent(
385                """\
386                params = {'spam': 1}
387
388                def test_spam(spam):
389                    assert spam == params['spam']
390                    params['spam'] += 1
391                """
392            )
393        )
394        result = testdir.runpytest()
395        result.stdout.fnmatch_lines(["*3 passed*"])
396        result = testdir.runpytest(testfile)
397        result.stdout.fnmatch_lines(["*3 passed*"])
398
399    def test_override_fixture_reusing_super_fixture_parametrization(self, testdir):
400        """Override a fixture at a lower level, reusing the higher-level fixture that
401        is parametrized (#1953).
402        """
403        testdir.makeconftest(
404            """
405            import pytest
406
407            @pytest.fixture(params=[1, 2])
408            def foo(request):
409                return request.param
410            """
411        )
412        testdir.makepyfile(
413            """
414            import pytest
415
416            @pytest.fixture
417            def foo(foo):
418                return foo * 2
419
420            def test_spam(foo):
421                assert foo in (2, 4)
422            """
423        )
424        result = testdir.runpytest()
425        result.stdout.fnmatch_lines(["*2 passed*"])
426
427    def test_override_parametrize_fixture_and_indirect(self, testdir):
428        """Override a fixture at a lower level, reusing the higher-level fixture that
429        is parametrized, while also using indirect parametrization.
430        """
431        testdir.makeconftest(
432            """
433            import pytest
434
435            @pytest.fixture(params=[1, 2])
436            def foo(request):
437                return request.param
438            """
439        )
440        testdir.makepyfile(
441            """
442            import pytest
443
444            @pytest.fixture
445            def foo(foo):
446                return foo * 2
447
448            @pytest.fixture
449            def bar(request):
450                return request.param * 100
451
452            @pytest.mark.parametrize("bar", [42], indirect=True)
453            def test_spam(bar, foo):
454                assert bar == 4200
455                assert foo in (2, 4)
456            """
457        )
458        result = testdir.runpytest()
459        result.stdout.fnmatch_lines(["*2 passed*"])
460
461    def test_override_top_level_fixture_reusing_super_fixture_parametrization(
462        self, testdir
463    ):
464        """Same as the above test, but with another level of overwriting."""
465        testdir.makeconftest(
466            """
467            import pytest
468
469            @pytest.fixture(params=['unused', 'unused'])
470            def foo(request):
471                return request.param
472            """
473        )
474        testdir.makepyfile(
475            """
476            import pytest
477
478            @pytest.fixture(params=[1, 2])
479            def foo(request):
480                return request.param
481
482            class Test:
483
484                @pytest.fixture
485                def foo(self, foo):
486                    return foo * 2
487
488                def test_spam(self, foo):
489                    assert foo in (2, 4)
490            """
491        )
492        result = testdir.runpytest()
493        result.stdout.fnmatch_lines(["*2 passed*"])
494
495    def test_override_parametrized_fixture_with_new_parametrized_fixture(self, testdir):
496        """Overriding a parametrized fixture, while also parametrizing the new fixture and
497        simultaneously requesting the overwritten fixture as parameter, yields the same value
498        as ``request.param``.
499        """
500        testdir.makeconftest(
501            """
502            import pytest
503
504            @pytest.fixture(params=['ignored', 'ignored'])
505            def foo(request):
506                return request.param
507            """
508        )
509        testdir.makepyfile(
510            """
511            import pytest
512
513            @pytest.fixture(params=[10, 20])
514            def foo(foo, request):
515                assert request.param == foo
516                return foo * 2
517
518            def test_spam(foo):
519                assert foo in (20, 40)
520            """
521        )
522        result = testdir.runpytest()
523        result.stdout.fnmatch_lines(["*2 passed*"])
524
525    def test_autouse_fixture_plugin(self, testdir):
526        # A fixture from a plugin has no baseid set, which screwed up
527        # the autouse fixture handling.
528        testdir.makepyfile(
529            testplugin="""
530            import pytest
531
532            @pytest.fixture(autouse=True)
533            def foo(request):
534                request.function.foo = 7
535        """
536        )
537        testdir.syspathinsert()
538        testdir.makepyfile(
539            """
540            pytest_plugins = 'testplugin'
541
542            def test_foo(request):
543                assert request.function.foo == 7
544        """
545        )
546        result = testdir.runpytest()
547        assert result.ret == 0
548
549    def test_funcarg_lookup_error(self, testdir):
550        testdir.makeconftest(
551            """
552            import pytest
553
554            @pytest.fixture
555            def a_fixture(): pass
556
557            @pytest.fixture
558            def b_fixture(): pass
559
560            @pytest.fixture
561            def c_fixture(): pass
562
563            @pytest.fixture
564            def d_fixture(): pass
565        """
566        )
567        testdir.makepyfile(
568            """
569            def test_lookup_error(unknown):
570                pass
571        """
572        )
573        result = testdir.runpytest()
574        result.stdout.fnmatch_lines(
575            [
576                "*ERROR at setup of test_lookup_error*",
577                "  def test_lookup_error(unknown):*",
578                "E       fixture 'unknown' not found",
579                ">       available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*",
580                # sorted
581                ">       use 'py*test --fixtures *' for help on them.",
582                "*1 error*",
583            ]
584        )
585        result.stdout.no_fnmatch_line("*INTERNAL*")
586
587    def test_fixture_excinfo_leak(self, testdir):
588        # on python2 sys.excinfo would leak into fixture executions
589        testdir.makepyfile(
590            """
591            import sys
592            import traceback
593            import pytest
594
595            @pytest.fixture
596            def leak():
597                if sys.exc_info()[0]:  # python3 bug :)
598                    traceback.print_exc()
599                #fails
600                assert sys.exc_info() == (None, None, None)
601
602            def test_leak(leak):
603                if sys.exc_info()[0]:  # python3 bug :)
604                    traceback.print_exc()
605                assert sys.exc_info() == (None, None, None)
606        """
607        )
608        result = testdir.runpytest()
609        assert result.ret == 0
610
611
612class TestRequestBasic:
613    def test_request_attributes(self, testdir):
614        item = testdir.getitem(
615            """
616            import pytest
617
618            @pytest.fixture
619            def something(request): pass
620            def test_func(something): pass
621        """
622        )
623        req = fixtures.FixtureRequest(item)
624        assert req.function == item.obj
625        assert req.keywords == item.keywords
626        assert hasattr(req.module, "test_func")
627        assert req.cls is None
628        assert req.function.__name__ == "test_func"
629        assert req.config == item.config
630        assert repr(req).find(req.function.__name__) != -1
631
632    def test_request_attributes_method(self, testdir):
633        (item,) = testdir.getitems(
634            """
635            import pytest
636            class TestB(object):
637
638                @pytest.fixture
639                def something(self, request):
640                    return 1
641                def test_func(self, something):
642                    pass
643        """
644        )
645        req = item._request
646        assert req.cls.__name__ == "TestB"
647        assert req.instance.__class__ == req.cls
648
649    def test_request_contains_funcarg_arg2fixturedefs(self, testdir):
650        modcol = testdir.getmodulecol(
651            """
652            import pytest
653            @pytest.fixture
654            def something(request):
655                pass
656            class TestClass(object):
657                def test_method(self, something):
658                    pass
659        """
660        )
661        (item1,) = testdir.genitems([modcol])
662        assert item1.name == "test_method"
663        arg2fixturedefs = fixtures.FixtureRequest(item1)._arg2fixturedefs
664        assert len(arg2fixturedefs) == 1
665        assert arg2fixturedefs["something"][0].argname == "something"
666
667    @pytest.mark.skipif(
668        hasattr(sys, "pypy_version_info"),
669        reason="this method of test doesn't work on pypy",
670    )
671    def test_request_garbage(self, testdir):
672        try:
673            import xdist  # noqa
674        except ImportError:
675            pass
676        else:
677            pytest.xfail("this test is flaky when executed with xdist")
678        testdir.makepyfile(
679            """
680            import sys
681            import pytest
682            from _pytest.fixtures import PseudoFixtureDef
683            import gc
684
685            @pytest.fixture(autouse=True)
686            def something(request):
687                original = gc.get_debug()
688                gc.set_debug(gc.DEBUG_SAVEALL)
689                gc.collect()
690
691                yield
692
693                try:
694                    gc.collect()
695                    leaked = [x for _ in gc.garbage if isinstance(_, PseudoFixtureDef)]
696                    assert leaked == []
697                finally:
698                    gc.set_debug(original)
699
700            def test_func():
701                pass
702        """
703        )
704        result = testdir.runpytest_subprocess()
705        result.stdout.fnmatch_lines(["* 1 passed in *"])
706
707    def test_getfixturevalue_recursive(self, testdir):
708        testdir.makeconftest(
709            """
710            import pytest
711
712            @pytest.fixture
713            def something(request):
714                return 1
715        """
716        )
717        testdir.makepyfile(
718            """
719            import pytest
720
721            @pytest.fixture
722            def something(request):
723                return request.getfixturevalue("something") + 1
724            def test_func(something):
725                assert something == 2
726        """
727        )
728        reprec = testdir.inline_run()
729        reprec.assertoutcome(passed=1)
730
731    def test_getfixturevalue_teardown(self, testdir):
732        """
733        Issue #1895
734
735        `test_inner` requests `inner` fixture, which in turn requests `resource`
736        using `getfixturevalue`. `test_func` then requests `resource`.
737
738        `resource` is teardown before `inner` because the fixture mechanism won't consider
739        `inner` dependent on `resource` when it is used via `getfixturevalue`: `test_func`
740        will then cause the `resource`'s finalizer to be called first because of this.
741        """
742        testdir.makepyfile(
743            """
744            import pytest
745
746            @pytest.fixture(scope='session')
747            def resource():
748                r = ['value']
749                yield r
750                r.pop()
751
752            @pytest.fixture(scope='session')
753            def inner(request):
754                resource = request.getfixturevalue('resource')
755                assert resource == ['value']
756                yield
757                assert resource == ['value']
758
759            def test_inner(inner):
760                pass
761
762            def test_func(resource):
763                pass
764        """
765        )
766        result = testdir.runpytest()
767        result.stdout.fnmatch_lines(["* 2 passed in *"])
768
769    def test_getfixturevalue(self, testdir):
770        item = testdir.getitem(
771            """
772            import pytest
773            values = [2]
774            @pytest.fixture
775            def something(request): return 1
776            @pytest.fixture
777            def other(request):
778                return values.pop()
779            def test_func(something): pass
780        """
781        )
782        req = item._request
783
784        with pytest.raises(pytest.FixtureLookupError):
785            req.getfixturevalue("notexists")
786        val = req.getfixturevalue("something")
787        assert val == 1
788        val = req.getfixturevalue("something")
789        assert val == 1
790        val2 = req.getfixturevalue("other")
791        assert val2 == 2
792        val2 = req.getfixturevalue("other")  # see about caching
793        assert val2 == 2
794        item._request._fillfixtures()
795        assert item.funcargs["something"] == 1
796        assert len(get_public_names(item.funcargs)) == 2
797        assert "request" in item.funcargs
798
799    def test_request_addfinalizer(self, testdir):
800        item = testdir.getitem(
801            """
802            import pytest
803            teardownlist = []
804            @pytest.fixture
805            def something(request):
806                request.addfinalizer(lambda: teardownlist.append(1))
807            def test_func(something): pass
808        """
809        )
810        item.session._setupstate.prepare(item)
811        item._request._fillfixtures()
812        # successively check finalization calls
813        teardownlist = item.getparent(pytest.Module).obj.teardownlist
814        ss = item.session._setupstate
815        assert not teardownlist
816        ss.teardown_exact(item, None)
817        print(ss.stack)
818        assert teardownlist == [1]
819
820    def test_request_addfinalizer_failing_setup(self, testdir):
821        testdir.makepyfile(
822            """
823            import pytest
824            values = [1]
825            @pytest.fixture
826            def myfix(request):
827                request.addfinalizer(values.pop)
828                assert 0
829            def test_fix(myfix):
830                pass
831            def test_finalizer_ran():
832                assert not values
833        """
834        )
835        reprec = testdir.inline_run("-s")
836        reprec.assertoutcome(failed=1, passed=1)
837
838    def test_request_addfinalizer_failing_setup_module(self, testdir):
839        testdir.makepyfile(
840            """
841            import pytest
842            values = [1, 2]
843            @pytest.fixture(scope="module")
844            def myfix(request):
845                request.addfinalizer(values.pop)
846                request.addfinalizer(values.pop)
847                assert 0
848            def test_fix(myfix):
849                pass
850        """
851        )
852        reprec = testdir.inline_run("-s")
853        mod = reprec.getcalls("pytest_runtest_setup")[0].item.module
854        assert not mod.values
855
856    def test_request_addfinalizer_partial_setup_failure(self, testdir):
857        p = testdir.makepyfile(
858            """
859            import pytest
860            values = []
861            @pytest.fixture
862            def something(request):
863                request.addfinalizer(lambda: values.append(None))
864            def test_func(something, missingarg):
865                pass
866            def test_second():
867                assert len(values) == 1
868        """
869        )
870        result = testdir.runpytest(p)
871        result.stdout.fnmatch_lines(
872            ["*1 error*"]  # XXX the whole module collection fails
873        )
874
875    def test_request_subrequest_addfinalizer_exceptions(self, testdir):
876        """
877        Ensure exceptions raised during teardown by a finalizer are suppressed
878        until all finalizers are called, re-raising the first exception (#2440)
879        """
880        testdir.makepyfile(
881            """
882            import pytest
883            values = []
884            def _excepts(where):
885                raise Exception('Error in %s fixture' % where)
886            @pytest.fixture
887            def subrequest(request):
888                return request
889            @pytest.fixture
890            def something(subrequest):
891                subrequest.addfinalizer(lambda: values.append(1))
892                subrequest.addfinalizer(lambda: values.append(2))
893                subrequest.addfinalizer(lambda: _excepts('something'))
894            @pytest.fixture
895            def excepts(subrequest):
896                subrequest.addfinalizer(lambda: _excepts('excepts'))
897                subrequest.addfinalizer(lambda: values.append(3))
898            def test_first(something, excepts):
899                pass
900            def test_second():
901                assert values == [3, 2, 1]
902        """
903        )
904        result = testdir.runpytest()
905        result.stdout.fnmatch_lines(
906            ["*Exception: Error in excepts fixture", "* 2 passed, 1 error in *"]
907        )
908
909    def test_request_getmodulepath(self, testdir):
910        modcol = testdir.getmodulecol("def test_somefunc(): pass")
911        (item,) = testdir.genitems([modcol])
912        req = fixtures.FixtureRequest(item)
913        assert req.fspath == modcol.fspath
914
915    def test_request_fixturenames(self, testdir):
916        testdir.makepyfile(
917            """
918            import pytest
919            from _pytest.pytester import get_public_names
920            @pytest.fixture()
921            def arg1():
922                pass
923            @pytest.fixture()
924            def farg(arg1):
925                pass
926            @pytest.fixture(autouse=True)
927            def sarg(tmpdir):
928                pass
929            def test_function(request, farg):
930                assert set(get_public_names(request.fixturenames)) == \
931                       set(["tmpdir", "sarg", "arg1", "request", "farg",
932                            "tmp_path", "tmp_path_factory"])
933        """
934        )
935        reprec = testdir.inline_run()
936        reprec.assertoutcome(passed=1)
937
938    def test_request_fixturenames_dynamic_fixture(self, testdir):
939        """Regression test for #3057"""
940        testdir.copy_example("fixtures/test_getfixturevalue_dynamic.py")
941        result = testdir.runpytest()
942        result.stdout.fnmatch_lines(["*1 passed*"])
943
944    def test_setupdecorator_and_xunit(self, testdir):
945        testdir.makepyfile(
946            """
947            import pytest
948            values = []
949            @pytest.fixture(scope='module', autouse=True)
950            def setup_module():
951                values.append("module")
952            @pytest.fixture(autouse=True)
953            def setup_function():
954                values.append("function")
955
956            def test_func():
957                pass
958
959            class TestClass(object):
960                @pytest.fixture(scope="class", autouse=True)
961                def setup_class(self):
962                    values.append("class")
963                @pytest.fixture(autouse=True)
964                def setup_method(self):
965                    values.append("method")
966                def test_method(self):
967                    pass
968            def test_all():
969                assert values == ["module", "function", "class",
970                             "function", "method", "function"]
971        """
972        )
973        reprec = testdir.inline_run("-v")
974        reprec.assertoutcome(passed=3)
975
976    def test_fixtures_sub_subdir_normalize_sep(self, testdir):
977        # this tests that normalization of nodeids takes place
978        b = testdir.mkdir("tests").mkdir("unit")
979        b.join("conftest.py").write(
980            textwrap.dedent(
981                """\
982                import pytest
983                @pytest.fixture
984                def arg1():
985                    pass
986                """
987            )
988        )
989        p = b.join("test_module.py")
990        p.write("def test_func(arg1): pass")
991        result = testdir.runpytest(p, "--fixtures")
992        assert result.ret == 0
993        result.stdout.fnmatch_lines(
994            """
995            *fixtures defined*conftest*
996            *arg1*
997        """
998        )
999
1000    def test_show_fixtures_color_yes(self, testdir):
1001        testdir.makepyfile("def test_this(): assert 1")
1002        result = testdir.runpytest("--color=yes", "--fixtures")
1003        assert "\x1b[32mtmpdir" in result.stdout.str()
1004
1005    def test_newstyle_with_request(self, testdir):
1006        testdir.makepyfile(
1007            """
1008            import pytest
1009            @pytest.fixture()
1010            def arg(request):
1011                pass
1012            def test_1(arg):
1013                pass
1014        """
1015        )
1016        reprec = testdir.inline_run()
1017        reprec.assertoutcome(passed=1)
1018
1019    def test_setupcontext_no_param(self, testdir):
1020        testdir.makepyfile(
1021            """
1022            import pytest
1023            @pytest.fixture(params=[1,2])
1024            def arg(request):
1025                return request.param
1026
1027            @pytest.fixture(autouse=True)
1028            def mysetup(request, arg):
1029                assert not hasattr(request, "param")
1030            def test_1(arg):
1031                assert arg in (1,2)
1032        """
1033        )
1034        reprec = testdir.inline_run()
1035        reprec.assertoutcome(passed=2)
1036
1037
1038class TestRequestMarking:
1039    def test_applymarker(self, testdir):
1040        item1, item2 = testdir.getitems(
1041            """
1042            import pytest
1043
1044            @pytest.fixture
1045            def something(request):
1046                pass
1047            class TestClass(object):
1048                def test_func1(self, something):
1049                    pass
1050                def test_func2(self, something):
1051                    pass
1052        """
1053        )
1054        req1 = fixtures.FixtureRequest(item1)
1055        assert "xfail" not in item1.keywords
1056        req1.applymarker(pytest.mark.xfail)
1057        assert "xfail" in item1.keywords
1058        assert "skipif" not in item1.keywords
1059        req1.applymarker(pytest.mark.skipif)
1060        assert "skipif" in item1.keywords
1061        with pytest.raises(ValueError):
1062            req1.applymarker(42)
1063
1064    def test_accesskeywords(self, testdir):
1065        testdir.makepyfile(
1066            """
1067            import pytest
1068            @pytest.fixture()
1069            def keywords(request):
1070                return request.keywords
1071            @pytest.mark.XYZ
1072            def test_function(keywords):
1073                assert keywords["XYZ"]
1074                assert "abc" not in keywords
1075        """
1076        )
1077        reprec = testdir.inline_run()
1078        reprec.assertoutcome(passed=1)
1079
1080    def test_accessmarker_dynamic(self, testdir):
1081        testdir.makeconftest(
1082            """
1083            import pytest
1084            @pytest.fixture()
1085            def keywords(request):
1086                return request.keywords
1087
1088            @pytest.fixture(scope="class", autouse=True)
1089            def marking(request):
1090                request.applymarker(pytest.mark.XYZ("hello"))
1091        """
1092        )
1093        testdir.makepyfile(
1094            """
1095            import pytest
1096            def test_fun1(keywords):
1097                assert keywords["XYZ"] is not None
1098                assert "abc" not in keywords
1099            def test_fun2(keywords):
1100                assert keywords["XYZ"] is not None
1101                assert "abc" not in keywords
1102        """
1103        )
1104        reprec = testdir.inline_run()
1105        reprec.assertoutcome(passed=2)
1106
1107
1108class TestFixtureUsages:
1109    def test_noargfixturedec(self, testdir):
1110        testdir.makepyfile(
1111            """
1112            import pytest
1113            @pytest.fixture
1114            def arg1():
1115                return 1
1116
1117            def test_func(arg1):
1118                assert arg1 == 1
1119        """
1120        )
1121        reprec = testdir.inline_run()
1122        reprec.assertoutcome(passed=1)
1123
1124    def test_receives_funcargs(self, testdir):
1125        testdir.makepyfile(
1126            """
1127            import pytest
1128            @pytest.fixture()
1129            def arg1():
1130                return 1
1131
1132            @pytest.fixture()
1133            def arg2(arg1):
1134                return arg1 + 1
1135
1136            def test_add(arg2):
1137                assert arg2 == 2
1138            def test_all(arg1, arg2):
1139                assert arg1 == 1
1140                assert arg2 == 2
1141        """
1142        )
1143        reprec = testdir.inline_run()
1144        reprec.assertoutcome(passed=2)
1145
1146    def test_receives_funcargs_scope_mismatch(self, testdir):
1147        testdir.makepyfile(
1148            """
1149            import pytest
1150            @pytest.fixture(scope="function")
1151            def arg1():
1152                return 1
1153
1154            @pytest.fixture(scope="module")
1155            def arg2(arg1):
1156                return arg1 + 1
1157
1158            def test_add(arg2):
1159                assert arg2 == 2
1160        """
1161        )
1162        result = testdir.runpytest()
1163        result.stdout.fnmatch_lines(
1164            [
1165                "*ScopeMismatch*involved factories*",
1166                "test_receives_funcargs_scope_mismatch.py:6:  def arg2(arg1)",
1167                "test_receives_funcargs_scope_mismatch.py:2:  def arg1()",
1168                "*1 error*",
1169            ]
1170        )
1171
1172    def test_receives_funcargs_scope_mismatch_issue660(self, testdir):
1173        testdir.makepyfile(
1174            """
1175            import pytest
1176            @pytest.fixture(scope="function")
1177            def arg1():
1178                return 1
1179
1180            @pytest.fixture(scope="module")
1181            def arg2(arg1):
1182                return arg1 + 1
1183
1184            def test_add(arg1, arg2):
1185                assert arg2 == 2
1186        """
1187        )
1188        result = testdir.runpytest()
1189        result.stdout.fnmatch_lines(
1190            ["*ScopeMismatch*involved factories*", "* def arg2*", "*1 error*"]
1191        )
1192
1193    def test_invalid_scope(self, testdir):
1194        testdir.makepyfile(
1195            """
1196            import pytest
1197            @pytest.fixture(scope="functions")
1198            def badscope():
1199                pass
1200
1201            def test_nothing(badscope):
1202                pass
1203        """
1204        )
1205        result = testdir.runpytest_inprocess()
1206        result.stdout.fnmatch_lines(
1207            "*Fixture 'badscope' from test_invalid_scope.py got an unexpected scope value 'functions'"
1208        )
1209
1210    @pytest.mark.parametrize("scope", ["function", "session"])
1211    def test_parameters_without_eq_semantics(self, scope, testdir):
1212        testdir.makepyfile(
1213            """
1214            class NoEq1:  # fails on `a == b` statement
1215                def __eq__(self, _):
1216                    raise RuntimeError
1217
1218            class NoEq2:  # fails on `if a == b:` statement
1219                def __eq__(self, _):
1220                    class NoBool:
1221                        def __bool__(self):
1222                            raise RuntimeError
1223                    return NoBool()
1224
1225            import pytest
1226            @pytest.fixture(params=[NoEq1(), NoEq2()], scope={scope!r})
1227            def no_eq(request):
1228                return request.param
1229
1230            def test1(no_eq):
1231                pass
1232
1233            def test2(no_eq):
1234                pass
1235        """.format(
1236                scope=scope
1237            )
1238        )
1239        result = testdir.runpytest()
1240        result.stdout.fnmatch_lines(["*4 passed*"])
1241
1242    def test_funcarg_parametrized_and_used_twice(self, testdir):
1243        testdir.makepyfile(
1244            """
1245            import pytest
1246            values = []
1247            @pytest.fixture(params=[1,2])
1248            def arg1(request):
1249                values.append(1)
1250                return request.param
1251
1252            @pytest.fixture()
1253            def arg2(arg1):
1254                return arg1 + 1
1255
1256            def test_add(arg1, arg2):
1257                assert arg2 == arg1 + 1
1258                assert len(values) == arg1
1259        """
1260        )
1261        result = testdir.runpytest()
1262        result.stdout.fnmatch_lines(["*2 passed*"])
1263
1264    def test_factory_uses_unknown_funcarg_as_dependency_error(self, testdir):
1265        testdir.makepyfile(
1266            """
1267            import pytest
1268
1269            @pytest.fixture()
1270            def fail(missing):
1271                return
1272
1273            @pytest.fixture()
1274            def call_fail(fail):
1275                return
1276
1277            def test_missing(call_fail):
1278                pass
1279            """
1280        )
1281        result = testdir.runpytest()
1282        result.stdout.fnmatch_lines(
1283            """
1284            *pytest.fixture()*
1285            *def call_fail(fail)*
1286            *pytest.fixture()*
1287            *def fail*
1288            *fixture*'missing'*not found*
1289        """
1290        )
1291
1292    def test_factory_setup_as_classes_fails(self, testdir):
1293        testdir.makepyfile(
1294            """
1295            import pytest
1296            class arg1(object):
1297                def __init__(self, request):
1298                    self.x = 1
1299            arg1 = pytest.fixture()(arg1)
1300
1301        """
1302        )
1303        reprec = testdir.inline_run()
1304        values = reprec.getfailedcollections()
1305        assert len(values) == 1
1306
1307    def test_usefixtures_marker(self, testdir):
1308        testdir.makepyfile(
1309            """
1310            import pytest
1311
1312            values = []
1313
1314            @pytest.fixture(scope="class")
1315            def myfix(request):
1316                request.cls.hello = "world"
1317                values.append(1)
1318
1319            class TestClass(object):
1320                def test_one(self):
1321                    assert self.hello == "world"
1322                    assert len(values) == 1
1323                def test_two(self):
1324                    assert self.hello == "world"
1325                    assert len(values) == 1
1326            pytest.mark.usefixtures("myfix")(TestClass)
1327        """
1328        )
1329        reprec = testdir.inline_run()
1330        reprec.assertoutcome(passed=2)
1331
1332    def test_usefixtures_ini(self, testdir):
1333        testdir.makeini(
1334            """
1335            [pytest]
1336            usefixtures = myfix
1337        """
1338        )
1339        testdir.makeconftest(
1340            """
1341            import pytest
1342
1343            @pytest.fixture(scope="class")
1344            def myfix(request):
1345                request.cls.hello = "world"
1346
1347        """
1348        )
1349        testdir.makepyfile(
1350            """
1351            class TestClass(object):
1352                def test_one(self):
1353                    assert self.hello == "world"
1354                def test_two(self):
1355                    assert self.hello == "world"
1356        """
1357        )
1358        reprec = testdir.inline_run()
1359        reprec.assertoutcome(passed=2)
1360
1361    def test_usefixtures_seen_in_showmarkers(self, testdir):
1362        result = testdir.runpytest("--markers")
1363        result.stdout.fnmatch_lines(
1364            """
1365            *usefixtures(fixturename1*mark tests*fixtures*
1366        """
1367        )
1368
1369    def test_request_instance_issue203(self, testdir):
1370        testdir.makepyfile(
1371            """
1372            import pytest
1373
1374            class TestClass(object):
1375                @pytest.fixture
1376                def setup1(self, request):
1377                    assert self == request.instance
1378                    self.arg1 = 1
1379                def test_hello(self, setup1):
1380                    assert self.arg1 == 1
1381        """
1382        )
1383        reprec = testdir.inline_run()
1384        reprec.assertoutcome(passed=1)
1385
1386    def test_fixture_parametrized_with_iterator(self, testdir):
1387        testdir.makepyfile(
1388            """
1389            import pytest
1390
1391            values = []
1392            def f():
1393                yield 1
1394                yield 2
1395            dec = pytest.fixture(scope="module", params=f())
1396
1397            @dec
1398            def arg(request):
1399                return request.param
1400            @dec
1401            def arg2(request):
1402                return request.param
1403
1404            def test_1(arg):
1405                values.append(arg)
1406            def test_2(arg2):
1407                values.append(arg2*10)
1408        """
1409        )
1410        reprec = testdir.inline_run("-v")
1411        reprec.assertoutcome(passed=4)
1412        values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
1413        assert values == [1, 2, 10, 20]
1414
1415    def test_setup_functions_as_fixtures(self, testdir):
1416        """Ensure setup_* methods obey fixture scope rules (#517, #3094)."""
1417        testdir.makepyfile(
1418            """
1419            import pytest
1420
1421            DB_INITIALIZED = None
1422
1423            @pytest.fixture(scope="session", autouse=True)
1424            def db():
1425                global DB_INITIALIZED
1426                DB_INITIALIZED = True
1427                yield
1428                DB_INITIALIZED = False
1429
1430            def setup_module():
1431                assert DB_INITIALIZED
1432
1433            def teardown_module():
1434                assert DB_INITIALIZED
1435
1436            class TestClass(object):
1437
1438                def setup_method(self, method):
1439                    assert DB_INITIALIZED
1440
1441                def teardown_method(self, method):
1442                    assert DB_INITIALIZED
1443
1444                def test_printer_1(self):
1445                    pass
1446
1447                def test_printer_2(self):
1448                    pass
1449        """
1450        )
1451        result = testdir.runpytest()
1452        result.stdout.fnmatch_lines(["* 2 passed in *"])
1453
1454
1455class TestFixtureManagerParseFactories:
1456    @pytest.fixture
1457    def testdir(self, request):
1458        testdir = request.getfixturevalue("testdir")
1459        testdir.makeconftest(
1460            """
1461            import pytest
1462
1463            @pytest.fixture
1464            def hello(request):
1465                return "conftest"
1466
1467            @pytest.fixture
1468            def fm(request):
1469                return request._fixturemanager
1470
1471            @pytest.fixture
1472            def item(request):
1473                return request._pyfuncitem
1474        """
1475        )
1476        return testdir
1477
1478    def test_parsefactories_evil_objects_issue214(self, testdir):
1479        testdir.makepyfile(
1480            """
1481            class A(object):
1482                def __call__(self):
1483                    pass
1484                def __getattr__(self, name):
1485                    raise RuntimeError()
1486            a = A()
1487            def test_hello():
1488                pass
1489        """
1490        )
1491        reprec = testdir.inline_run()
1492        reprec.assertoutcome(passed=1, failed=0)
1493
1494    def test_parsefactories_conftest(self, testdir):
1495        testdir.makepyfile(
1496            """
1497            def test_hello(item, fm):
1498                for name in ("fm", "hello", "item"):
1499                    faclist = fm.getfixturedefs(name, item.nodeid)
1500                    assert len(faclist) == 1
1501                    fac = faclist[0]
1502                    assert fac.func.__name__ == name
1503        """
1504        )
1505        reprec = testdir.inline_run("-s")
1506        reprec.assertoutcome(passed=1)
1507
1508    def test_parsefactories_conftest_and_module_and_class(self, testdir):
1509        testdir.makepyfile(
1510            """\
1511            import pytest
1512
1513            @pytest.fixture
1514            def hello(request):
1515                return "module"
1516            class TestClass(object):
1517                @pytest.fixture
1518                def hello(self, request):
1519                    return "class"
1520                def test_hello(self, item, fm):
1521                    faclist = fm.getfixturedefs("hello", item.nodeid)
1522                    print(faclist)
1523                    assert len(faclist) == 3
1524
1525                    assert faclist[0].func(item._request) == "conftest"
1526                    assert faclist[1].func(item._request) == "module"
1527                    assert faclist[2].func(item._request) == "class"
1528            """
1529        )
1530        reprec = testdir.inline_run("-s")
1531        reprec.assertoutcome(passed=1)
1532
1533    def test_parsefactories_relative_node_ids(self, testdir):
1534        # example mostly taken from:
1535        # https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html
1536        runner = testdir.mkdir("runner")
1537        package = testdir.mkdir("package")
1538        package.join("conftest.py").write(
1539            textwrap.dedent(
1540                """\
1541            import pytest
1542            @pytest.fixture
1543            def one():
1544                return 1
1545            """
1546            )
1547        )
1548        package.join("test_x.py").write(
1549            textwrap.dedent(
1550                """\
1551                def test_x(one):
1552                    assert one == 1
1553                """
1554            )
1555        )
1556        sub = package.mkdir("sub")
1557        sub.join("__init__.py").ensure()
1558        sub.join("conftest.py").write(
1559            textwrap.dedent(
1560                """\
1561                import pytest
1562                @pytest.fixture
1563                def one():
1564                    return 2
1565                """
1566            )
1567        )
1568        sub.join("test_y.py").write(
1569            textwrap.dedent(
1570                """\
1571                def test_x(one):
1572                    assert one == 2
1573                """
1574            )
1575        )
1576        reprec = testdir.inline_run()
1577        reprec.assertoutcome(passed=2)
1578        with runner.as_cwd():
1579            reprec = testdir.inline_run("..")
1580            reprec.assertoutcome(passed=2)
1581
1582    def test_package_xunit_fixture(self, testdir):
1583        testdir.makepyfile(
1584            __init__="""\
1585            values = []
1586        """
1587        )
1588        package = testdir.mkdir("package")
1589        package.join("__init__.py").write(
1590            textwrap.dedent(
1591                """\
1592                from .. import values
1593                def setup_module():
1594                    values.append("package")
1595                def teardown_module():
1596                    values[:] = []
1597                """
1598            )
1599        )
1600        package.join("test_x.py").write(
1601            textwrap.dedent(
1602                """\
1603                from .. import values
1604                def test_x():
1605                    assert values == ["package"]
1606                """
1607            )
1608        )
1609        package = testdir.mkdir("package2")
1610        package.join("__init__.py").write(
1611            textwrap.dedent(
1612                """\
1613                from .. import values
1614                def setup_module():
1615                    values.append("package2")
1616                def teardown_module():
1617                    values[:] = []
1618                """
1619            )
1620        )
1621        package.join("test_x.py").write(
1622            textwrap.dedent(
1623                """\
1624                from .. import values
1625                def test_x():
1626                    assert values == ["package2"]
1627                """
1628            )
1629        )
1630        reprec = testdir.inline_run()
1631        reprec.assertoutcome(passed=2)
1632
1633    def test_package_fixture_complex(self, testdir):
1634        testdir.makepyfile(
1635            __init__="""\
1636            values = []
1637        """
1638        )
1639        testdir.syspathinsert(testdir.tmpdir.dirname)
1640        package = testdir.mkdir("package")
1641        package.join("__init__.py").write("")
1642        package.join("conftest.py").write(
1643            textwrap.dedent(
1644                """\
1645                import pytest
1646                from .. import values
1647                @pytest.fixture(scope="package")
1648                def one():
1649                    values.append("package")
1650                    yield values
1651                    values.pop()
1652                @pytest.fixture(scope="package", autouse=True)
1653                def two():
1654                    values.append("package-auto")
1655                    yield values
1656                    values.pop()
1657                """
1658            )
1659        )
1660        package.join("test_x.py").write(
1661            textwrap.dedent(
1662                """\
1663                from .. import values
1664                def test_package_autouse():
1665                    assert values == ["package-auto"]
1666                def test_package(one):
1667                    assert values == ["package-auto", "package"]
1668                """
1669            )
1670        )
1671        reprec = testdir.inline_run()
1672        reprec.assertoutcome(passed=2)
1673
1674    def test_collect_custom_items(self, testdir):
1675        testdir.copy_example("fixtures/custom_item")
1676        result = testdir.runpytest("foo")
1677        result.stdout.fnmatch_lines(["*passed*"])
1678
1679
1680class TestAutouseDiscovery:
1681    @pytest.fixture
1682    def testdir(self, testdir):
1683        testdir.makeconftest(
1684            """
1685            import pytest
1686            @pytest.fixture(autouse=True)
1687            def perfunction(request, tmpdir):
1688                pass
1689
1690            @pytest.fixture()
1691            def arg1(tmpdir):
1692                pass
1693            @pytest.fixture(autouse=True)
1694            def perfunction2(arg1):
1695                pass
1696
1697            @pytest.fixture
1698            def fm(request):
1699                return request._fixturemanager
1700
1701            @pytest.fixture
1702            def item(request):
1703                return request._pyfuncitem
1704        """
1705        )
1706        return testdir
1707
1708    def test_parsefactories_conftest(self, testdir):
1709        testdir.makepyfile(
1710            """
1711            from _pytest.pytester import get_public_names
1712            def test_check_setup(item, fm):
1713                autousenames = fm._getautousenames(item.nodeid)
1714                assert len(get_public_names(autousenames)) == 2
1715                assert "perfunction2" in autousenames
1716                assert "perfunction" in autousenames
1717        """
1718        )
1719        reprec = testdir.inline_run("-s")
1720        reprec.assertoutcome(passed=1)
1721
1722    def test_two_classes_separated_autouse(self, testdir):
1723        testdir.makepyfile(
1724            """
1725            import pytest
1726            class TestA(object):
1727                values = []
1728                @pytest.fixture(autouse=True)
1729                def setup1(self):
1730                    self.values.append(1)
1731                def test_setup1(self):
1732                    assert self.values == [1]
1733            class TestB(object):
1734                values = []
1735                @pytest.fixture(autouse=True)
1736                def setup2(self):
1737                    self.values.append(1)
1738                def test_setup2(self):
1739                    assert self.values == [1]
1740        """
1741        )
1742        reprec = testdir.inline_run()
1743        reprec.assertoutcome(passed=2)
1744
1745    def test_setup_at_classlevel(self, testdir):
1746        testdir.makepyfile(
1747            """
1748            import pytest
1749            class TestClass(object):
1750                @pytest.fixture(autouse=True)
1751                def permethod(self, request):
1752                    request.instance.funcname = request.function.__name__
1753                def test_method1(self):
1754                    assert self.funcname == "test_method1"
1755                def test_method2(self):
1756                    assert self.funcname == "test_method2"
1757        """
1758        )
1759        reprec = testdir.inline_run("-s")
1760        reprec.assertoutcome(passed=2)
1761
1762    @pytest.mark.xfail(reason="'enabled' feature not implemented")
1763    def test_setup_enabled_functionnode(self, testdir):
1764        testdir.makepyfile(
1765            """
1766            import pytest
1767
1768            def enabled(parentnode, markers):
1769                return "needsdb" in markers
1770
1771            @pytest.fixture(params=[1,2])
1772            def db(request):
1773                return request.param
1774
1775            @pytest.fixture(enabled=enabled, autouse=True)
1776            def createdb(db):
1777                pass
1778
1779            def test_func1(request):
1780                assert "db" not in request.fixturenames
1781
1782            @pytest.mark.needsdb
1783            def test_func2(request):
1784                assert "db" in request.fixturenames
1785        """
1786        )
1787        reprec = testdir.inline_run("-s")
1788        reprec.assertoutcome(passed=2)
1789
1790    def test_callables_nocode(self, testdir):
1791        """An imported mock.call would break setup/factory discovery due to
1792        it being callable and __code__ not being a code object."""
1793        testdir.makepyfile(
1794            """
1795           class _call(tuple):
1796               def __call__(self, *k, **kw):
1797                   pass
1798               def __getattr__(self, k):
1799                   return self
1800
1801           call = _call()
1802        """
1803        )
1804        reprec = testdir.inline_run("-s")
1805        reprec.assertoutcome(failed=0, passed=0)
1806
1807    def test_autouse_in_conftests(self, testdir):
1808        a = testdir.mkdir("a")
1809        b = testdir.mkdir("a1")
1810        conftest = testdir.makeconftest(
1811            """
1812            import pytest
1813            @pytest.fixture(autouse=True)
1814            def hello():
1815                xxx
1816        """
1817        )
1818        conftest.move(a.join(conftest.basename))
1819        a.join("test_something.py").write("def test_func(): pass")
1820        b.join("test_otherthing.py").write("def test_func(): pass")
1821        result = testdir.runpytest()
1822        result.stdout.fnmatch_lines(
1823            """
1824            *1 passed*1 error*
1825        """
1826        )
1827
1828    def test_autouse_in_module_and_two_classes(self, testdir):
1829        testdir.makepyfile(
1830            """
1831            import pytest
1832            values = []
1833            @pytest.fixture(autouse=True)
1834            def append1():
1835                values.append("module")
1836            def test_x():
1837                assert values == ["module"]
1838
1839            class TestA(object):
1840                @pytest.fixture(autouse=True)
1841                def append2(self):
1842                    values.append("A")
1843                def test_hello(self):
1844                    assert values == ["module", "module", "A"], values
1845            class TestA2(object):
1846                def test_world(self):
1847                    assert values == ["module", "module", "A", "module"], values
1848        """
1849        )
1850        reprec = testdir.inline_run()
1851        reprec.assertoutcome(passed=3)
1852
1853
1854class TestAutouseManagement:
1855    def test_autouse_conftest_mid_directory(self, testdir):
1856        pkgdir = testdir.mkpydir("xyz123")
1857        pkgdir.join("conftest.py").write(
1858            textwrap.dedent(
1859                """\
1860                import pytest
1861                @pytest.fixture(autouse=True)
1862                def app():
1863                    import sys
1864                    sys._myapp = "hello"
1865                """
1866            )
1867        )
1868        t = pkgdir.ensure("tests", "test_app.py")
1869        t.write(
1870            textwrap.dedent(
1871                """\
1872                import sys
1873                def test_app():
1874                    assert sys._myapp == "hello"
1875                """
1876            )
1877        )
1878        reprec = testdir.inline_run("-s")
1879        reprec.assertoutcome(passed=1)
1880
1881    def test_funcarg_and_setup(self, testdir):
1882        testdir.makepyfile(
1883            """
1884            import pytest
1885            values = []
1886            @pytest.fixture(scope="module")
1887            def arg():
1888                values.append(1)
1889                return 0
1890            @pytest.fixture(scope="module", autouse=True)
1891            def something(arg):
1892                values.append(2)
1893
1894            def test_hello(arg):
1895                assert len(values) == 2
1896                assert values == [1,2]
1897                assert arg == 0
1898
1899            def test_hello2(arg):
1900                assert len(values) == 2
1901                assert values == [1,2]
1902                assert arg == 0
1903        """
1904        )
1905        reprec = testdir.inline_run()
1906        reprec.assertoutcome(passed=2)
1907
1908    def test_uses_parametrized_resource(self, testdir):
1909        testdir.makepyfile(
1910            """
1911            import pytest
1912            values = []
1913            @pytest.fixture(params=[1,2])
1914            def arg(request):
1915                return request.param
1916
1917            @pytest.fixture(autouse=True)
1918            def something(arg):
1919                values.append(arg)
1920
1921            def test_hello():
1922                if len(values) == 1:
1923                    assert values == [1]
1924                elif len(values) == 2:
1925                    assert values == [1, 2]
1926                else:
1927                    0/0
1928
1929        """
1930        )
1931        reprec = testdir.inline_run("-s")
1932        reprec.assertoutcome(passed=2)
1933
1934    def test_session_parametrized_function(self, testdir):
1935        testdir.makepyfile(
1936            """
1937            import pytest
1938
1939            values = []
1940
1941            @pytest.fixture(scope="session", params=[1,2])
1942            def arg(request):
1943               return request.param
1944
1945            @pytest.fixture(scope="function", autouse=True)
1946            def append(request, arg):
1947                if request.function.__name__ == "test_some":
1948                    values.append(arg)
1949
1950            def test_some():
1951                pass
1952
1953            def test_result(arg):
1954                assert len(values) == arg
1955                assert values[:arg] == [1,2][:arg]
1956        """
1957        )
1958        reprec = testdir.inline_run("-v", "-s")
1959        reprec.assertoutcome(passed=4)
1960
1961    def test_class_function_parametrization_finalization(self, testdir):
1962        p = testdir.makeconftest(
1963            """
1964            import pytest
1965            import pprint
1966
1967            values = []
1968
1969            @pytest.fixture(scope="function", params=[1,2])
1970            def farg(request):
1971                return request.param
1972
1973            @pytest.fixture(scope="class", params=list("ab"))
1974            def carg(request):
1975                return request.param
1976
1977            @pytest.fixture(scope="function", autouse=True)
1978            def append(request, farg, carg):
1979                def fin():
1980                    values.append("fin_%s%s" % (carg, farg))
1981                request.addfinalizer(fin)
1982        """
1983        )
1984        testdir.makepyfile(
1985            """
1986            import pytest
1987
1988            class TestClass(object):
1989                def test_1(self):
1990                    pass
1991            class TestClass2(object):
1992                def test_2(self):
1993                    pass
1994        """
1995        )
1996        confcut = "--confcutdir={}".format(testdir.tmpdir)
1997        reprec = testdir.inline_run("-v", "-s", confcut)
1998        reprec.assertoutcome(passed=8)
1999        config = reprec.getcalls("pytest_unconfigure")[0].config
2000        values = config.pluginmanager._getconftestmodules(p, importmode="prepend")[
2001            0
2002        ].values
2003        assert values == ["fin_a1", "fin_a2", "fin_b1", "fin_b2"] * 2
2004
2005    def test_scope_ordering(self, testdir):
2006        testdir.makepyfile(
2007            """
2008            import pytest
2009            values = []
2010            @pytest.fixture(scope="function", autouse=True)
2011            def fappend2():
2012                values.append(2)
2013            @pytest.fixture(scope="class", autouse=True)
2014            def classappend3():
2015                values.append(3)
2016            @pytest.fixture(scope="module", autouse=True)
2017            def mappend():
2018                values.append(1)
2019
2020            class TestHallo(object):
2021                def test_method(self):
2022                    assert values == [1,3,2]
2023        """
2024        )
2025        reprec = testdir.inline_run()
2026        reprec.assertoutcome(passed=1)
2027
2028    def test_parametrization_setup_teardown_ordering(self, testdir):
2029        testdir.makepyfile(
2030            """
2031            import pytest
2032            values = []
2033            def pytest_generate_tests(metafunc):
2034                if metafunc.cls is None:
2035                    assert metafunc.function is test_finish
2036                if metafunc.cls is not None:
2037                    metafunc.parametrize("item", [1,2], scope="class")
2038            class TestClass(object):
2039                @pytest.fixture(scope="class", autouse=True)
2040                def addteardown(self, item, request):
2041                    values.append("setup-%d" % item)
2042                    request.addfinalizer(lambda: values.append("teardown-%d" % item))
2043                def test_step1(self, item):
2044                    values.append("step1-%d" % item)
2045                def test_step2(self, item):
2046                    values.append("step2-%d" % item)
2047
2048            def test_finish():
2049                print(values)
2050                assert values == ["setup-1", "step1-1", "step2-1", "teardown-1",
2051                             "setup-2", "step1-2", "step2-2", "teardown-2",]
2052        """
2053        )
2054        reprec = testdir.inline_run("-s")
2055        reprec.assertoutcome(passed=5)
2056
2057    def test_ordering_autouse_before_explicit(self, testdir):
2058        testdir.makepyfile(
2059            """
2060            import pytest
2061
2062            values = []
2063            @pytest.fixture(autouse=True)
2064            def fix1():
2065                values.append(1)
2066            @pytest.fixture()
2067            def arg1():
2068                values.append(2)
2069            def test_hello(arg1):
2070                assert values == [1,2]
2071        """
2072        )
2073        reprec = testdir.inline_run()
2074        reprec.assertoutcome(passed=1)
2075
2076    @pytest.mark.parametrize("param1", ["", "params=[1]"], ids=["p00", "p01"])
2077    @pytest.mark.parametrize("param2", ["", "params=[1]"], ids=["p10", "p11"])
2078    def test_ordering_dependencies_torndown_first(self, testdir, param1, param2):
2079        """#226"""
2080        testdir.makepyfile(
2081            """
2082            import pytest
2083            values = []
2084            @pytest.fixture(%(param1)s)
2085            def arg1(request):
2086                request.addfinalizer(lambda: values.append("fin1"))
2087                values.append("new1")
2088            @pytest.fixture(%(param2)s)
2089            def arg2(request, arg1):
2090                request.addfinalizer(lambda: values.append("fin2"))
2091                values.append("new2")
2092
2093            def test_arg(arg2):
2094                pass
2095            def test_check():
2096                assert values == ["new1", "new2", "fin2", "fin1"]
2097        """
2098            % locals()
2099        )
2100        reprec = testdir.inline_run("-s")
2101        reprec.assertoutcome(passed=2)
2102
2103
2104class TestFixtureMarker:
2105    def test_parametrize(self, testdir):
2106        testdir.makepyfile(
2107            """
2108            import pytest
2109            @pytest.fixture(params=["a", "b", "c"])
2110            def arg(request):
2111                return request.param
2112            values = []
2113            def test_param(arg):
2114                values.append(arg)
2115            def test_result():
2116                assert values == list("abc")
2117        """
2118        )
2119        reprec = testdir.inline_run()
2120        reprec.assertoutcome(passed=4)
2121
2122    def test_multiple_parametrization_issue_736(self, testdir):
2123        testdir.makepyfile(
2124            """
2125            import pytest
2126
2127            @pytest.fixture(params=[1,2,3])
2128            def foo(request):
2129                return request.param
2130
2131            @pytest.mark.parametrize('foobar', [4,5,6])
2132            def test_issue(foo, foobar):
2133                assert foo in [1,2,3]
2134                assert foobar in [4,5,6]
2135        """
2136        )
2137        reprec = testdir.inline_run()
2138        reprec.assertoutcome(passed=9)
2139
2140    @pytest.mark.parametrize(
2141        "param_args",
2142        ["'fixt, val'", "'fixt,val'", "['fixt', 'val']", "('fixt', 'val')"],
2143    )
2144    def test_override_parametrized_fixture_issue_979(self, testdir, param_args):
2145        """Make sure a parametrized argument can override a parametrized fixture.
2146
2147        This was a regression introduced in the fix for #736.
2148        """
2149        testdir.makepyfile(
2150            """
2151            import pytest
2152
2153            @pytest.fixture(params=[1, 2])
2154            def fixt(request):
2155                return request.param
2156
2157            @pytest.mark.parametrize(%s, [(3, 'x'), (4, 'x')])
2158            def test_foo(fixt, val):
2159                pass
2160        """
2161            % param_args
2162        )
2163        reprec = testdir.inline_run()
2164        reprec.assertoutcome(passed=2)
2165
2166    def test_scope_session(self, testdir):
2167        testdir.makepyfile(
2168            """
2169            import pytest
2170            values = []
2171            @pytest.fixture(scope="module")
2172            def arg():
2173                values.append(1)
2174                return 1
2175
2176            def test_1(arg):
2177                assert arg == 1
2178            def test_2(arg):
2179                assert arg == 1
2180                assert len(values) == 1
2181            class TestClass(object):
2182                def test3(self, arg):
2183                    assert arg == 1
2184                    assert len(values) == 1
2185        """
2186        )
2187        reprec = testdir.inline_run()
2188        reprec.assertoutcome(passed=3)
2189
2190    def test_scope_session_exc(self, testdir):
2191        testdir.makepyfile(
2192            """
2193            import pytest
2194            values = []
2195            @pytest.fixture(scope="session")
2196            def fix():
2197                values.append(1)
2198                pytest.skip('skipping')
2199
2200            def test_1(fix):
2201                pass
2202            def test_2(fix):
2203                pass
2204            def test_last():
2205                assert values == [1]
2206        """
2207        )
2208        reprec = testdir.inline_run()
2209        reprec.assertoutcome(skipped=2, passed=1)
2210
2211    def test_scope_session_exc_two_fix(self, testdir):
2212        testdir.makepyfile(
2213            """
2214            import pytest
2215            values = []
2216            m = []
2217            @pytest.fixture(scope="session")
2218            def a():
2219                values.append(1)
2220                pytest.skip('skipping')
2221            @pytest.fixture(scope="session")
2222            def b(a):
2223                m.append(1)
2224
2225            def test_1(b):
2226                pass
2227            def test_2(b):
2228                pass
2229            def test_last():
2230                assert values == [1]
2231                assert m == []
2232        """
2233        )
2234        reprec = testdir.inline_run()
2235        reprec.assertoutcome(skipped=2, passed=1)
2236
2237    def test_scope_exc(self, testdir):
2238        testdir.makepyfile(
2239            test_foo="""
2240                def test_foo(fix):
2241                    pass
2242            """,
2243            test_bar="""
2244                def test_bar(fix):
2245                    pass
2246            """,
2247            conftest="""
2248                import pytest
2249                reqs = []
2250                @pytest.fixture(scope="session")
2251                def fix(request):
2252                    reqs.append(1)
2253                    pytest.skip()
2254                @pytest.fixture
2255                def req_list():
2256                    return reqs
2257            """,
2258            test_real="""
2259                def test_last(req_list):
2260                    assert req_list == [1]
2261            """,
2262        )
2263        reprec = testdir.inline_run()
2264        reprec.assertoutcome(skipped=2, passed=1)
2265
2266    def test_scope_module_uses_session(self, testdir):
2267        testdir.makepyfile(
2268            """
2269            import pytest
2270            values = []
2271            @pytest.fixture(scope="module")
2272            def arg():
2273                values.append(1)
2274                return 1
2275
2276            def test_1(arg):
2277                assert arg == 1
2278            def test_2(arg):
2279                assert arg == 1
2280                assert len(values) == 1
2281            class TestClass(object):
2282                def test3(self, arg):
2283                    assert arg == 1
2284                    assert len(values) == 1
2285        """
2286        )
2287        reprec = testdir.inline_run()
2288        reprec.assertoutcome(passed=3)
2289
2290    def test_scope_module_and_finalizer(self, testdir):
2291        testdir.makeconftest(
2292            """
2293            import pytest
2294            finalized_list = []
2295            created_list = []
2296            @pytest.fixture(scope="module")
2297            def arg(request):
2298                created_list.append(1)
2299                assert request.scope == "module"
2300                request.addfinalizer(lambda: finalized_list.append(1))
2301            @pytest.fixture
2302            def created(request):
2303                return len(created_list)
2304            @pytest.fixture
2305            def finalized(request):
2306                return len(finalized_list)
2307        """
2308        )
2309        testdir.makepyfile(
2310            test_mod1="""
2311                def test_1(arg, created, finalized):
2312                    assert created == 1
2313                    assert finalized == 0
2314                def test_2(arg, created, finalized):
2315                    assert created == 1
2316                    assert finalized == 0""",
2317            test_mod2="""
2318                def test_3(arg, created, finalized):
2319                    assert created == 2
2320                    assert finalized == 1""",
2321            test_mode3="""
2322                def test_4(arg, created, finalized):
2323                    assert created == 3
2324                    assert finalized == 2
2325            """,
2326        )
2327        reprec = testdir.inline_run()
2328        reprec.assertoutcome(passed=4)
2329
2330    def test_scope_mismatch_various(self, testdir):
2331        testdir.makeconftest(
2332            """
2333            import pytest
2334            finalized = []
2335            created = []
2336            @pytest.fixture(scope="function")
2337            def arg(request):
2338                pass
2339        """
2340        )
2341        testdir.makepyfile(
2342            test_mod1="""
2343                import pytest
2344                @pytest.fixture(scope="session")
2345                def arg(request):
2346                    request.getfixturevalue("arg")
2347                def test_1(arg):
2348                    pass
2349            """
2350        )
2351        result = testdir.runpytest()
2352        assert result.ret != 0
2353        result.stdout.fnmatch_lines(
2354            ["*ScopeMismatch*You tried*function*session*request*"]
2355        )
2356
2357    def test_dynamic_scope(self, testdir):
2358        testdir.makeconftest(
2359            """
2360            import pytest
2361
2362
2363            def pytest_addoption(parser):
2364                parser.addoption("--extend-scope", action="store_true", default=False)
2365
2366
2367            def dynamic_scope(fixture_name, config):
2368                if config.getoption("--extend-scope"):
2369                    return "session"
2370                return "function"
2371
2372
2373            @pytest.fixture(scope=dynamic_scope)
2374            def dynamic_fixture(calls=[]):
2375                calls.append("call")
2376                return len(calls)
2377
2378        """
2379        )
2380
2381        testdir.makepyfile(
2382            """
2383            def test_first(dynamic_fixture):
2384                assert dynamic_fixture == 1
2385
2386
2387            def test_second(dynamic_fixture):
2388                assert dynamic_fixture == 2
2389
2390        """
2391        )
2392
2393        reprec = testdir.inline_run()
2394        reprec.assertoutcome(passed=2)
2395
2396        reprec = testdir.inline_run("--extend-scope")
2397        reprec.assertoutcome(passed=1, failed=1)
2398
2399    def test_dynamic_scope_bad_return(self, testdir):
2400        testdir.makepyfile(
2401            """
2402            import pytest
2403
2404            def dynamic_scope(**_):
2405                return "wrong-scope"
2406
2407            @pytest.fixture(scope=dynamic_scope)
2408            def fixture():
2409                pass
2410
2411        """
2412        )
2413        result = testdir.runpytest()
2414        result.stdout.fnmatch_lines(
2415            "Fixture 'fixture' from test_dynamic_scope_bad_return.py "
2416            "got an unexpected scope value 'wrong-scope'"
2417        )
2418
2419    def test_register_only_with_mark(self, testdir):
2420        testdir.makeconftest(
2421            """
2422            import pytest
2423            @pytest.fixture()
2424            def arg():
2425                return 1
2426        """
2427        )
2428        testdir.makepyfile(
2429            test_mod1="""
2430                import pytest
2431                @pytest.fixture()
2432                def arg(arg):
2433                    return arg + 1
2434                def test_1(arg):
2435                    assert arg == 2
2436            """
2437        )
2438        reprec = testdir.inline_run()
2439        reprec.assertoutcome(passed=1)
2440
2441    def test_parametrize_and_scope(self, testdir):
2442        testdir.makepyfile(
2443            """
2444            import pytest
2445            @pytest.fixture(scope="module", params=["a", "b", "c"])
2446            def arg(request):
2447                return request.param
2448            values = []
2449            def test_param(arg):
2450                values.append(arg)
2451        """
2452        )
2453        reprec = testdir.inline_run("-v")
2454        reprec.assertoutcome(passed=3)
2455        values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
2456        assert len(values) == 3
2457        assert "a" in values
2458        assert "b" in values
2459        assert "c" in values
2460
2461    def test_scope_mismatch(self, testdir):
2462        testdir.makeconftest(
2463            """
2464            import pytest
2465            @pytest.fixture(scope="function")
2466            def arg(request):
2467                pass
2468        """
2469        )
2470        testdir.makepyfile(
2471            """
2472            import pytest
2473            @pytest.fixture(scope="session")
2474            def arg(arg):
2475                pass
2476            def test_mismatch(arg):
2477                pass
2478        """
2479        )
2480        result = testdir.runpytest()
2481        result.stdout.fnmatch_lines(["*ScopeMismatch*", "*1 error*"])
2482
2483    def test_parametrize_separated_order(self, testdir):
2484        testdir.makepyfile(
2485            """
2486            import pytest
2487
2488            @pytest.fixture(scope="module", params=[1, 2])
2489            def arg(request):
2490                return request.param
2491
2492            values = []
2493            def test_1(arg):
2494                values.append(arg)
2495            def test_2(arg):
2496                values.append(arg)
2497        """
2498        )
2499        reprec = testdir.inline_run("-v")
2500        reprec.assertoutcome(passed=4)
2501        values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
2502        assert values == [1, 1, 2, 2]
2503
2504    def test_module_parametrized_ordering(self, testdir):
2505        testdir.makeini(
2506            """
2507            [pytest]
2508            console_output_style=classic
2509        """
2510        )
2511        testdir.makeconftest(
2512            """
2513            import pytest
2514
2515            @pytest.fixture(scope="session", params="s1 s2".split())
2516            def sarg():
2517                pass
2518            @pytest.fixture(scope="module", params="m1 m2".split())
2519            def marg():
2520                pass
2521        """
2522        )
2523        testdir.makepyfile(
2524            test_mod1="""
2525            def test_func(sarg):
2526                pass
2527            def test_func1(marg):
2528                pass
2529        """,
2530            test_mod2="""
2531            def test_func2(sarg):
2532                pass
2533            def test_func3(sarg, marg):
2534                pass
2535            def test_func3b(sarg, marg):
2536                pass
2537            def test_func4(marg):
2538                pass
2539        """,
2540        )
2541        result = testdir.runpytest("-v")
2542        result.stdout.fnmatch_lines(
2543            """
2544            test_mod1.py::test_func[s1] PASSED
2545            test_mod2.py::test_func2[s1] PASSED
2546            test_mod2.py::test_func3[s1-m1] PASSED
2547            test_mod2.py::test_func3b[s1-m1] PASSED
2548            test_mod2.py::test_func3[s1-m2] PASSED
2549            test_mod2.py::test_func3b[s1-m2] PASSED
2550            test_mod1.py::test_func[s2] PASSED
2551            test_mod2.py::test_func2[s2] PASSED
2552            test_mod2.py::test_func3[s2-m1] PASSED
2553            test_mod2.py::test_func3b[s2-m1] PASSED
2554            test_mod2.py::test_func4[m1] PASSED
2555            test_mod2.py::test_func3[s2-m2] PASSED
2556            test_mod2.py::test_func3b[s2-m2] PASSED
2557            test_mod2.py::test_func4[m2] PASSED
2558            test_mod1.py::test_func1[m1] PASSED
2559            test_mod1.py::test_func1[m2] PASSED
2560        """
2561        )
2562
2563    def test_dynamic_parametrized_ordering(self, testdir):
2564        testdir.makeini(
2565            """
2566            [pytest]
2567            console_output_style=classic
2568        """
2569        )
2570        testdir.makeconftest(
2571            """
2572            import pytest
2573
2574            def pytest_configure(config):
2575                class DynamicFixturePlugin(object):
2576                    @pytest.fixture(scope='session', params=['flavor1', 'flavor2'])
2577                    def flavor(self, request):
2578                        return request.param
2579                config.pluginmanager.register(DynamicFixturePlugin(), 'flavor-fixture')
2580
2581            @pytest.fixture(scope='session', params=['vxlan', 'vlan'])
2582            def encap(request):
2583                return request.param
2584
2585            @pytest.fixture(scope='session', autouse='True')
2586            def reprovision(request, flavor, encap):
2587                pass
2588        """
2589        )
2590        testdir.makepyfile(
2591            """
2592            def test(reprovision):
2593                pass
2594            def test2(reprovision):
2595                pass
2596        """
2597        )
2598        result = testdir.runpytest("-v")
2599        result.stdout.fnmatch_lines(
2600            """
2601            test_dynamic_parametrized_ordering.py::test[flavor1-vxlan] PASSED
2602            test_dynamic_parametrized_ordering.py::test2[flavor1-vxlan] PASSED
2603            test_dynamic_parametrized_ordering.py::test[flavor2-vxlan] PASSED
2604            test_dynamic_parametrized_ordering.py::test2[flavor2-vxlan] PASSED
2605            test_dynamic_parametrized_ordering.py::test[flavor2-vlan] PASSED
2606            test_dynamic_parametrized_ordering.py::test2[flavor2-vlan] PASSED
2607            test_dynamic_parametrized_ordering.py::test[flavor1-vlan] PASSED
2608            test_dynamic_parametrized_ordering.py::test2[flavor1-vlan] PASSED
2609        """
2610        )
2611
2612    def test_class_ordering(self, testdir):
2613        testdir.makeini(
2614            """
2615            [pytest]
2616            console_output_style=classic
2617        """
2618        )
2619        testdir.makeconftest(
2620            """
2621            import pytest
2622
2623            values = []
2624
2625            @pytest.fixture(scope="function", params=[1,2])
2626            def farg(request):
2627                return request.param
2628
2629            @pytest.fixture(scope="class", params=list("ab"))
2630            def carg(request):
2631                return request.param
2632
2633            @pytest.fixture(scope="function", autouse=True)
2634            def append(request, farg, carg):
2635                def fin():
2636                    values.append("fin_%s%s" % (carg, farg))
2637                request.addfinalizer(fin)
2638        """
2639        )
2640        testdir.makepyfile(
2641            """
2642            import pytest
2643
2644            class TestClass2(object):
2645                def test_1(self):
2646                    pass
2647                def test_2(self):
2648                    pass
2649            class TestClass(object):
2650                def test_3(self):
2651                    pass
2652        """
2653        )
2654        result = testdir.runpytest("-vs")
2655        result.stdout.re_match_lines(
2656            r"""
2657            test_class_ordering.py::TestClass2::test_1\[a-1\] PASSED
2658            test_class_ordering.py::TestClass2::test_1\[a-2\] PASSED
2659            test_class_ordering.py::TestClass2::test_2\[a-1\] PASSED
2660            test_class_ordering.py::TestClass2::test_2\[a-2\] PASSED
2661            test_class_ordering.py::TestClass2::test_1\[b-1\] PASSED
2662            test_class_ordering.py::TestClass2::test_1\[b-2\] PASSED
2663            test_class_ordering.py::TestClass2::test_2\[b-1\] PASSED
2664            test_class_ordering.py::TestClass2::test_2\[b-2\] PASSED
2665            test_class_ordering.py::TestClass::test_3\[a-1\] PASSED
2666            test_class_ordering.py::TestClass::test_3\[a-2\] PASSED
2667            test_class_ordering.py::TestClass::test_3\[b-1\] PASSED
2668            test_class_ordering.py::TestClass::test_3\[b-2\] PASSED
2669        """
2670        )
2671
2672    def test_parametrize_separated_order_higher_scope_first(self, testdir):
2673        testdir.makepyfile(
2674            """
2675            import pytest
2676
2677            @pytest.fixture(scope="function", params=[1, 2])
2678            def arg(request):
2679                param = request.param
2680                request.addfinalizer(lambda: values.append("fin:%s" % param))
2681                values.append("create:%s" % param)
2682                return request.param
2683
2684            @pytest.fixture(scope="module", params=["mod1", "mod2"])
2685            def modarg(request):
2686                param = request.param
2687                request.addfinalizer(lambda: values.append("fin:%s" % param))
2688                values.append("create:%s" % param)
2689                return request.param
2690
2691            values = []
2692            def test_1(arg):
2693                values.append("test1")
2694            def test_2(modarg):
2695                values.append("test2")
2696            def test_3(arg, modarg):
2697                values.append("test3")
2698            def test_4(modarg, arg):
2699                values.append("test4")
2700        """
2701        )
2702        reprec = testdir.inline_run("-v")
2703        reprec.assertoutcome(passed=12)
2704        values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
2705        expected = [
2706            "create:1",
2707            "test1",
2708            "fin:1",
2709            "create:2",
2710            "test1",
2711            "fin:2",
2712            "create:mod1",
2713            "test2",
2714            "create:1",
2715            "test3",
2716            "fin:1",
2717            "create:2",
2718            "test3",
2719            "fin:2",
2720            "create:1",
2721            "test4",
2722            "fin:1",
2723            "create:2",
2724            "test4",
2725            "fin:2",
2726            "fin:mod1",
2727            "create:mod2",
2728            "test2",
2729            "create:1",
2730            "test3",
2731            "fin:1",
2732            "create:2",
2733            "test3",
2734            "fin:2",
2735            "create:1",
2736            "test4",
2737            "fin:1",
2738            "create:2",
2739            "test4",
2740            "fin:2",
2741            "fin:mod2",
2742        ]
2743        import pprint
2744
2745        pprint.pprint(list(zip(values, expected)))
2746        assert values == expected
2747
2748    def test_parametrized_fixture_teardown_order(self, testdir):
2749        testdir.makepyfile(
2750            """
2751            import pytest
2752            @pytest.fixture(params=[1,2], scope="class")
2753            def param1(request):
2754                return request.param
2755
2756            values = []
2757
2758            class TestClass(object):
2759                @classmethod
2760                @pytest.fixture(scope="class", autouse=True)
2761                def setup1(self, request, param1):
2762                    values.append(1)
2763                    request.addfinalizer(self.teardown1)
2764                @classmethod
2765                def teardown1(self):
2766                    assert values.pop() == 1
2767                @pytest.fixture(scope="class", autouse=True)
2768                def setup2(self, request, param1):
2769                    values.append(2)
2770                    request.addfinalizer(self.teardown2)
2771                @classmethod
2772                def teardown2(self):
2773                    assert values.pop() == 2
2774                def test(self):
2775                    pass
2776
2777            def test_finish():
2778                assert not values
2779        """
2780        )
2781        result = testdir.runpytest("-v")
2782        result.stdout.fnmatch_lines(
2783            """
2784            *3 passed*
2785        """
2786        )
2787        result.stdout.no_fnmatch_line("*error*")
2788
2789    def test_fixture_finalizer(self, testdir):
2790        testdir.makeconftest(
2791            """
2792            import pytest
2793            import sys
2794
2795            @pytest.fixture
2796            def browser(request):
2797
2798                def finalize():
2799                    sys.stdout.write('Finalized')
2800                request.addfinalizer(finalize)
2801                return {}
2802        """
2803        )
2804        b = testdir.mkdir("subdir")
2805        b.join("test_overridden_fixture_finalizer.py").write(
2806            textwrap.dedent(
2807                """\
2808                import pytest
2809                @pytest.fixture
2810                def browser(browser):
2811                    browser['visited'] = True
2812                    return browser
2813
2814                def test_browser(browser):
2815                    assert browser['visited'] is True
2816                """
2817            )
2818        )
2819        reprec = testdir.runpytest("-s")
2820        for test in ["test_browser"]:
2821            reprec.stdout.fnmatch_lines(["*Finalized*"])
2822
2823    def test_class_scope_with_normal_tests(self, testdir):
2824        testpath = testdir.makepyfile(
2825            """
2826            import pytest
2827
2828            class Box(object):
2829                value = 0
2830
2831            @pytest.fixture(scope='class')
2832            def a(request):
2833                Box.value += 1
2834                return Box.value
2835
2836            def test_a(a):
2837                assert a == 1
2838
2839            class Test1(object):
2840                def test_b(self, a):
2841                    assert a == 2
2842
2843            class Test2(object):
2844                def test_c(self, a):
2845                    assert a == 3"""
2846        )
2847        reprec = testdir.inline_run(testpath)
2848        for test in ["test_a", "test_b", "test_c"]:
2849            assert reprec.matchreport(test).passed
2850
2851    def test_request_is_clean(self, testdir):
2852        testdir.makepyfile(
2853            """
2854            import pytest
2855            values = []
2856            @pytest.fixture(params=[1, 2])
2857            def fix(request):
2858                request.addfinalizer(lambda: values.append(request.param))
2859            def test_fix(fix):
2860                pass
2861        """
2862        )
2863        reprec = testdir.inline_run("-s")
2864        values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
2865        assert values == [1, 2]
2866
2867    def test_parametrize_separated_lifecycle(self, testdir):
2868        testdir.makepyfile(
2869            """
2870            import pytest
2871
2872            values = []
2873            @pytest.fixture(scope="module", params=[1, 2])
2874            def arg(request):
2875                x = request.param
2876                request.addfinalizer(lambda: values.append("fin%s" % x))
2877                return request.param
2878            def test_1(arg):
2879                values.append(arg)
2880            def test_2(arg):
2881                values.append(arg)
2882        """
2883        )
2884        reprec = testdir.inline_run("-vs")
2885        reprec.assertoutcome(passed=4)
2886        values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
2887        import pprint
2888
2889        pprint.pprint(values)
2890        # assert len(values) == 6
2891        assert values[0] == values[1] == 1
2892        assert values[2] == "fin1"
2893        assert values[3] == values[4] == 2
2894        assert values[5] == "fin2"
2895
2896    def test_parametrize_function_scoped_finalizers_called(self, testdir):
2897        testdir.makepyfile(
2898            """
2899            import pytest
2900
2901            @pytest.fixture(scope="function", params=[1, 2])
2902            def arg(request):
2903                x = request.param
2904                request.addfinalizer(lambda: values.append("fin%s" % x))
2905                return request.param
2906
2907            values = []
2908            def test_1(arg):
2909                values.append(arg)
2910            def test_2(arg):
2911                values.append(arg)
2912            def test_3():
2913                assert len(values) == 8
2914                assert values == [1, "fin1", 2, "fin2", 1, "fin1", 2, "fin2"]
2915        """
2916        )
2917        reprec = testdir.inline_run("-v")
2918        reprec.assertoutcome(passed=5)
2919
2920    @pytest.mark.parametrize("scope", ["session", "function", "module"])
2921    def test_finalizer_order_on_parametrization(self, scope, testdir):
2922        """#246"""
2923        testdir.makepyfile(
2924            """
2925            import pytest
2926            values = []
2927
2928            @pytest.fixture(scope=%(scope)r, params=["1"])
2929            def fix1(request):
2930                return request.param
2931
2932            @pytest.fixture(scope=%(scope)r)
2933            def fix2(request, base):
2934                def cleanup_fix2():
2935                    assert not values, "base should not have been finalized"
2936                request.addfinalizer(cleanup_fix2)
2937
2938            @pytest.fixture(scope=%(scope)r)
2939            def base(request, fix1):
2940                def cleanup_base():
2941                    values.append("fin_base")
2942                    print("finalizing base")
2943                request.addfinalizer(cleanup_base)
2944
2945            def test_begin():
2946                pass
2947            def test_baz(base, fix2):
2948                pass
2949            def test_other():
2950                pass
2951        """
2952            % {"scope": scope}
2953        )
2954        reprec = testdir.inline_run("-lvs")
2955        reprec.assertoutcome(passed=3)
2956
2957    def test_class_scope_parametrization_ordering(self, testdir):
2958        """#396"""
2959        testdir.makepyfile(
2960            """
2961            import pytest
2962            values = []
2963            @pytest.fixture(params=["John", "Doe"], scope="class")
2964            def human(request):
2965                request.addfinalizer(lambda: values.append("fin %s" % request.param))
2966                return request.param
2967
2968            class TestGreetings(object):
2969                def test_hello(self, human):
2970                    values.append("test_hello")
2971
2972            class TestMetrics(object):
2973                def test_name(self, human):
2974                    values.append("test_name")
2975
2976                def test_population(self, human):
2977                    values.append("test_population")
2978        """
2979        )
2980        reprec = testdir.inline_run()
2981        reprec.assertoutcome(passed=6)
2982        values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
2983        assert values == [
2984            "test_hello",
2985            "fin John",
2986            "test_hello",
2987            "fin Doe",
2988            "test_name",
2989            "test_population",
2990            "fin John",
2991            "test_name",
2992            "test_population",
2993            "fin Doe",
2994        ]
2995
2996    def test_parametrize_setup_function(self, testdir):
2997        testdir.makepyfile(
2998            """
2999            import pytest
3000
3001            @pytest.fixture(scope="module", params=[1, 2])
3002            def arg(request):
3003                return request.param
3004
3005            @pytest.fixture(scope="module", autouse=True)
3006            def mysetup(request, arg):
3007                request.addfinalizer(lambda: values.append("fin%s" % arg))
3008                values.append("setup%s" % arg)
3009
3010            values = []
3011            def test_1(arg):
3012                values.append(arg)
3013            def test_2(arg):
3014                values.append(arg)
3015            def test_3():
3016                import pprint
3017                pprint.pprint(values)
3018                if arg == 1:
3019                    assert values == ["setup1", 1, 1, ]
3020                elif arg == 2:
3021                    assert values == ["setup1", 1, 1, "fin1",
3022                                 "setup2", 2, 2, ]
3023
3024        """
3025        )
3026        reprec = testdir.inline_run("-v")
3027        reprec.assertoutcome(passed=6)
3028
3029    def test_fixture_marked_function_not_collected_as_test(self, testdir):
3030        testdir.makepyfile(
3031            """
3032            import pytest
3033            @pytest.fixture
3034            def test_app():
3035                return 1
3036
3037            def test_something(test_app):
3038                assert test_app == 1
3039        """
3040        )
3041        reprec = testdir.inline_run()
3042        reprec.assertoutcome(passed=1)
3043
3044    def test_params_and_ids(self, testdir):
3045        testdir.makepyfile(
3046            """
3047            import pytest
3048
3049            @pytest.fixture(params=[object(), object()],
3050                            ids=['alpha', 'beta'])
3051            def fix(request):
3052                return request.param
3053
3054            def test_foo(fix):
3055                assert 1
3056        """
3057        )
3058        res = testdir.runpytest("-v")
3059        res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"])
3060
3061    def test_params_and_ids_yieldfixture(self, testdir):
3062        testdir.makepyfile(
3063            """
3064            import pytest
3065
3066            @pytest.fixture(params=[object(), object()], ids=['alpha', 'beta'])
3067            def fix(request):
3068                 yield request.param
3069
3070            def test_foo(fix):
3071                assert 1
3072        """
3073        )
3074        res = testdir.runpytest("-v")
3075        res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"])
3076
3077    def test_deterministic_fixture_collection(self, testdir, monkeypatch):
3078        """#920"""
3079        testdir.makepyfile(
3080            """
3081            import pytest
3082
3083            @pytest.fixture(scope="module",
3084                            params=["A",
3085                                    "B",
3086                                    "C"])
3087            def A(request):
3088                return request.param
3089
3090            @pytest.fixture(scope="module",
3091                            params=["DDDDDDDDD", "EEEEEEEEEEEE", "FFFFFFFFFFF", "banansda"])
3092            def B(request, A):
3093                return request.param
3094
3095            def test_foo(B):
3096                # Something funky is going on here.
3097                # Despite specified seeds, on what is collected,
3098                # sometimes we get unexpected passes. hashing B seems
3099                # to help?
3100                assert hash(B) or True
3101            """
3102        )
3103        monkeypatch.setenv("PYTHONHASHSEED", "1")
3104        out1 = testdir.runpytest_subprocess("-v")
3105        monkeypatch.setenv("PYTHONHASHSEED", "2")
3106        out2 = testdir.runpytest_subprocess("-v")
3107        out1 = [
3108            line
3109            for line in out1.outlines
3110            if line.startswith("test_deterministic_fixture_collection.py::test_foo")
3111        ]
3112        out2 = [
3113            line
3114            for line in out2.outlines
3115            if line.startswith("test_deterministic_fixture_collection.py::test_foo")
3116        ]
3117        assert len(out1) == 12
3118        assert out1 == out2
3119
3120
3121class TestRequestScopeAccess:
3122    pytestmark = pytest.mark.parametrize(
3123        ("scope", "ok", "error"),
3124        [
3125            ["session", "", "fspath class function module"],
3126            ["module", "module fspath", "cls function"],
3127            ["class", "module fspath cls", "function"],
3128            ["function", "module fspath cls function", ""],
3129        ],
3130    )
3131
3132    def test_setup(self, testdir, scope, ok, error):
3133        testdir.makepyfile(
3134            """
3135            import pytest
3136            @pytest.fixture(scope=%r, autouse=True)
3137            def myscoped(request):
3138                for x in %r:
3139                    assert hasattr(request, x)
3140                for x in %r:
3141                    pytest.raises(AttributeError, lambda:
3142                        getattr(request, x))
3143                assert request.session
3144                assert request.config
3145            def test_func():
3146                pass
3147        """
3148            % (scope, ok.split(), error.split())
3149        )
3150        reprec = testdir.inline_run("-l")
3151        reprec.assertoutcome(passed=1)
3152
3153    def test_funcarg(self, testdir, scope, ok, error):
3154        testdir.makepyfile(
3155            """
3156            import pytest
3157            @pytest.fixture(scope=%r)
3158            def arg(request):
3159                for x in %r:
3160                    assert hasattr(request, x)
3161                for x in %r:
3162                    pytest.raises(AttributeError, lambda:
3163                        getattr(request, x))
3164                assert request.session
3165                assert request.config
3166            def test_func(arg):
3167                pass
3168        """
3169            % (scope, ok.split(), error.split())
3170        )
3171        reprec = testdir.inline_run()
3172        reprec.assertoutcome(passed=1)
3173
3174
3175class TestErrors:
3176    def test_subfactory_missing_funcarg(self, testdir):
3177        testdir.makepyfile(
3178            """
3179            import pytest
3180            @pytest.fixture()
3181            def gen(qwe123):
3182                return 1
3183            def test_something(gen):
3184                pass
3185        """
3186        )
3187        result = testdir.runpytest()
3188        assert result.ret != 0
3189        result.stdout.fnmatch_lines(
3190            ["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"]
3191        )
3192
3193    def test_issue498_fixture_finalizer_failing(self, testdir):
3194        testdir.makepyfile(
3195            """
3196            import pytest
3197            @pytest.fixture
3198            def fix1(request):
3199                def f():
3200                    raise KeyError
3201                request.addfinalizer(f)
3202                return object()
3203
3204            values = []
3205            def test_1(fix1):
3206                values.append(fix1)
3207            def test_2(fix1):
3208                values.append(fix1)
3209            def test_3():
3210                assert values[0] != values[1]
3211        """
3212        )
3213        result = testdir.runpytest()
3214        result.stdout.fnmatch_lines(
3215            """
3216            *ERROR*teardown*test_1*
3217            *KeyError*
3218            *ERROR*teardown*test_2*
3219            *KeyError*
3220            *3 pass*2 errors*
3221        """
3222        )
3223
3224    def test_setupfunc_missing_funcarg(self, testdir):
3225        testdir.makepyfile(
3226            """
3227            import pytest
3228            @pytest.fixture(autouse=True)
3229            def gen(qwe123):
3230                return 1
3231            def test_something():
3232                pass
3233        """
3234        )
3235        result = testdir.runpytest()
3236        assert result.ret != 0
3237        result.stdout.fnmatch_lines(
3238            ["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"]
3239        )
3240
3241
3242class TestShowFixtures:
3243    def test_funcarg_compat(self, testdir):
3244        config = testdir.parseconfigure("--funcargs")
3245        assert config.option.showfixtures
3246
3247    def test_show_fixtures(self, testdir):
3248        result = testdir.runpytest("--fixtures")
3249        result.stdout.fnmatch_lines(
3250            [
3251                "tmpdir_factory [[]session scope[]]",
3252                "*for the test session*",
3253                "tmpdir",
3254                "*temporary directory*",
3255            ]
3256        )
3257
3258    def test_show_fixtures_verbose(self, testdir):
3259        result = testdir.runpytest("--fixtures", "-v")
3260        result.stdout.fnmatch_lines(
3261            [
3262                "tmpdir_factory [[]session scope[]] -- *tmpdir.py*",
3263                "*for the test session*",
3264                "tmpdir -- *tmpdir.py*",
3265                "*temporary directory*",
3266            ]
3267        )
3268
3269    def test_show_fixtures_testmodule(self, testdir):
3270        p = testdir.makepyfile(
3271            '''
3272            import pytest
3273            @pytest.fixture
3274            def _arg0():
3275                """ hidden """
3276            @pytest.fixture
3277            def arg1():
3278                """  hello world """
3279        '''
3280        )
3281        result = testdir.runpytest("--fixtures", p)
3282        result.stdout.fnmatch_lines(
3283            """
3284            *tmpdir
3285            *fixtures defined from*
3286            *arg1*
3287            *hello world*
3288        """
3289        )
3290        result.stdout.no_fnmatch_line("*arg0*")
3291
3292    @pytest.mark.parametrize("testmod", [True, False])
3293    def test_show_fixtures_conftest(self, testdir, testmod):
3294        testdir.makeconftest(
3295            '''
3296            import pytest
3297            @pytest.fixture
3298            def arg1():
3299                """  hello world """
3300        '''
3301        )
3302        if testmod:
3303            testdir.makepyfile(
3304                """
3305                def test_hello():
3306                    pass
3307            """
3308            )
3309        result = testdir.runpytest("--fixtures")
3310        result.stdout.fnmatch_lines(
3311            """
3312            *tmpdir*
3313            *fixtures defined from*conftest*
3314            *arg1*
3315            *hello world*
3316        """
3317        )
3318
3319    def test_show_fixtures_trimmed_doc(self, testdir):
3320        p = testdir.makepyfile(
3321            textwrap.dedent(
3322                '''\
3323                import pytest
3324                @pytest.fixture
3325                def arg1():
3326                    """
3327                    line1
3328                    line2
3329
3330                    """
3331                @pytest.fixture
3332                def arg2():
3333                    """
3334                    line1
3335                    line2
3336
3337                    """
3338                '''
3339            )
3340        )
3341        result = testdir.runpytest("--fixtures", p)
3342        result.stdout.fnmatch_lines(
3343            textwrap.dedent(
3344                """\
3345                * fixtures defined from test_show_fixtures_trimmed_doc *
3346                arg2
3347                    line1
3348                    line2
3349                arg1
3350                    line1
3351                    line2
3352                """
3353            )
3354        )
3355
3356    def test_show_fixtures_indented_doc(self, testdir):
3357        p = testdir.makepyfile(
3358            textwrap.dedent(
3359                '''\
3360                import pytest
3361                @pytest.fixture
3362                def fixture1():
3363                    """
3364                    line1
3365                        indented line
3366                    """
3367                '''
3368            )
3369        )
3370        result = testdir.runpytest("--fixtures", p)
3371        result.stdout.fnmatch_lines(
3372            textwrap.dedent(
3373                """\
3374                * fixtures defined from test_show_fixtures_indented_doc *
3375                fixture1
3376                    line1
3377                        indented line
3378                """
3379            )
3380        )
3381
3382    def test_show_fixtures_indented_doc_first_line_unindented(self, testdir):
3383        p = testdir.makepyfile(
3384            textwrap.dedent(
3385                '''\
3386                import pytest
3387                @pytest.fixture
3388                def fixture1():
3389                    """line1
3390                    line2
3391                        indented line
3392                    """
3393                '''
3394            )
3395        )
3396        result = testdir.runpytest("--fixtures", p)
3397        result.stdout.fnmatch_lines(
3398            textwrap.dedent(
3399                """\
3400                * fixtures defined from test_show_fixtures_indented_doc_first_line_unindented *
3401                fixture1
3402                    line1
3403                    line2
3404                        indented line
3405                """
3406            )
3407        )
3408
3409    def test_show_fixtures_indented_in_class(self, testdir):
3410        p = testdir.makepyfile(
3411            textwrap.dedent(
3412                '''\
3413                import pytest
3414                class TestClass(object):
3415                    @pytest.fixture
3416                    def fixture1(self):
3417                        """line1
3418                        line2
3419                            indented line
3420                        """
3421                '''
3422            )
3423        )
3424        result = testdir.runpytest("--fixtures", p)
3425        result.stdout.fnmatch_lines(
3426            textwrap.dedent(
3427                """\
3428                * fixtures defined from test_show_fixtures_indented_in_class *
3429                fixture1
3430                    line1
3431                    line2
3432                        indented line
3433                """
3434            )
3435        )
3436
3437    def test_show_fixtures_different_files(self, testdir):
3438        """`--fixtures` only shows fixtures from first file (#833)."""
3439        testdir.makepyfile(
3440            test_a='''
3441            import pytest
3442
3443            @pytest.fixture
3444            def fix_a():
3445                """Fixture A"""
3446                pass
3447
3448            def test_a(fix_a):
3449                pass
3450        '''
3451        )
3452        testdir.makepyfile(
3453            test_b='''
3454            import pytest
3455
3456            @pytest.fixture
3457            def fix_b():
3458                """Fixture B"""
3459                pass
3460
3461            def test_b(fix_b):
3462                pass
3463        '''
3464        )
3465        result = testdir.runpytest("--fixtures")
3466        result.stdout.fnmatch_lines(
3467            """
3468            * fixtures defined from test_a *
3469            fix_a
3470                Fixture A
3471
3472            * fixtures defined from test_b *
3473            fix_b
3474                Fixture B
3475        """
3476        )
3477
3478    def test_show_fixtures_with_same_name(self, testdir):
3479        testdir.makeconftest(
3480            '''
3481            import pytest
3482            @pytest.fixture
3483            def arg1():
3484                """Hello World in conftest.py"""
3485                return "Hello World"
3486        '''
3487        )
3488        testdir.makepyfile(
3489            """
3490            def test_foo(arg1):
3491                assert arg1 == "Hello World"
3492        """
3493        )
3494        testdir.makepyfile(
3495            '''
3496            import pytest
3497            @pytest.fixture
3498            def arg1():
3499                """Hi from test module"""
3500                return "Hi"
3501            def test_bar(arg1):
3502                assert arg1 == "Hi"
3503        '''
3504        )
3505        result = testdir.runpytest("--fixtures")
3506        result.stdout.fnmatch_lines(
3507            """
3508            * fixtures defined from conftest *
3509            arg1
3510                Hello World in conftest.py
3511
3512            * fixtures defined from test_show_fixtures_with_same_name *
3513            arg1
3514                Hi from test module
3515        """
3516        )
3517
3518    def test_fixture_disallow_twice(self):
3519        """Test that applying @pytest.fixture twice generates an error (#2334)."""
3520        with pytest.raises(ValueError):
3521
3522            @pytest.fixture
3523            @pytest.fixture
3524            def foo():
3525                raise NotImplementedError()
3526
3527
3528class TestContextManagerFixtureFuncs:
3529    @pytest.fixture(params=["fixture", "yield_fixture"])
3530    def flavor(self, request, testdir, monkeypatch):
3531        monkeypatch.setenv("PYTEST_FIXTURE_FLAVOR", request.param)
3532        testdir.makepyfile(
3533            test_context="""
3534            import os
3535            import pytest
3536            import warnings
3537            VAR = "PYTEST_FIXTURE_FLAVOR"
3538            if VAR not in os.environ:
3539                warnings.warn("PYTEST_FIXTURE_FLAVOR was not set, assuming fixture")
3540                fixture = pytest.fixture
3541            else:
3542                fixture = getattr(pytest, os.environ[VAR])
3543        """
3544        )
3545
3546    def test_simple(self, testdir, flavor):
3547        testdir.makepyfile(
3548            """
3549            from test_context import fixture
3550            @fixture
3551            def arg1():
3552                print("setup")
3553                yield 1
3554                print("teardown")
3555            def test_1(arg1):
3556                print("test1", arg1)
3557            def test_2(arg1):
3558                print("test2", arg1)
3559                assert 0
3560        """
3561        )
3562        result = testdir.runpytest("-s")
3563        result.stdout.fnmatch_lines(
3564            """
3565            *setup*
3566            *test1 1*
3567            *teardown*
3568            *setup*
3569            *test2 1*
3570            *teardown*
3571        """
3572        )
3573
3574    def test_scoped(self, testdir, flavor):
3575        testdir.makepyfile(
3576            """
3577            from test_context import fixture
3578            @fixture(scope="module")
3579            def arg1():
3580                print("setup")
3581                yield 1
3582                print("teardown")
3583            def test_1(arg1):
3584                print("test1", arg1)
3585            def test_2(arg1):
3586                print("test2", arg1)
3587        """
3588        )
3589        result = testdir.runpytest("-s")
3590        result.stdout.fnmatch_lines(
3591            """
3592            *setup*
3593            *test1 1*
3594            *test2 1*
3595            *teardown*
3596        """
3597        )
3598
3599    def test_setup_exception(self, testdir, flavor):
3600        testdir.makepyfile(
3601            """
3602            from test_context import fixture
3603            @fixture(scope="module")
3604            def arg1():
3605                pytest.fail("setup")
3606                yield 1
3607            def test_1(arg1):
3608                pass
3609        """
3610        )
3611        result = testdir.runpytest("-s")
3612        result.stdout.fnmatch_lines(
3613            """
3614            *pytest.fail*setup*
3615            *1 error*
3616        """
3617        )
3618
3619    def test_teardown_exception(self, testdir, flavor):
3620        testdir.makepyfile(
3621            """
3622            from test_context import fixture
3623            @fixture(scope="module")
3624            def arg1():
3625                yield 1
3626                pytest.fail("teardown")
3627            def test_1(arg1):
3628                pass
3629        """
3630        )
3631        result = testdir.runpytest("-s")
3632        result.stdout.fnmatch_lines(
3633            """
3634            *pytest.fail*teardown*
3635            *1 passed*1 error*
3636        """
3637        )
3638
3639    def test_yields_more_than_one(self, testdir, flavor):
3640        testdir.makepyfile(
3641            """
3642            from test_context import fixture
3643            @fixture(scope="module")
3644            def arg1():
3645                yield 1
3646                yield 2
3647            def test_1(arg1):
3648                pass
3649        """
3650        )
3651        result = testdir.runpytest("-s")
3652        result.stdout.fnmatch_lines(
3653            """
3654            *fixture function*
3655            *test_yields*:2*
3656        """
3657        )
3658
3659    def test_custom_name(self, testdir, flavor):
3660        testdir.makepyfile(
3661            """
3662            from test_context import fixture
3663            @fixture(name='meow')
3664            def arg1():
3665                return 'mew'
3666            def test_1(meow):
3667                print(meow)
3668        """
3669        )
3670        result = testdir.runpytest("-s")
3671        result.stdout.fnmatch_lines(["*mew*"])
3672
3673
3674class TestParameterizedSubRequest:
3675    def test_call_from_fixture(self, testdir):
3676        testdir.makepyfile(
3677            test_call_from_fixture="""
3678            import pytest
3679
3680            @pytest.fixture(params=[0, 1, 2])
3681            def fix_with_param(request):
3682                return request.param
3683
3684            @pytest.fixture
3685            def get_named_fixture(request):
3686                return request.getfixturevalue('fix_with_param')
3687
3688            def test_foo(request, get_named_fixture):
3689                pass
3690            """
3691        )
3692        result = testdir.runpytest()
3693        result.stdout.fnmatch_lines(
3694            [
3695                "The requested fixture has no parameter defined for test:",
3696                "    test_call_from_fixture.py::test_foo",
3697                "Requested fixture 'fix_with_param' defined in:",
3698                "test_call_from_fixture.py:4",
3699                "Requested here:",
3700                "test_call_from_fixture.py:9",
3701                "*1 error in*",
3702            ]
3703        )
3704
3705    def test_call_from_test(self, testdir):
3706        testdir.makepyfile(
3707            test_call_from_test="""
3708            import pytest
3709
3710            @pytest.fixture(params=[0, 1, 2])
3711            def fix_with_param(request):
3712                return request.param
3713
3714            def test_foo(request):
3715                request.getfixturevalue('fix_with_param')
3716            """
3717        )
3718        result = testdir.runpytest()
3719        result.stdout.fnmatch_lines(
3720            [
3721                "The requested fixture has no parameter defined for test:",
3722                "    test_call_from_test.py::test_foo",
3723                "Requested fixture 'fix_with_param' defined in:",
3724                "test_call_from_test.py:4",
3725                "Requested here:",
3726                "test_call_from_test.py:8",
3727                "*1 failed*",
3728            ]
3729        )
3730
3731    def test_external_fixture(self, testdir):
3732        testdir.makeconftest(
3733            """
3734            import pytest
3735
3736            @pytest.fixture(params=[0, 1, 2])
3737            def fix_with_param(request):
3738                return request.param
3739            """
3740        )
3741
3742        testdir.makepyfile(
3743            test_external_fixture="""
3744            def test_foo(request):
3745                request.getfixturevalue('fix_with_param')
3746            """
3747        )
3748        result = testdir.runpytest()
3749        result.stdout.fnmatch_lines(
3750            [
3751                "The requested fixture has no parameter defined for test:",
3752                "    test_external_fixture.py::test_foo",
3753                "",
3754                "Requested fixture 'fix_with_param' defined in:",
3755                "conftest.py:4",
3756                "Requested here:",
3757                "test_external_fixture.py:2",
3758                "*1 failed*",
3759            ]
3760        )
3761
3762    def test_non_relative_path(self, testdir):
3763        tests_dir = testdir.mkdir("tests")
3764        fixdir = testdir.mkdir("fixtures")
3765        fixfile = fixdir.join("fix.py")
3766        fixfile.write(
3767            textwrap.dedent(
3768                """\
3769                import pytest
3770
3771                @pytest.fixture(params=[0, 1, 2])
3772                def fix_with_param(request):
3773                    return request.param
3774                """
3775            )
3776        )
3777
3778        testfile = tests_dir.join("test_foos.py")
3779        testfile.write(
3780            textwrap.dedent(
3781                """\
3782                from fix import fix_with_param
3783
3784                def test_foo(request):
3785                    request.getfixturevalue('fix_with_param')
3786                """
3787            )
3788        )
3789
3790        tests_dir.chdir()
3791        testdir.syspathinsert(fixdir)
3792        result = testdir.runpytest()
3793        result.stdout.fnmatch_lines(
3794            [
3795                "The requested fixture has no parameter defined for test:",
3796                "    test_foos.py::test_foo",
3797                "",
3798                "Requested fixture 'fix_with_param' defined in:",
3799                "{}:4".format(fixfile),
3800                "Requested here:",
3801                "test_foos.py:4",
3802                "*1 failed*",
3803            ]
3804        )
3805
3806        # With non-overlapping rootdir, passing tests_dir.
3807        rootdir = testdir.mkdir("rootdir")
3808        rootdir.chdir()
3809        result = testdir.runpytest("--rootdir", rootdir, tests_dir)
3810        result.stdout.fnmatch_lines(
3811            [
3812                "The requested fixture has no parameter defined for test:",
3813                "    test_foos.py::test_foo",
3814                "",
3815                "Requested fixture 'fix_with_param' defined in:",
3816                "{}:4".format(fixfile),
3817                "Requested here:",
3818                "{}:4".format(testfile),
3819                "*1 failed*",
3820            ]
3821        )
3822
3823
3824def test_pytest_fixture_setup_and_post_finalizer_hook(testdir):
3825    testdir.makeconftest(
3826        """
3827        def pytest_fixture_setup(fixturedef, request):
3828            print('ROOT setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
3829        def pytest_fixture_post_finalizer(fixturedef, request):
3830            print('ROOT finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
3831    """
3832    )
3833    testdir.makepyfile(
3834        **{
3835            "tests/conftest.py": """
3836            def pytest_fixture_setup(fixturedef, request):
3837                print('TESTS setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
3838            def pytest_fixture_post_finalizer(fixturedef, request):
3839                print('TESTS finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
3840        """,
3841            "tests/test_hooks.py": """
3842            import pytest
3843
3844            @pytest.fixture()
3845            def my_fixture():
3846                return 'some'
3847
3848            def test_func(my_fixture):
3849                print('TEST test_func')
3850                assert my_fixture == 'some'
3851        """,
3852        }
3853    )
3854    result = testdir.runpytest("-s")
3855    assert result.ret == 0
3856    result.stdout.fnmatch_lines(
3857        [
3858            "*TESTS setup hook called for my_fixture from test_func*",
3859            "*ROOT setup hook called for my_fixture from test_func*",
3860            "*TEST test_func*",
3861            "*TESTS finalizer hook called for my_fixture from test_func*",
3862            "*ROOT finalizer hook called for my_fixture from test_func*",
3863        ]
3864    )
3865
3866
3867class TestScopeOrdering:
3868    """Class of tests that ensure fixtures are ordered based on their scopes (#2405)"""
3869
3870    @pytest.mark.parametrize("variant", ["mark", "autouse"])
3871    def test_func_closure_module_auto(self, testdir, variant, monkeypatch):
3872        """Semantically identical to the example posted in #2405 when ``use_mark=True``"""
3873        monkeypatch.setenv("FIXTURE_ACTIVATION_VARIANT", variant)
3874        testdir.makepyfile(
3875            """
3876            import warnings
3877            import os
3878            import pytest
3879            VAR = 'FIXTURE_ACTIVATION_VARIANT'
3880            VALID_VARS = ('autouse', 'mark')
3881
3882            VARIANT = os.environ.get(VAR)
3883            if VARIANT is None or VARIANT not in VALID_VARS:
3884                warnings.warn("{!r} is not  in {}, assuming autouse".format(VARIANT, VALID_VARS) )
3885                variant = 'mark'
3886
3887            @pytest.fixture(scope='module', autouse=VARIANT == 'autouse')
3888            def m1(): pass
3889
3890            if VARIANT=='mark':
3891                pytestmark = pytest.mark.usefixtures('m1')
3892
3893            @pytest.fixture(scope='function', autouse=True)
3894            def f1(): pass
3895
3896            def test_func(m1):
3897                pass
3898        """
3899        )
3900        items, _ = testdir.inline_genitems()
3901        request = FixtureRequest(items[0])
3902        assert request.fixturenames == "m1 f1".split()
3903
3904    def test_func_closure_with_native_fixtures(self, testdir, monkeypatch) -> None:
3905        """Sanity check that verifies the order returned by the closures and the actual fixture execution order:
3906        The execution order may differ because of fixture inter-dependencies.
3907        """
3908        monkeypatch.setattr(pytest, "FIXTURE_ORDER", [], raising=False)
3909        testdir.makepyfile(
3910            """
3911            import pytest
3912
3913            FIXTURE_ORDER = pytest.FIXTURE_ORDER
3914
3915            @pytest.fixture(scope="session")
3916            def s1():
3917                FIXTURE_ORDER.append('s1')
3918
3919            @pytest.fixture(scope="package")
3920            def p1():
3921                FIXTURE_ORDER.append('p1')
3922
3923            @pytest.fixture(scope="module")
3924            def m1():
3925                FIXTURE_ORDER.append('m1')
3926
3927            @pytest.fixture(scope='session')
3928            def my_tmpdir_factory():
3929                FIXTURE_ORDER.append('my_tmpdir_factory')
3930
3931            @pytest.fixture
3932            def my_tmpdir(my_tmpdir_factory):
3933                FIXTURE_ORDER.append('my_tmpdir')
3934
3935            @pytest.fixture
3936            def f1(my_tmpdir):
3937                FIXTURE_ORDER.append('f1')
3938
3939            @pytest.fixture
3940            def f2():
3941                FIXTURE_ORDER.append('f2')
3942
3943            def test_foo(f1, p1, m1, f2, s1): pass
3944        """
3945        )
3946        items, _ = testdir.inline_genitems()
3947        request = FixtureRequest(items[0])
3948        # order of fixtures based on their scope and position in the parameter list
3949        assert (
3950            request.fixturenames == "s1 my_tmpdir_factory p1 m1 f1 f2 my_tmpdir".split()
3951        )
3952        testdir.runpytest()
3953        # actual fixture execution differs: dependent fixtures must be created first ("my_tmpdir")
3954        FIXTURE_ORDER = pytest.FIXTURE_ORDER  # type: ignore[attr-defined]
3955        assert FIXTURE_ORDER == "s1 my_tmpdir_factory p1 m1 my_tmpdir f1 f2".split()
3956
3957    def test_func_closure_module(self, testdir):
3958        testdir.makepyfile(
3959            """
3960            import pytest
3961
3962            @pytest.fixture(scope='module')
3963            def m1(): pass
3964
3965            @pytest.fixture(scope='function')
3966            def f1(): pass
3967
3968            def test_func(f1, m1):
3969                pass
3970        """
3971        )
3972        items, _ = testdir.inline_genitems()
3973        request = FixtureRequest(items[0])
3974        assert request.fixturenames == "m1 f1".split()
3975
3976    def test_func_closure_scopes_reordered(self, testdir):
3977        """Test ensures that fixtures are ordered by scope regardless of the order of the parameters, although
3978        fixtures of same scope keep the declared order
3979        """
3980        testdir.makepyfile(
3981            """
3982            import pytest
3983
3984            @pytest.fixture(scope='session')
3985            def s1(): pass
3986
3987            @pytest.fixture(scope='module')
3988            def m1(): pass
3989
3990            @pytest.fixture(scope='function')
3991            def f1(): pass
3992
3993            @pytest.fixture(scope='function')
3994            def f2(): pass
3995
3996            class Test:
3997
3998                @pytest.fixture(scope='class')
3999                def c1(cls): pass
4000
4001                def test_func(self, f2, f1, c1, m1, s1):
4002                    pass
4003        """
4004        )
4005        items, _ = testdir.inline_genitems()
4006        request = FixtureRequest(items[0])
4007        assert request.fixturenames == "s1 m1 c1 f2 f1".split()
4008
4009    def test_func_closure_same_scope_closer_root_first(self, testdir):
4010        """Auto-use fixtures of same scope are ordered by closer-to-root first"""
4011        testdir.makeconftest(
4012            """
4013            import pytest
4014
4015            @pytest.fixture(scope='module', autouse=True)
4016            def m_conf(): pass
4017        """
4018        )
4019        testdir.makepyfile(
4020            **{
4021                "sub/conftest.py": """
4022                import pytest
4023
4024                @pytest.fixture(scope='package', autouse=True)
4025                def p_sub(): pass
4026
4027                @pytest.fixture(scope='module', autouse=True)
4028                def m_sub(): pass
4029            """,
4030                "sub/__init__.py": "",
4031                "sub/test_func.py": """
4032                import pytest
4033
4034                @pytest.fixture(scope='module', autouse=True)
4035                def m_test(): pass
4036
4037                @pytest.fixture(scope='function')
4038                def f1(): pass
4039
4040                def test_func(m_test, f1):
4041                    pass
4042        """,
4043            }
4044        )
4045        items, _ = testdir.inline_genitems()
4046        request = FixtureRequest(items[0])
4047        assert request.fixturenames == "p_sub m_conf m_sub m_test f1".split()
4048
4049    def test_func_closure_all_scopes_complex(self, testdir):
4050        """Complex test involving all scopes and mixing autouse with normal fixtures"""
4051        testdir.makeconftest(
4052            """
4053            import pytest
4054
4055            @pytest.fixture(scope='session')
4056            def s1(): pass
4057
4058            @pytest.fixture(scope='package', autouse=True)
4059            def p1(): pass
4060        """
4061        )
4062        testdir.makepyfile(**{"__init__.py": ""})
4063        testdir.makepyfile(
4064            """
4065            import pytest
4066
4067            @pytest.fixture(scope='module', autouse=True)
4068            def m1(): pass
4069
4070            @pytest.fixture(scope='module')
4071            def m2(s1): pass
4072
4073            @pytest.fixture(scope='function')
4074            def f1(): pass
4075
4076            @pytest.fixture(scope='function')
4077            def f2(): pass
4078
4079            class Test:
4080
4081                @pytest.fixture(scope='class', autouse=True)
4082                def c1(self):
4083                    pass
4084
4085                def test_func(self, f2, f1, m2):
4086                    pass
4087        """
4088        )
4089        items, _ = testdir.inline_genitems()
4090        request = FixtureRequest(items[0])
4091        assert request.fixturenames == "s1 p1 m1 m2 c1 f2 f1".split()
4092
4093    def test_multiple_packages(self, testdir):
4094        """Complex test involving multiple package fixtures. Make sure teardowns
4095        are executed in order.
4096        .
4097        └── root
4098            ├── __init__.py
4099            ├── sub1
4100            │   ├── __init__.py
4101            │   ├── conftest.py
4102            │   └── test_1.py
4103            └── sub2
4104                ├── __init__.py
4105                ├── conftest.py
4106                └── test_2.py
4107        """
4108        root = testdir.mkdir("root")
4109        root.join("__init__.py").write("values = []")
4110        sub1 = root.mkdir("sub1")
4111        sub1.ensure("__init__.py")
4112        sub1.join("conftest.py").write(
4113            textwrap.dedent(
4114                """\
4115            import pytest
4116            from .. import values
4117            @pytest.fixture(scope="package")
4118            def fix():
4119                values.append("pre-sub1")
4120                yield values
4121                assert values.pop() == "pre-sub1"
4122        """
4123            )
4124        )
4125        sub1.join("test_1.py").write(
4126            textwrap.dedent(
4127                """\
4128            from .. import values
4129            def test_1(fix):
4130                assert values == ["pre-sub1"]
4131        """
4132            )
4133        )
4134        sub2 = root.mkdir("sub2")
4135        sub2.ensure("__init__.py")
4136        sub2.join("conftest.py").write(
4137            textwrap.dedent(
4138                """\
4139            import pytest
4140            from .. import values
4141            @pytest.fixture(scope="package")
4142            def fix():
4143                values.append("pre-sub2")
4144                yield values
4145                assert values.pop() == "pre-sub2"
4146        """
4147            )
4148        )
4149        sub2.join("test_2.py").write(
4150            textwrap.dedent(
4151                """\
4152            from .. import values
4153            def test_2(fix):
4154                assert values == ["pre-sub2"]
4155        """
4156            )
4157        )
4158        reprec = testdir.inline_run()
4159        reprec.assertoutcome(passed=2)
4160
4161    def test_class_fixture_self_instance(self, testdir):
4162        """Check that plugin classes which implement fixtures receive the plugin instance
4163        as self (see #2270).
4164        """
4165        testdir.makeconftest(
4166            """
4167            import pytest
4168
4169            def pytest_configure(config):
4170                config.pluginmanager.register(MyPlugin())
4171
4172            class MyPlugin():
4173                def __init__(self):
4174                    self.arg = 1
4175
4176                @pytest.fixture(scope='function')
4177                def myfix(self):
4178                    assert isinstance(self, MyPlugin)
4179                    return self.arg
4180        """
4181        )
4182
4183        testdir.makepyfile(
4184            """
4185            class TestClass(object):
4186                def test_1(self, myfix):
4187                    assert myfix == 1
4188        """
4189        )
4190        reprec = testdir.inline_run()
4191        reprec.assertoutcome(passed=1)
4192
4193
4194def test_call_fixture_function_error():
4195    """Check if an error is raised if a fixture function is called directly (#4545)"""
4196
4197    @pytest.fixture
4198    def fix():
4199        raise NotImplementedError()
4200
4201    with pytest.raises(pytest.fail.Exception):
4202        assert fix() == 1
4203
4204
4205def test_fixture_param_shadowing(testdir):
4206    """Parametrized arguments would be shadowed if a fixture with the same name also exists (#5036)"""
4207    testdir.makepyfile(
4208        """
4209        import pytest
4210
4211        @pytest.fixture(params=['a', 'b'])
4212        def argroot(request):
4213            return request.param
4214
4215        @pytest.fixture
4216        def arg(argroot):
4217            return argroot
4218
4219        # This should only be parametrized directly
4220        @pytest.mark.parametrize("arg", [1])
4221        def test_direct(arg):
4222            assert arg == 1
4223
4224        # This should be parametrized based on the fixtures
4225        def test_normal_fixture(arg):
4226            assert isinstance(arg, str)
4227
4228        # Indirect should still work:
4229
4230        @pytest.fixture
4231        def arg2(request):
4232            return 2*request.param
4233
4234        @pytest.mark.parametrize("arg2", [1], indirect=True)
4235        def test_indirect(arg2):
4236            assert arg2 == 2
4237    """
4238    )
4239    # Only one test should have run
4240    result = testdir.runpytest("-v")
4241    result.assert_outcomes(passed=4)
4242    result.stdout.fnmatch_lines(["*::test_direct[[]1[]]*"])
4243    result.stdout.fnmatch_lines(["*::test_normal_fixture[[]a[]]*"])
4244    result.stdout.fnmatch_lines(["*::test_normal_fixture[[]b[]]*"])
4245    result.stdout.fnmatch_lines(["*::test_indirect[[]1[]]*"])
4246
4247
4248def test_fixture_named_request(testdir):
4249    testdir.copy_example("fixtures/test_fixture_named_request.py")
4250    result = testdir.runpytest()
4251    result.stdout.fnmatch_lines(
4252        [
4253            "*'request' is a reserved word for fixtures, use another name:",
4254            "  *test_fixture_named_request.py:5",
4255        ]
4256    )
4257
4258
4259def test_indirect_fixture_does_not_break_scope(testdir):
4260    """Ensure that fixture scope is respected when using indirect fixtures (#570)"""
4261    testdir.makepyfile(
4262        """
4263        import pytest
4264        instantiated  = []
4265
4266        @pytest.fixture(scope="session")
4267        def fixture_1(request):
4268            instantiated.append(("fixture_1", request.param))
4269
4270
4271        @pytest.fixture(scope="session")
4272        def fixture_2(request):
4273            instantiated.append(("fixture_2", request.param))
4274
4275
4276        scenarios = [
4277            ("A", "a1"),
4278            ("A", "a2"),
4279            ("B", "b1"),
4280            ("B", "b2"),
4281            ("C", "c1"),
4282            ("C", "c2"),
4283        ]
4284
4285        @pytest.mark.parametrize(
4286            "fixture_1,fixture_2", scenarios, indirect=["fixture_1", "fixture_2"]
4287        )
4288        def test_create_fixtures(fixture_1, fixture_2):
4289            pass
4290
4291
4292        def test_check_fixture_instantiations():
4293            assert instantiated == [
4294                ('fixture_1', 'A'),
4295                ('fixture_2', 'a1'),
4296                ('fixture_2', 'a2'),
4297                ('fixture_1', 'B'),
4298                ('fixture_2', 'b1'),
4299                ('fixture_2', 'b2'),
4300                ('fixture_1', 'C'),
4301                ('fixture_2', 'c1'),
4302                ('fixture_2', 'c2'),
4303            ]
4304    """
4305    )
4306    result = testdir.runpytest()
4307    result.assert_outcomes(passed=7)
4308
4309
4310def test_fixture_parametrization_nparray(testdir):
4311    pytest.importorskip("numpy")
4312
4313    testdir.makepyfile(
4314        """
4315        from numpy import linspace
4316        from pytest import fixture
4317
4318        @fixture(params=linspace(1, 10, 10))
4319        def value(request):
4320            return request.param
4321
4322        def test_bug(value):
4323            assert value == value
4324    """
4325    )
4326    result = testdir.runpytest()
4327    result.assert_outcomes(passed=10)
4328
4329
4330def test_fixture_arg_ordering(testdir):
4331    """
4332    This test describes how fixtures in the same scope but without explicit dependencies
4333    between them are created. While users should make dependencies explicit, often
4334    they rely on this order, so this test exists to catch regressions in this regard.
4335    See #6540 and #6492.
4336    """
4337    p1 = testdir.makepyfile(
4338        """
4339        import pytest
4340
4341        suffixes = []
4342
4343        @pytest.fixture
4344        def fix_1(): suffixes.append("fix_1")
4345        @pytest.fixture
4346        def fix_2(): suffixes.append("fix_2")
4347        @pytest.fixture
4348        def fix_3(): suffixes.append("fix_3")
4349        @pytest.fixture
4350        def fix_4(): suffixes.append("fix_4")
4351        @pytest.fixture
4352        def fix_5(): suffixes.append("fix_5")
4353
4354        @pytest.fixture
4355        def fix_combined(fix_1, fix_2, fix_3, fix_4, fix_5): pass
4356
4357        def test_suffix(fix_combined):
4358            assert suffixes == ["fix_1", "fix_2", "fix_3", "fix_4", "fix_5"]
4359        """
4360    )
4361    result = testdir.runpytest("-vv", str(p1))
4362    assert result.ret == 0
4363
4364
4365def test_yield_fixture_with_no_value(testdir):
4366    testdir.makepyfile(
4367        """
4368        import pytest
4369        @pytest.fixture(name='custom')
4370        def empty_yield():
4371            if False:
4372                yield
4373
4374        def test_fixt(custom):
4375            pass
4376        """
4377    )
4378    expected = "E               ValueError: custom did not yield a value"
4379    result = testdir.runpytest()
4380    result.assert_outcomes(errors=1)
4381    result.stdout.fnmatch_lines([expected])
4382    assert result.ret == ExitCode.TESTS_FAILED
4383