[pytest-dev] Parametrized scope question

holger krekel holger at merlinux.eu
Tue Dec 15 15:07:04 EST 2015


On Tue, Dec 15, 2015 at 14:11 +0000, Alex Netes wrote:
> > I think you could use a marker on the class or even just a class attribute:
> > 
> >     import pytest
> > 
> >     @pytest.fixture(scope="class")
> >     def fix(request):
> >         return request.cls.attr * 10
> > 
> >     class TestA:
> >         attr = 1
> > 
> >         def test_one(self, fix):
> >             assert 0
> > 
> >     class TestB:
> >         attr = 2
> > 
> >         def test_one(self, fix):
> >             assert 0
> > 
> > The "request" object gives you back references into the context
> > of the test which requests fixtures.
> 
> Thanks. I think marker option better suite my needs. However, I have two questions.
> 1. I hit a problem with getting marker parameter from the fixture when it defined
> with 'class' scope. When I remove the scope, it works.
> 
>      Import pytest
> 
>     @pytest.fixture(scope="class")
>     def fix(request):
>         print request.node.get_marker('attr') # None
> 
>     @pytest.mark.attr('something')
>     class TestA:
>         def test_one(self, fix):
>             assert 0

I don't think there is much point in using a marker.
IISIC the missing marker on class-level is something
that should work.

>      ------------------------------
>      Import pytest
> 
>     @pytest.fixture
>     def fix(request):
>         print request.node.get_marker('attr') # <MarkInfo 'attr' args=('something',) kwargs={}>
> 
>     @pytest.mark.attr('something')
>     class TestA:
>         def test_one(self, fix):
>             assert 0
> 
> 2. Is it possible to provide arguments to the marker via the command line?

You can receive options via request.config.getoption(...) from a fixture
function.  You can not easily access command line options when applying
a marker because that happens at import time when options might not be
available yet. In some cases it might work to use ``pytest.config.getoption()``
within a marker however.

holger



More information about the pytest-dev mailing list