[pytest-dev] Parametrized scope question

holger krekel holger at merlinux.eu
Mon Dec 14 11:17:24 EST 2015


Hi Alex,

On Mon, Dec 14, 2015 at 15:50 +0000, Alex Netes wrote:
> Hello guys,
> 
> I'm new to Pytest and I encounter something I cannot explain.

I also hit many things which i can not explain, even with pytest 
and maybe even with this very mail.

> I'm trying to give fixture fixt_func() a parameter fixt_prm and expect the fixture to be called only once as it defined with 'class' scope, but the fixture is called twice as it ignores the scope. What am I missing?
> 
> 
> @pytest.fixture(scope='class')
> def fixt_func(request, resource, fixt_prm):
>     print fixt_prm
> 
> class TestA():
>     @pytest.mark.parametrize('resource', ['x'], scope='class')
>     @pytest.mark.parametrize(fixt_prm ', ['x'], scope='class')
>     @pytest.mark.parametrize('prm', ['a', 'b'])
>     def test_a(self, prm, fixt_func)
>         assert True

You are doing something i wasn't aware is possible. You pass a parameter
to the fixture function fixt_func through parametrization but as a fixture.
In any case, if we modify your example to:

    import pytest
    @pytest.fixture(scope='class')
    def fixt_func(request, fixt_prm):
        print "fixt_func"

    class TestA():
        @pytest.mark.parametrize('fixt_prm', ['x', 'y'], scope='class')
        def test_a(self, fixt_func):
            assert True

this will also call fixt_func twice for the two executing tests.
It's argubaly "correct" behaviour from a certain point of view.
fixt_prm is class-scoped so each parameter instance exists as a
different "class-level" value so we interpret it to mean each
class-scoped fixture function needs to be executed with both values.

Just imagine we wouldn't parametrize on the function directly 
but through a fixture function:

    import pytest
    @pytest.fixture(scope='class')
    def fixt_func(request, fixt_prm):
        print "fixt_func"

    @pytest.fixture(scope='class', params=["x", "y"])
    def fixt_prm(request):
        print "fixt_prm"

    class TestA():
        def test_a(self, fixt_func):
            assert True

Here it's maybe more obvious why this executes both
fixture functions twice.

however, i am not sure about your precise example above.  I can see why
you expect the two different "prm" values (and thus test functions) to
execute with the same class-level fixtures.  Maybe others can chime in
and say if they consider your example a bug or a "usual" behaviour.

holger




> _______________________________________________
> pytest-dev mailing list
> pytest-dev at python.org
> https://mail.python.org/mailman/listinfo/pytest-dev


-- 
about me:    http://holgerkrekel.net/about-me/
contracting: http://merlinux.eu


More information about the pytest-dev mailing list