[TIP] configure a pytest fixture

Masiar, Peter (PMASIAR) PMASIAR at arinc.com
Wed Nov 16 08:38:39 PST 2016

List as default argument might not work as you expect (it is common Python gotcha):


So if you want to exploit such “static” biding, you are fine, but be aware what you are doing.

Just my $0.02, when I’ve seen mutable default argument it raised a red flag in code review ☺

[cid:image002.png at 01D23FFD.F94C7920]Peter Masiar / Sr Software Developer / ARINC Direct
Information Management Services
2551 Riva Road, Annapolis, MD 21401  USA
Phone: +1 410-573-3363 / Fax: +1 410-266-2266
pmasiar at arinc.com - www.rockwellcollins.com<http://www.rockwellcollins.com/>

From: testing-in-python-bounces at lists.idyll.org [mailto:testing-in-python-bounces at lists.idyll.org] On Behalf Of oliver
Sent: Wednesday, November 16, 2016 11:12 AM
To: Bruno Oliveira; Florian Bruhin; testing-in-python at lists.idyll.org
Subject: Re: [TIP] configure a pytest fixture

On Wed, 16 Nov 2016 at 08:00 Bruno Oliveira <nicoddemus at gmail.com<mailto:nicoddemus at gmail.com>> wrote:
On Wed, Nov 16, 2016 at 10:56 AM oliver <oliver.schoenborn at gmail.com<mailto:oliver.schoenborn at gmail.com>> wrote:

On Wed, Nov 16, 2016, 00:23 Florian Bruhin <me at the-compiler.org<mailto:me at the-compiler.org>> wrote:

* oliver <oliver.schoenborn at gmail.com<mailto: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):


# 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)
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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161116/823843bb/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 10518 bytes
Desc: image001.png
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161116/823843bb/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 7569 bytes
Desc: image002.png
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161116/823843bb/attachment-0003.png>

More information about the testing-in-python mailing list