[TIP] configure a pytest fixture

oliver oliver.schoenborn at gmail.com
Wed Nov 16 08:11:58 PST 2016


On Wed, 16 Nov 2016 at 08:00 Bruno Oliveira <nicoddemus at gmail.com> wrote:

> On Wed, Nov 16, 2016 at 10:56 AM oliver <oliver.schoenborn at gmail.com>
> wrote:
>
>
>
> On Wed, Nov 16, 2016, 00:23 Florian Bruhin <me at the-compiler.org> wrote:
>
> Hi,
>
> * oliver <oliver.schoenborn at gmail.com> [2016-11-16 02:32:04 +0000]:
> > Thanks for the reply Bruno. I've used something like that in some
> fixtures
> > (actually returning a closure / lambda which, when called from the test,
> > returns a configured object), but it seems a little counter-intuitive
> (the
> > premise being that fixtures return objects ready to be used). How about
> > overloading the item operator [], so "def test(fixture[a,b])" would be
> > implemented in a similar way to annotations like "Map[str, int]" but
> > fixture[a,b] would allow the fixture function to receive a, b as
> > parameters.
>
> That's invalid Python syntax - fixture is an argument name here, not
> an object.
>
>
> Ah yes, rats! Then how about via a default value, this could easily be
> introspected by pytest:
>
> def test(fixture=a): would call fixture(a)
> def test(fixture=[a,b]): would call fixture(a, b)
>
>
> Not sure if that's not abusing the syntax and confusing to some, after all
> `fixture=1` seems like `fixture` would be `1`, but in fact could be
> anything returned by the fixture.
>
> My opinion is that I would not be a big fan of this.
>

If someone can understand that declaring an argument is actually going to
make the argument have the return value of the function that has the name
of the argument, they will certainly understand that for fixtures, the
default value is the args to the fixture. And there is no ambiguity,
because if the fixture is not declared it is not available to the function
(an auto-fixture cannot be configured). Perhaps the non-list version is not
necessary:

# fixture will have return value of fixture(123.  'some string'):
def test(fixture=[123, 'string']):

Another option that would allow some autoused fixtures to be configured
(scope must be compatible) would be a special pytest function. So given the
following:

# can be configured if desired:
@pytest.fixture(autouse=True, scope='method')
def autofixture(arg1=456):
     ...

then

# fixture will have return value of fixture(123.  'some string'), and
# autofixture for this method will be side-effects of call to
autofixture(456)
@pytest.config_fixtures(fixture=[123, 'string'])
def test1(fixture):
     ...

# autofixture for this method will be side-effects of call to
autofixture(789)
@pytest.config_fixtures(autofixture=[789])
def test2():
     ...

It's a bit more wordy than the default value approach, but still much
better than using a marker because no boiler-plate for me to write.

Oliver

-- 
Oliver
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161116/7b0843a6/attachment-0001.htm>


More information about the testing-in-python mailing list